eclipse / microprofile-metrics

microprofile-metrics
Apache License 2.0
101 stars 66 forks source link

Need a way to specify a metric name for metrics created by non-mp-metrics annotations #237

Open donbourne opened 6 years ago

donbourne commented 6 years ago

For the fault tolerance sub-spec, a number of annotations exist - specifically @Retry, @Timeout, @CircuitBreaker and @Bulkhead. Each of these annotations can be used to decorate a method. Since these annotations don't include the naming attributes that our mpMetrics rely on, there is currently no way for those fault tolerance annotations to let the developer indicate what name they want to use for the metrics created by each of the fault-tolerance-originating metrics.

One suggested design is to provide a new annotation that can be used on the same method as one or more fault tolerance annotations:

see https://github.com/eclipse/microprofile-fault-tolerance/issues/234 for discussion illustrating the problem.

rsoika commented 5 years ago

I have a question about the status of this issue because I have exactly the same situation as described by @donbourne :

I register one Counter Metric with a general name and add different Tags. Running on Wilfly 16.0.0 this does not duplicate the metric if the tags change. It only updates my coutner but is ignoring the updated Tags. The metric is not duplicated. The expected result should look like this:

 application:my_coutner{category="ABC"} 3.0
 application:my_coutner{category="DEF"} 1.0
donbourne commented 5 years ago

@rsoika with the MP Metrics 2.0 release you can do what you're describing. With v2.0, tags are used in combination with the metric's name to create the identity of the metric. That means you can do something like this:

@Counted(name = "my_coutner", absolute = true, tags={"category=ABC"})
public void countMeA() { }

@Counted(name = "my_coutner", absolute = true, tags={"category=DEF"})
public void countMeB() { }

Note that, when using the MP Metrics v1.1.x release, tags are part of the metric's metadata (and each metric name is associated with only one metadata instance, and that metadata instance is expected to be invariant).

rsoika commented 5 years ago

Thanks for that info. So I guess Wildfly 16.0.0 is still using Metrics v1.1 , I will check this....

If I need to create a metric programatically is the following code the right way:

Map<String,String> metaDataMap=new HashMap<String, String>();

metaDataMap.put("name", "my_metric");
metaDataMap.put("type", MetricType.COUNTER.toString());
metaDataMap.put("reusable", "true");
metaDataMap.put("displayName", "");
metaDataMap.put("description", "");
metaDataMap.put("unit", MetricUnits.NONE.toString());
// create metric 1 with a custom tag value
Metadata metric_1=new Metadata(metaDataMap);
metric_1.addTag("type=ABC");
metricRegistry.counter(metric_1);

Sorry for asking this coding question here, but in the moment there are not so many examples available for this case. Most address only the annotations which I can not use in my case.

rsoika commented 5 years ago

Wildfly 16.0.0 is using microprofile-metrics-api-1.1.jar. This explains why I failed with my implementation. Thank you for clarifying.

donbourne commented 5 years ago

@rsoika to get a Counter programatically, you'd probably do this...

Metadata m = Metadata.builder()
  .withName("my_metric")
  .withDescription("my description")
  .withType(MetricType.COUNTER)
  .build();

Tags[] tags = {new Tag("type","ABC")};

Counter counter = metricRegistry.counter(m, tags);
rsoika commented 5 years ago

Thanks a lot! I wrote a short Blog about 'How Create a Metric Programatically' with your examples.

rsoika commented 5 years ago

Just to confirm the behavior of MP Metrics v2.0.0:

I build a custom Docker image with Wildfly 16.0.0 (which is running MP Metrics v1.1) and updated the Microprofile Metric to v2.0.0 -> Works great now! Counter metric with custom tags behaves as expected. See also discussion about Wildfly here.