-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
52 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
Sources/Exporters/OpenTelemetryProtocolCommon/common/ExporterMetrics.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
public class ExporterMetrics { | ||
public enum TransporterType: String { | ||
case grpc = "grpc" | ||
case protoBuf = "http" | ||
case httpJson = "http-json" | ||
} | ||
|
||
public static let ATTRIBUTE_KEY_TYPE: String = "type" | ||
public static let ATTRIBUTE_KEY_SUCCESS: String = "success" | ||
|
||
private let meterProvider: StableMeterProvider | ||
private let exporterName: String | ||
private let transportName: String | ||
private var seenAttrs: [String: AttributeValue] = [:] | ||
private var successAttrs: [String: AttributeValue] = [:] | ||
private var failedAttrs: [String: AttributeValue] = [:] | ||
|
||
private var seen: LongCounter? | ||
private var exported: LongCounter? | ||
|
||
public init( | ||
type: String, | ||
meterProvider: StableMeterProvider, | ||
exporterName: String, | ||
transportName: TransporterType | ||
) { | ||
self.meterProvider = meterProvider | ||
self.exporterName = exporterName | ||
self.transportName = transportName.rawValue | ||
self.seenAttrs = [ | ||
ExporterMetrics.ATTRIBUTE_KEY_TYPE: .string(type) | ||
] | ||
self.successAttrs = [ | ||
ExporterMetrics.ATTRIBUTE_KEY_SUCCESS: .bool(true) | ||
] | ||
self.failedAttrs = [ | ||
ExporterMetrics.ATTRIBUTE_KEY_SUCCESS: .bool(false) | ||
] | ||
|
||
self.seen = meter.counterBuilder(name: "\(exporterName).exporter.seen").build() | ||
self.exported = meter.counterBuilder(name: "\(exporterName).exporter.exported").build() | ||
|
||
} | ||
|
||
public func addSeen(value: Int) -> Void { | ||
seen?.add(value: value, attribute: seenAttrs) | ||
} | ||
|
||
public func addSuccess(value: Int) -> Void { | ||
exported?.add(value: value, attribute: successAttrs) | ||
} | ||
|
||
public func addFailed(value: Int) -> Void { | ||
exported?.add(value: value, attribute: failedAttrs) | ||
} | ||
|
||
// MARK: - Private functions | ||
|
||
/*** | ||
* Create an instance for recording exporter metrics under the meter | ||
* "io.opentelemetry.exporters." + exporterName + "-transporterType". | ||
**/ | ||
private var meter: StableMeter { | ||
meterProvider.get(name: "io.opentelemetry.exporters.\(exporterName)-\(transportName)") | ||
} | ||
|
||
// MARK: - Static function | ||
|
||
public static func makeExporterMetric( | ||
type: String, | ||
meterProvider: StableMeterProvider, | ||
exporterName: String, | ||
transportName: TransporterType | ||
) -> ExporterMetrics { | ||
ExporterMetrics( | ||
type: type, | ||
meterProvider: meterProvider, | ||
exporterName: exporterName, | ||
transportName: transportName | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.