Hi, In principle you can register metric/metric groups dynamically it should be working just fine. However your code probably won't work, because per every record you are creating a new group and new counter, that most likely will be colliding with an old one. So every time you are defining a new group or new counter, you should remember it in some field.
Best, Piotrek pt., 17 gru 2021 o 14:03 Witold Baryluk <witold.bary...@gmail.com> napisaĆ(a): > Hi, > > I want to track and increment some monitoring counters from `map`, > but have them broken down by dynamically defined values of a label. > The set of values is unknown at creation time, but it is bounded > (less than 100 different values during a 30 day period, usually ~5). > > There is no easy way of doing this, compared to other Prometheus > native systems (i.e. in Go, or C++), but it looks like it might be possible > using some workarounds: > > > public class MyMapper extends RichMapFunction<String, String> { > private transient MetricGroup metric_group; > > @Override > public void open(Configuration config) { > this.metric_group = getRuntimeContext().getMetricGroup(); > } > > @Override > public String map(String value) throws Exception { > Group group = this.metric_group.addGroup("kind", getKind(value)); > group.counter("latency_sum").inc(getLatencyMicros(value)); > group.counter("latency_count").inc(); > return value; > } > } > > Will this work? Is there a better way? > > But, this does not look nice at all compared to how > other projects handle Prometheus labels. Flink metrics are not well > mapped into Prometheus metrics here. > > Second question, is it ok to call addGroup and counter, dynamically like > this, > or should it be cached? Do I need any such cache (which would be map > of string to Counter), be protected by some mutex when I lookup or add > to it? > > Cheers, > Witold >