swift-prometheus icon indicating copy to clipboard operation
swift-prometheus copied to clipboard

Allow PrometheusClient to init with a predefined set of common dimensions.

Open avolokhov opened this issue 4 years ago • 2 comments

Feature Request

When reporting Prometheus metrics, it's sometimes useful to always append a certain set of dimensions to every published metric (e.g. nodeId, appName, etc.).

Motivation Behind Feature

We don't control metrics creation in dependencies and can't set dimensions to their metrics. This would mean we won't be able to distinguish such metrics published from different apps or nodes.

Feature Description

PrometheusClient may accept a set of common dimensions it'll append to every published metric.

class PrometheusClient {
  let dimensions: [(String, String)]

  <...>
}

I can see it might clash with current Labels signature and may require a "merge" method on the MetricLabels protocol:

public protocol MetricLabels: Encodable, Hashable {
    /// Create empty labels
    init()

    func append(dimensions: [(String, String)])
}

Alternatives or Workarounds

Currently I'm trying to use a custom conformance to MetricsFactory

class DimensionalPrometheusMetrics: MetricsFactory {
    let prometheus: PrometheusClient
    let commonDimensions: [(String, String)]

    <...>
}

Which is not ideal because it will only work when I publish metrics via swift-metrics api. If need custom buckets/quantiles I'll have to set dimensions manually.

avolokhov avatar Nov 13 '20 14:11 avolokhov

@avolokhov Thanks for opening this issue! I think this is a great feature to add.

However, with regards to "identifying metrics", the prometheus backend will automatically add job and instance labels to ALL metrics. Job being the job_name and instance being the host:port of the instance the metric was scraped from. I would suggest using these when possible, instead of adding them manually.

For other use-cases, I will look into adding base labels to PrometheusClient 👍

MrLotU avatar Nov 27 '20 13:11 MrLotU

1.0 shipped, so moving to 1.0.1

ktoso avatar Jun 23 '22 23:06 ktoso