we use flink metrics to expose custom metrics - `<...
# troubleshooting
s
we use flink metrics to expose custom metrics -
<https://nightlies.apache.org/flink/flink-docs-master/docs/ops/metrics/>
but we have no idea how to add custom labels for metrics. even it’s possible or not, or what’s best practice for it in example:
Copy code
class MyMapper extends RichMapFunction[String,String] {
  @transient private var counter: Counter = _

  override def open(parameters: Configuration): Unit = {
    counter = getRuntimeContext()
      .getMetricGroup()
      .counter("myCounter")
  }

  override def map(value: String): String = {
    value match {
                  case "OK" => counter.inc("customLabel: SUCCESS") 
                  case "KO" => counter.inc("customLabel: FAIL") 
                  case _ => counter.inc("customLabel: UNKNOWN") 
                }
    value
  }
}
so depends on data we want to add some labels Is that possible? crazy idea comes to my head about create side output, generate metrics there and sink them via httpSink to prometheus push gateway 😄
a
Labels are just more groups in metric 😊 as far as I understand
Copy code
getRuntimeContext()
          .getMetricGroup()
          .addGroup("valid", result.toString)
          .addGroup("event", event)
          .addGroup("team", team)
          .counter("count")
s
Issue here that you’re not allowed to put custom metric depends on logic inside
processFunction
it’s fully static thing, and we want to make it a bit dynamic
a
That’s exactly what I’m doing with this code
s
wait, you have following code
Copy code
metric = getRuntimeContext()
          .getMetricGroup()
          .addGroup("valid", result.toString)
          .addGroup("event", event)
          .addGroup("team", team)
          .counter("count")
metric.inc()
inside process function, not in
open()
?
a
In open I define
_val nameToCounter_: mutable.HashMap[String, _Counter_]
When I first get specific set of values for this metric I put this counter into hashMap and use this HashMap in .process()
Copy code
def process()....
  nameToCounter(hashedSetOfValues).inc()
t
i have not been able to get grouping to act as a label, at least for prometheus. i wind up with string values concatenated into the metric name.