.netmultithreadingprometheusprometheus-net

Are Metrics instances from the Prometheus .NET client library thread-safe


For prometheus-net, none of the samples make it particularly clear whether instances of a given Metric are thread-safe:

i.e. in a random controller method, am I supposed to do:

static readonly Histogram xyzMetric = Metrics.CreateHistogram(...)

Or is the intended usage that I always create a local instance and manage that without sharing it across threads?

Given the various amounts of configuration DSLery involved, I trust the answer is yes (as it is for the Java clients)... Am I right ?


Solution

  • As of 30 Jan 2019, https://github.com/prometheus-net/prometheus-net#best-practices-and-usage states that

    The library is thread-safe

    However, there are some elements, such as histograms, in which there is no guarantee, due to the way in which metrics are gathered, and then rendered for the scrape, which can yield very minor glitches, e.g. as noted in https://github.com/prometheus-net/prometheus-net/blob/master/Prometheus.NetStandard/Histogram.cs#L10

    /// The histogram is thread-safe but not atomic - the sum of values and total count of events
    /// may not add up perfectly with bucket contents if new observations are made during a collection.