How to define additional or custom tags for default Spring Boot 2 metrics?

Recently I switched to Spring Boot 2 with Micrometer. As I got these shiny new metrics, I talked with our DevOps guys and we started exporting them to Telegraf.

To distinguish between different applications and application nodes, we decided to use tags. This works perfectly for custom metrics, but now I started thinking about the pre-defined. To achieve the same for default metrics, I need the ability to add extra tags for them as well.

Is it possible to achieve this? Am I doing this right?

EDIT: I tried next approach:

@Component
public class MyMetricsImpl implements MyMetrics {

    @Autowired
    protected MyProperties myProperties;
    @Autowired
    protected MeterRegistry meterRegistry;

    @PostConstruct
    public void initialize() {
        this.meterRegistry.config()
                .commonTags(commonTags());
    }

    @Override
    public List<Tag> commonTags() {
        List<Tag> tags = new ArrayList<>();
        tags.add(Tag.of("application", myProperties.getApplicationName()));
        tags.add(Tag.of("node", myProperties.getNodeName()));
        return tags;
    }
}

The problem is that my metrics behave correctly and even some of the Boot’s metrics (at least http.server.requests) see my tags. But jvm.*, system.*, tomcat.* and many others still don’t have the needed tags.

Answer

If you are looking for common tags support, you can do it by registering a MeterFilter doing it.

See this commit or this branch for an example.

With the upcoming Spring Boot 2.1.0.M1, you can use the following properties:

management.metrics.tags.*= # Common tags that are applied to every meter.

See the reference for details.


UPDATED:

As the question has been updated, I checked the updated question with this MeterFilter-based approach and confirmed it’s working as follows:

Request: http://localhost:8080/actuator/metrics/jvm.gc.memory.allocated

Response:

{
  "name" : "jvm.gc.memory.allocated",
  "measurements" : [ {
    "statistic" : "COUNT",
    "value" : 1.98180864E8
  } ],
  "availableTags" : [ {
    "tag" : "stack",
    "values" : [ "prod" ]
  }, {
    "tag" : "region",
    "values" : [ "us-east-1" ]
  } ]
}

I didn’t check the approach which has been provided in the updated question but I’d just use the proven MeterFilter-based approach unless there’s any reason to stick with the approach.


2nd UPDATED:

I looked into the approach and was able to reproduce it with this branch.

It’s too late to apply common tags in @PostConstruct as some metrics have been registered already. The reason why http.server.requests works is that it will be registered with the first request. Try to put a breakpoint on the point of filters’ application if you’re interested in it.

In short, try the above approach which is similar to the upcoming Spring Boot out-of-box support.

Leave a Reply

Your email address will not be published. Required fields are marked *