Skip to main content

Use metrics to monitor functions

To ensure that running functions are healthy at any time, you can configure functions to publish arbitrary metrics to the metrics interface that can be queried.

note

Using the language-native interface for Java or Python is not able to publish metrics and stats to Pulsar.

You can use both built-in metrics and customized metrics to monitor functions.

  • Use the built-in function metrics. Pulsar Functions expose the metrics that can be collected and used for monitoring the health of Java, Python, and Go functions. You can check the metrics by following the monitoring guide.
  • Set your customized metrics. In addition to the built-in metrics, Pulsar allows you to customize metrics for Java and Python functions. Function workers collect user-defined metrics to Prometheus automatically and you can check them in Grafana.

Here is an example of how to customize metrics for Java, Python and Go functions by using the Context object on a per-key basis. For example, you can set a metric for the process-count key and set another one for the elevens-count key every time the function processes a message.

import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;

public class MetricRecorderFunction implements Function<Integer, Void> {
@Override
public void apply(Integer input, Context context) {
// Records the metric 1 every time a message arrives
context.recordMetric("hit-count", 1);

// Records the metric only if the arriving number equals 11
if (input == 11) {
context.recordMetric("elevens-count", 1);
}

return null;
}
}