Hello team, I am a beginner with Flink and I want...
# troubleshooting
n
Hello team, I am a beginner with Flink and I want to register some metrics and I am reading this guide . I have written the following simple code in java:
Copy code
public class FlinkMetric extends RichMapFunction<String, String> {

	public void playWithMetrics() {
		// Gauge
		Gauge<Integer> random = () -> 1;
		this.getRuntimeContext().getMetricGroup().gauge("myGauge", random);
	}

	@Override
  	public String map(String value) throws Exception {
	    return null;
  	}

}
This class is simply called my the main class and I am running my application as a jar by submitting it on flink web's submit job option. I am getting the following error:
Copy code
java.lang.IllegalStateException: The runtime context has not been initialized yet.
I looked into it and I understand that the runtime context is null. Can you please tell me why the runtime context is not getting set automatically? Thanks
s
Hey, it must be registered in the
open
method first as a metric, you only have access to the runtime context there.
n
Hi @sap1ens thanks for replying. I tried adding the same code in open(override like shown in docs) and still getting the same error. I moved playWithMetrics code to open() and then from the main class I called new
FlinkMetric().open(new Configuration())
I don't understand when is the lifecycle open() called? is it the right way?
d
The constructor is called in the client, before the job graph is serialized and sent off to the cluster for execution. At that point, there is no runtime context. You need to do things like this in the open() method, which is called as the cluster is starting the job.
n
Thanks @David Anderson . So there is no way to report metrics when the application is running on the cluster?
d
On the contrary. There's no way to report metrics during the phase before the job starts running.
n
Can you please let me know how to report metrics in my example? I have two simple classes: 1. Main class 2. FlinkMetrics class that extends RichMapFunction and tries to collect metrics in open() method. The application is built into a jar and then submitted as a job on the apache flink web UI running on the default port 8081. Thanks. I am trying to understand the metrics lifecycle here
Your question isn't really clear. If you need more help, please share all of the code, and tell us what you are doing to inspect the metrics.
s
Just wanted to add, you can’t use metrics functionality separately from your business logic. So instead of writing a separate class for metrics think how you can add them to your operators actually processing the data
👍 1
n
Hi @sap1ens and @David Anderson Thanks for your help, I have understood the concept and am able to see metrics now.
👍 1
flink 1