Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate Tags caused by the conversion between '.' and '_' #5808

Open
zlsmith86 opened this issue Jan 15, 2025 · 1 comment
Open

Duplicate Tags caused by the conversion between '.' and '_' #5808

zlsmith86 opened this issue Jan 15, 2025 · 1 comment
Labels
registry: prometheus A Prometheus Registry related issue waiting for feedback We need additional information before we can continue

Comments

@zlsmith86
Copy link

Describe the bug
If a tag exists a.b and a_b, duplicate labels will be created in the prometheus output.

my_metric_name{a_b="alpha", a_b="alpha"}

Environment

  • Micrometer version: 1.11.9
  • Micrometer registry: 1.11.9
  • OS: docker-alpine, and Windows
  • Java version: 17

To Reproduce

@Test
public void TagTest() {
    Tags tags = Tags.of(Tag.of("a.b", "alpha"), Tag.of("a_b", "alpha"));
    assertThat(tags.stream()).hasSize(1);
}

Here is the current workaround that i put in place to account for this issue.

registry.config().meterFilter(
        new MeterFilter() {
            @Override
            public Meter.Id map(Meter.Id id) {

                HashSet<String> tagKeys = new HashSet<>();
                List<Tag> tags = new ArrayList<>();

                for (Tag tag : id.getTagsAsIterable()) {
                    if (!tagKeys.contains(tag.getKey().replace(".", "_"))) {
                        tags.add(tag);
                        tagKeys.add(tag.getKey().replace(".", "_"));
                    }
                    else {
                        System.out.println("Duplicate Tag:  " + tag.getKey());
                    }
                }

                return id.replaceTags(tags);
            }
        });

Expected behavior
duplicates will be detected in both output forms a.b and a_b

@shakuzen shakuzen added the registry: prometheus A Prometheus Registry related issue label Jan 16, 2025
@shakuzen
Copy link
Member

Do you not control the instrumentation that is defining the duplicate tag names? Note that Micrometer 1.11.x is no longer in OSS support - see our support page. With the newer Prometheus Java client (1.x) used since Micrometer 1.13, an exception will be thrown when scraping:

@Test
void duplicateTagNamesAfterNamingConvention() {
    Counter.builder("counter").tags("a.b", "alpha", "a_b", "alpha").register(registry).increment();
    System.out.println(registry.scrape()); // throws IllegalArgumentException: a_b: duplicate label name
}

The Tags class is not aware of naming conventions by design. The same Tags instance may be used with multiple different registries and naming conventions. Tags does deduplication, but a.b and a_b are different tag names.

If behavior were to change, it would be at the level of the PrometheusMeterRegistry or the Prometheus Java client, I think. It seems better to fix the instrumentation, though. What's the reason you would define tag names like a.b and a_b? For the scrape output with Micrometer 1.12 and earlier, how does the Prometheus server handle the duplicate labels when scraping?

Pinging @fstab in case you have any input on this from the Prometheus Java client's perspective.

@shakuzen shakuzen added waiting for feedback We need additional information before we can continue and removed waiting-for-triage labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
registry: prometheus A Prometheus Registry related issue waiting for feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

2 participants