feat: add resourceAttributes support to OpenMetricsSink and PrometheusSink#213
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the metrics sinks by introducing a mechanism to attach global resource-level attributes to all emitted metrics. This allows for better contextualization and filtering of metrics in monitoring systems, enabling users to easily identify and categorize metrics based on their origin or environment. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Repository UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdded provider-based resource attributes across metrics sinks: OpenMetrics/Prometheus now accept a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request adds support for resourceAttributes to OpenMetricsSink and PrometheusSink, allowing for custom labels to be added to all metrics. However, the current implementation is vulnerable to Prometheus Exposition Format Injection because it does not validate or escape the keys provided in the resourceAttributes map, which could allow an attacker to inject arbitrary labels or disrupt the monitoring system. It is strongly recommended to add validation for label keys to ensure they follow Prometheus naming conventions. Additionally, while the implementation is clean and well-tested, consider refactoring the new tests to reduce duplication and improve maintainability.
metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt
Show resolved
Hide resolved
metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt
Show resolved
Hide resolved
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt (1)
151-163: Strengthen “on all samples” assertionsCurrent checks only prove labels exist somewhere in output, not on every sample line. A partial-label regression would still pass.
Proposed test tightening
val rendered = buffer.toString() - rendered.shouldContain("install_id=\"abc-123\"") - rendered.shouldContain("device_model=\"Pixel 9\"") + rendered.lineSequence() + .filter { it.isNotBlank() && !it.startsWith("#") } + .forEach { line -> + line.shouldContain("""install_id="abc-123"""") + line.shouldContain("""device_model="Pixel 9"""") + }Also applies to: 181-193
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt` around lines 151 - 163, The test currently only asserts that resource labels appear somewhere in the rendered output; update the "resource attributes are included as labels on all samples" test to assert they appear on every sample line by splitting the rendered output (from buffer.toString()) into lines, filtering to metric/sample lines (e.g., lines that are not empty and do not start with "#" or HELP/TYPE comments), and then for each remaining line assert it contains both install_id="abc-123" and device_model="Pixel 9"; apply the same tightening to the other test block that uses OpenMetricsSink and AppendableStringSink around snapshot rendering (the similar assertions at the other location).metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt (1)
693-726: Document parameter precedence in public KDocPlease add KDoc for
resourceAttributesandresourceAttributesProvider, especially precedence/override behavior when both are provided.As per coding guidelines
**/*.kt: Document public code via kdocs.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt` around lines 693 - 726, Add KDoc to the public factory functions OpenMetricsSink and PrometheusSink for the parameters resourceAttributes and resourceAttributesProvider clarifying usage and precedence: state that resourceAttributes is a convenience static map used when no provider is supplied, resourceAttributesProvider is a function that will be invoked with the Metric snapshot Meta to produce attributes, and when a non-default resourceAttributesProvider is passed it takes precedence over the resourceAttributes map (the default provider simply returns the resourceAttributes map). Keep the description concise and include parameter names exactly as resourceAttributes and resourceAttributesProvider so readers can find them in the signatures.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt`:
- Around line 677-686: The baseLabels function currently calls putAll(extras)
(the extras map derived from resourceAttributes), which can inject arbitrary
keys as metric label names; validate and sanitize those keys before merging: in
baseLabels (function baseLabels) filter the extras/resourceAttributes map to
only include keys that match Prometheus/OpenMetrics label name rules (e.g. regex
like "^[A-Za-z_][A-Za-z0-9_]*$") or transform invalid characters to valid ones,
then putAll the validated/sanitized map instead of the raw extras to prevent
emitting invalid label names.
---
Nitpick comments:
In
`@metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt`:
- Around line 693-726: Add KDoc to the public factory functions OpenMetricsSink
and PrometheusSink for the parameters resourceAttributes and
resourceAttributesProvider clarifying usage and precedence: state that
resourceAttributes is a convenience static map used when no provider is
supplied, resourceAttributesProvider is a function that will be invoked with the
Metric snapshot Meta to produce attributes, and when a non-default
resourceAttributesProvider is passed it takes precedence over the
resourceAttributes map (the default provider simply returns the
resourceAttributes map). Keep the description concise and include parameter
names exactly as resourceAttributes and resourceAttributesProvider so readers
can find them in the signatures.
In
`@metrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt`:
- Around line 151-163: The test currently only asserts that resource labels
appear somewhere in the rendered output; update the "resource attributes are
included as labels on all samples" test to assert they appear on every sample
line by splitting the rendered output (from buffer.toString()) into lines,
filtering to metric/sample lines (e.g., lines that are not empty and do not
start with "#" or HELP/TYPE comments), and then for each remaining line assert
it contains both install_id="abc-123" and device_model="Pixel 9"; apply the same
tightening to the other test block that uses OpenMetricsSink and
AppendableStringSink around snapshot rendering (the similar assertions at the
other location).
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.ktmetrics/src/jvmTest/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSinkJvmTest.kt
metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt
Show resolved
Hide resolved
metrics/src/commonMain/kotlin/pro/respawn/flowmvi/metrics/openmetrics/OpenMetricsSink.kt
Outdated
Show resolved
Hide resolved
…utesProvider 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.
Summary
OpenMetricsSinkandPrometheusSinknow accept aresourceAttributes: Map<String, String>parameter that appends fixed key-value labels (e.g.install_id,device_model) to every metric sample's label set.Testing
./gradlew :metrics:assemble./gradlew allTestsCloses #203
Summary by CodeRabbit