Skip to content

Instantly share code, notes, and snippets.

@jkschneider
Last active September 6, 2018 11:59
Show Gist options
  • Save jkschneider/a7af362d51da99b21dd9c84484213a44 to your computer and use it in GitHub Desktop.
Save jkschneider/a7af362d51da99b21dd9c84484213a44 to your computer and use it in GitHub Desktop.
Demonstrating how to use a prefix with Micrometer Etsy-flavored Statsd bound for Graphite.
package io.micrometer.core.samples;
import io.micrometer.graphite.GraphiteHierarchicalNameMapper;
import io.micrometer.statsd.StatsdConfig;
import io.micrometer.statsd.StatsdFlavor;
import io.micrometer.statsd.StatsdMeterRegistry;
public class StatsdPrefixSample {
public static void main(String[] args) {
StatsdMeterRegistry registry = StatsdMeterRegistry
.builder(new StatsdConfig() {
@Override
public String get(String key) {
return null;
}
@Override
public StatsdFlavor flavor() {
return StatsdFlavor.ETSY;
}
})
.nameMapper(new GraphiteHierarchicalNameMapper("server"))
.lineSink(System.out::println)
.build();
registry.config().commonTags("server", "MYSERVERNAME");
registry.counter("my.counter", "othertag", "value").increment();
}
}
@jkschneider
Copy link
Author

jkschneider commented Sep 5, 2018

Line 23 is just for demonstration purposes, and shouldn't be part of your typical config. It just means that we are redirecting statsd lines to System.out rather than to the default UDP port.

@jkschneider
Copy link
Author

In Spring Boot, Line 19 can be configured with management.metrics.export.statsd.flavor=etsy instead.

@jkschneider
Copy link
Author

jkschneider commented Sep 5, 2018

Because we are using a non-default HierarchicalNameMapper, namely GraphiteHierarchicalNameMapper, you must provide your own wiring for StatsdMeterRegistry. Spring Boot's autoconfiguration will step aside in the presence of this @Bean:

@Bean StatsdMeterRegistry graphiteStyleStatsdRegistry(StatsdConfig config) {
   return StatsdMeterRegistry.builder(config)
      .nameMapper(new GraphiteHierarchicalNameMapper("server"))
      .build();
}

The constructor argument to GraphiteHierarchicalNameMapper contains the tagsAsPrefix definition.

@jkschneider
Copy link
Author

Line 26 should be defined in a @Bean MeterRegistryCustomizer as seen in the Spring Boot reference docs.

@oren659
Copy link

oren659 commented Sep 6, 2018

Hi John,

Thanks for publishing this code, got a few questions:

  1. per your comment: "Line 23 is just for demonstration purposes, and shouldn't be part of your typical config. It just means that we are redirecting statsd lines to System.out rather than to the default UDP port."
    when i remove this line i don't see any publish messages via wire-shark, not to my configure host at
    management.export.statsd.host: or management.export.statsd.host. and also not to localhost which is the default apparently.

  2. per your comment:"In Spring Boot, Line 19 can be configured with management.metrics.export.statsd.flavor=etsy instead."
    it seems that both ways produce different results:
    with line 19:
    MYSERVERNAME.myCounter.othertag.value.statistic.count:1|c
    without line 19 and with etsy flavor property:
    my.counter:1|c|#statistic:count,othertag:value,server:MYSERVERNAME

  3. Calling to line 28 multiple time gives the same result, should i expect it to increment the number between calls ?
    MYSERVERNAME.myCounter.othertag.value.statistic.count:1|c
    MYSERVERNAME.myCounter.othertag.value.statistic.count:1|c
    MYSERVERNAME.myCounter.othertag.value.statistic.count:1|c

  4. Adding a simple private method with @timed annotation and calling it from main doesn't produce any more printouts, should it ?
    @timed
    private void simplePrivateMethod(){

    }
    Any chance you can add example of a timer and a counter ?
    i was able to mesure the time of the simplePrivateMethod by wrapping the method call with this:
    Timer timer = Timer.builder("myservice").tag("method", "manual").register(registry);
    long start = System.nanoTime();
    StatsdPrefixSample statsdPrefixSample=new StatsdPrefixSample();
    statsdPrefixSample.simplePrivateMethod();
    but that made me confuse about the use of the @timed annotation, do both measure the time of the method execution ?

5 What about all the "outOfTheBox" meters that are being sent such as JVM, Tomcat etc? will they get the prefix "MYSERVERNAME" too ?
6. per your comment "Line 26 should be defined in a @bean MeterRegistryCustomizer as seen in the Spring Boot reference docs."
after defining it as a bean i got
myCounter.othertag.value.statistic.count:1|c instead of
MYSERVERNAME.myCounter.othertag.value.statistic.count:1|c

Thanks in advanced for your help, my organization really want to replace all of our meter handling to Micrometer but we so far we are struggling with setting it up.

Can we have a quick call to troubleshoot this ? or maybe i can upload my code so we can find the issue with it ?
Thanks,
Oren

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment