Skip to content

Commit fb356fa

Browse files
committed
refactor: remove static resourceAttributes in favor of resourceAttributesProvider
The `resourceAttributes` parameter has been removed from `toOtlpPayload`, `OtlpJsonSink`, `OpenMetricsSink`, and `PrometheusSink`. Callers should now use `resourceAttributesProvider` to provide dynamic attributes based on the snapshot metadata. Tests have been updated to reflect this change.
1 parent a6fbbc3 commit fb356fa

4 files changed

Lines changed: 17 additions & 20 deletions

File tree

metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,7 @@ public fun OpenMetricsSink(
698698
includeUnit: Boolean = true,
699699
includeTimestamp: Boolean = false,
700700
surfaceVersion: MetricsSchemaVersion? = null,
701-
resourceAttributes: Map<String, String> = emptyMap(),
702-
resourceAttributesProvider: (Meta) -> Map<String, String> = { resourceAttributes },
701+
resourceAttributesProvider: (Meta) -> Map<String, String> = { emptyMap() },
703702
): MetricsSink = MappingSink(delegate) {
704703
val surface = MetricSurface.fromVersion(surfaceVersion ?: it.meta.schemaVersion)
705704
val snapshot = it.downgradeTo(surface.version)
@@ -721,8 +720,7 @@ public fun PrometheusSink(
721720
includeUnit: Boolean = false,
722721
includeTimestamp: Boolean = false,
723722
surfaceVersion: MetricsSchemaVersion? = null,
724-
resourceAttributes: Map<String, String> = emptyMap(),
725-
resourceAttributesProvider: (Meta) -> Map<String, String> = { resourceAttributes },
723+
resourceAttributesProvider: (Meta) -> Map<String, String> = { emptyMap() },
726724
): MetricsSink = MappingSink(delegate) {
727725
val surface = MetricSurface.fromVersion(surfaceVersion ?: it.meta.schemaVersion)
728726
val snapshot = it.downgradeTo(surface.version)

metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/otel/OtlpJsonSink.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ private val DefaultJson: Json = Json {
3434
/** Maps a [MetricsSnapshot] to an OTLP JSON payload ready for serialization. */
3535
public fun MetricsSnapshot.toOtlpPayload(
3636
namespace: String = DEFAULT_NAMESPACE,
37-
resourceAttributes: Map<String, String> = emptyMap(),
38-
resourceAttributesProvider: (Meta) -> Map<String, String> = { resourceAttributes },
37+
resourceAttributesProvider: (Meta) -> Map<String, String> = { emptyMap() },
3938
scopeName: String = DEFAULT_SCOPE,
4039
temporality: AggregationTemporality = AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE,
4140
fixedTimestamp: Instant? = null,
@@ -71,16 +70,14 @@ public fun OtlpJsonMetricsSink(
7170
delegate: Sink<String>,
7271
namespace: String = DEFAULT_NAMESPACE,
7372
scopeName: String = DEFAULT_SCOPE,
74-
resourceAttributes: Map<String, String> = emptyMap(),
75-
resourceAttributesProvider: (Meta) -> Map<String, String> = { resourceAttributes },
73+
resourceAttributesProvider: (Meta) -> Map<String, String> = { emptyMap() },
7674
temporality: AggregationTemporality = AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE,
7775
fixedTimestamp: Instant? = null,
7876
surfaceVersion: MetricsSchemaVersion? = null,
7977
json: Json = DefaultJson,
8078
): MetricsSink = MappingSink(delegate) { snapshot ->
8179
val payload = snapshot.toOtlpPayload(
8280
namespace = namespace,
83-
resourceAttributes = resourceAttributes,
8481
resourceAttributesProvider = resourceAttributesProvider,
8582
scopeName = scopeName,
8683
temporality = temporality,

metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class OpenMetricsSinkJvmTest : FreeSpec({
152152
val buffer = StringBuilder()
153153
val sink = OpenMetricsSink(
154154
delegate = AppendableStringSink(buffer),
155-
resourceAttributes = mapOf("install_id" to "abc-123", "device_model" to "Pixel 9"),
155+
resourceAttributesProvider = { mapOf("install_id" to "abc-123", "device_model" to "Pixel 9") },
156156
)
157157

158158
sink.emit(snapshot)
@@ -162,27 +162,27 @@ class OpenMetricsSinkJvmTest : FreeSpec({
162162
rendered.shouldContain("device_model=\"Pixel 9\"")
163163
}
164164

165-
"resource attributes provider overrides static attributes and meta defaults" {
165+
"resource attributes provider overrides meta defaults" {
166166
val buffer = StringBuilder()
167167
val sink = OpenMetricsSink(
168168
delegate = AppendableStringSink(buffer),
169-
resourceAttributes = mapOf("env" to "staging"),
170-
resourceAttributesProvider = { mapOf("env" to "production", "store" to "custom_store") },
169+
resourceAttributesProvider = {
170+
mapOf("env" to "production", "store" to "custom_store")
171+
},
171172
)
172173

173174
sink.emit(snapshot)
174175

175176
val rendered = buffer.toString()
176177
rendered.shouldContain("env=\"production\"")
177178
rendered.shouldContain("store=\"custom_store\"")
178-
rendered.shouldNotContain("env=\"staging\"")
179179
}
180180

181181
"prometheus sink resource attributes are included as labels on all samples" {
182182
val buffer = StringBuilder()
183183
val sink = PrometheusSink(
184184
delegate = AppendableStringSink(buffer),
185-
resourceAttributes = mapOf("install_id" to "abc-123", "device_model" to "Pixel 9"),
185+
resourceAttributesProvider = { mapOf("install_id" to "abc-123", "device_model" to "Pixel 9") },
186186
)
187187

188188
sink.emit(snapshot)
@@ -192,11 +192,10 @@ class OpenMetricsSinkJvmTest : FreeSpec({
192192
rendered.shouldContain("device_model=\"Pixel 9\"")
193193
}
194194

195-
"prometheus sink resource attributes provider overrides static attributes and meta defaults" {
195+
"prometheus sink resource attributes provider overrides meta defaults" {
196196
val buffer = StringBuilder()
197197
val sink = PrometheusSink(
198198
delegate = AppendableStringSink(buffer),
199-
resourceAttributes = mapOf("env" to "staging"),
200199
resourceAttributesProvider = { mapOf("env" to "production", "store" to "custom_store") },
201200
)
202201

@@ -205,6 +204,5 @@ class OpenMetricsSinkJvmTest : FreeSpec({
205204
val rendered = buffer.toString()
206205
rendered.shouldContain("env=\"production\"")
207206
rendered.shouldContain("store=\"custom_store\"")
208-
rendered.shouldNotContain("env=\"staging\"")
209207
}
210208
})

metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/otel/OtlpJsonSinkTest.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class OtlpJsonSinkTest : FreeSpec({
2626

2727
"resource attributes include store identity and extras" {
2828
val payload = snapshot.toOtlpPayload(
29-
resourceAttributes = mapOf("service.name" to "demo-service"),
29+
resourceAttributesProvider = {
30+
mapOf("service.name" to "demo-service")
31+
},
3032
scopeName = "flowmvi.metrics"
3133
)
3234

@@ -281,7 +283,9 @@ class OtlpJsonSinkTest : FreeSpec({
281283

282284
"resource attributes are sorted lexicographically after merge" {
283285
val payload = snapshot.toOtlpPayload(
284-
resourceAttributes = mapOf("z-key" to "z", "a-key" to "a")
286+
resourceAttributesProvider = {
287+
mapOf("z-key" to "z", "a-key" to "a")
288+
}
285289
)
286290
val keys = payload.resourceMetrics.single().resource!!.attributes.map { it.key }
287291

0 commit comments

Comments
 (0)