Search code examples
cadence-workflow

How can I emit custom metrics from a Cadence workflow or activity?


Cadence emits a bunch of metrics using tally. Is it possible to emit my own metrics using Cadence SDK?

Go SDK has cadence.GetMetricsScope().Counter(counterName).Inc() but it doesn’t seem to work when I just call it. Am I missing some required configuration?


Solution

  • You can add your own metrics using both the Go and Java SDKs. The following examples show how to do it using the Go SDK, and Java SDK isn’t much different than it.

    In workflow code:

    cadence.GetMetricsScope().Counter(counterName).Inc(1)
    

    In activity code:

    cadence.GetActivityMetricsScope().Counter(counterName).Inc(1)
    

    However, just doing that will not be sufficient to actually emit the metrics. The reason is that tally uses tally.NoopScope by default, which does nothing as the name implies. Therefore, you’d also need to set up the MetricsScope as part of the WorkerOptions as in the following example:

    workerOptions := cadence.WorkerOptions{
     MetricsScope: myTallyScope, 
    }
    worker := cadence.NewWorker(service, domain, taskList, workerOptions)
    

    You can create myTallyScope by following the examples in tally documentation for Go and Java.