|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftPrometheus open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2025 SwiftPrometheus project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftPrometheus project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/// A descriptor that defines the components of a fully qualified Prometheus metric name. |
| 16 | +/// |
| 17 | +/// The final, underscore-separated metric name is generated by the ``name`` computed property. The ``helpText`` |
| 18 | +/// is used to generate the corresponding `# HELP` line in the Prometheus exposition format. |
| 19 | +/// - Warning: This initializer will trigger a `preconditionFailure` if ``metricName`` is an empty string. |
| 20 | +public struct MetricNameDescriptor { |
| 21 | + /// An optional top-level namespace for the metric. |
| 22 | + public let namespace: String? |
| 23 | + |
| 24 | + /// An optional subsystem to group related metrics. |
| 25 | + public let subsystem: String? |
| 26 | + |
| 27 | + /// The required, descriptive base name of the metric. |
| 28 | + public let metricName: String |
| 29 | + |
| 30 | + /// An optional suffix describing the metric's unit (e.g., `total`). |
| 31 | + public let unitName: String? |
| 32 | + |
| 33 | + /// Optional help text for the metric. If a non-empty string is provided, it will be emitted as a `# HELP` line in the exposition format. |
| 34 | + /// If the parameter is omitted or an empty string is passed, the `# HELP` line will not be generated for this metric. |
| 35 | + public let helpText: String? |
| 36 | + |
| 37 | + /// Creates a new ``MetricNameDescriptor`` that defines the components of a fully qualified Prometheus metric name. |
| 38 | + /// |
| 39 | + /// - Parameter namespace: An optional top-level namespace for the metric. |
| 40 | + /// - Parameter subsystem: An optional subsystem to group related metrics within a namespace. |
| 41 | + /// - Parameter metricName: The required, descriptive base name of the metric. |
| 42 | + /// - Parameter unitName: An optional suffix describing the metric's unit (e.g., `total`). |
| 43 | + /// - Parameter helpText: Optional help text for the metric. If a non-empty string is provided, it will be emitted as a `# HELP` line in the exposition format. |
| 44 | + /// If the parameter is omitted or an empty string is passed, the `# HELP` line will not be generated for this metric. |
| 45 | + public init( |
| 46 | + namespace: String? = nil, |
| 47 | + subsystem: String? = nil, |
| 48 | + metricName: String, |
| 49 | + unitName: String? = nil, |
| 50 | + helpText: String? = nil |
| 51 | + ) { |
| 52 | + precondition(!metricName.isEmpty, "metricName must not be empty") |
| 53 | + self.namespace = namespace |
| 54 | + self.subsystem = subsystem |
| 55 | + self.metricName = metricName |
| 56 | + self.unitName = unitName |
| 57 | + self.helpText = helpText |
| 58 | + } |
| 59 | + |
| 60 | + /// The fully qualified metric name, joining non-empty components with underscores (e.g. `namespace_subsytem_metricName_unitName`). |
| 61 | + public var name: String { |
| 62 | + [namespace, subsystem, metricName, unitName] |
| 63 | + .compactMap { $0?.isEmpty == false ? $0 : nil } |
| 64 | + .joined(separator: "_") |
| 65 | + } |
| 66 | +} |
0 commit comments