Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The vast majority of custom metrics will be part of custom compiled devices. As an example, let’s assume you’re a device author and your device sends messages to a web service. You’d like to track the duration of the web service call, the upload volume (bytes uploaded) and rate (bytes per second) as well as the same metrics on the download side. You also want to track the message size sent to the service.

For tracking operation duration, you will need a histogram. You will also need a histogram to track the message size, transfer rate, and total transfer volume (one histogram for upload, and one for download).

Connexion’s BaseDevice class, from which all custom devices derive, contains scaffolding for declaring metrics. There are three methods exposed for creating metrics: GetOrCreateCounter, GetOrCreateGauge, and GetOrCreateHistogram. The MetricsProvider is also exposed should you need more advanced functionality.

Info

Connexion’s metrics implementation is closely related to the prometheus-net library. Many of our conventions and signatures mimic those of this library.

Your metrics should be created in the InitializeMetrics() override. By using this method, your metrics will automatically be updated as users enable and disable metrics via the UI. Let’s look at an example device class:

Code Block
breakoutModefull-width
languagec#
  public class MetricSample : BaseDevice<MetricSampleConfiguration>
  {
    private IHistogram m_SendDurationHistogram;   // measure the time taken to send the payload

    public override void Start()
    {
      // make sure you call InitializeMetrics
      InitializeMetrics();
    }
    protected override void InitializeMetrics()
    {
      // construct your metrics here
      m_SendDurationHistogram = GetOrCreateHistogram("MyDevice.send_duration_in_s",                           // name of your metric. See notes about X.y syntax
                                                     "The duration (in seconds) to send to the foo service",  // description
                                                     new HistogramConfiguration                               // configuration
                                                     {
                                                       LabelNames = new[] { "Group", "Tab", "Channel", "ChannelKey" },          // associate this metric with the owner channel
                                                       Buckets = new double[] { 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10 }  // buckets (in seconds)
                                                     }).WithLabels(MessageChannel.GroupName, MessageChannel.TabName, MessageChannel.ChannelName, MessageChannel.ChannelKeyString);
    }

    public override Task ProcessMessageAsync(IMessageContext context, CancellationToken cancellationToken)
    {
      // ...your code           
      using (m_SendDurationHistogram.NewTimer())    // the Histogram.NewTimer() method is used to measure the wrapped methods duration.
      { 
        await SendToFooServiceAsync(...).ConfigureAwait(false);
      }
      // ... your code
    }
  }