|
| 1 | +// |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | + |
| 6 | + |
| 7 | +import Foundation |
| 8 | +import OpenTelemetryApi |
| 9 | +import OpenTelemetrySdk |
| 10 | +import OpenTelemetryProtocolExporter |
| 11 | +import GRPC |
| 12 | +import NIO |
| 13 | +import NIOHPACK |
| 14 | + |
| 15 | +/* |
| 16 | + Stable metrics is the working name for the otel-swift implementation of the current OpenTelemetry metrics specification. |
| 17 | + The existing otel-swift metric implementation is old and out-of-spec. While Stable Metrics is in an experimental phase it will maintaion |
| 18 | + the "stable" prefix, and can be expected to be present on overlapping constructs in the implementation. |
| 19 | + Expected time line will be as follows: |
| 20 | + Phase 1: |
| 21 | + Provide access to Stable Metrics along side existing Metrics. Once Stable Metrics are considered stable we will move onto phase 2. |
| 22 | + Phase 2: |
| 23 | + Mark all existing Metric APIs as deprecated. This will maintained for a period TBD |
| 24 | + Phase 3: |
| 25 | + Remove deprecated metrics api and remove Stable prefix from Stable metrics. |
| 26 | + |
| 27 | + |
| 28 | + Below is an example used the Stable Metrics API |
| 29 | + |
| 30 | + */ |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +/* |
| 36 | + Basic configuration for metrics |
| 37 | + */ |
| 38 | +func basicConfiguration() { |
| 39 | + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 40 | + let exporterChannel = ClientConnection.insecure(group: group) |
| 41 | + .connect(host: "localhost", port: 8200) |
| 42 | + |
| 43 | + |
| 44 | + // register view will process all instruments using `.*` regex |
| 45 | + |
| 46 | + OpenTelemetry.registerStableMeterProvider(meterProvider: StableMeterProviderBuilder() |
| 47 | + .registerView(selector: InstrumentSelector.builder().setInstrument(name: ".*").build(), view: StableView.builder().build()) |
| 48 | + .registerMetricReader(reader:StablePeriodicMetricReaderBuilder(exporter: StableOtlpMetricExporter(channel: exporterChannel)).build()) |
| 49 | + .build() |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +func complexViewConfiguration() { |
| 55 | + let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 56 | + let exporterChannel = ClientConnection.insecure(group: group) |
| 57 | + .connect(host: "localhost", port: 8200) |
| 58 | + |
| 59 | + // The example registers a View that re-configures the Gauge instrument into a sum instrument named "GaugeSum" |
| 60 | + |
| 61 | + OpenTelemetry.registerStableMeterProvider(meterProvider: StableMeterProviderBuilder() |
| 62 | + .registerView(selector: InstrumentSelector.builder().setInstrument(name: "Gauge").build(), view: StableView.builder().withName(name: "GaugeSum").withAggregation(aggregation: Aggregations.sum()).build()) |
| 63 | + .registerMetricReader(reader:StablePeriodicMetricReaderBuilder(exporter: StableOtlpMetricExporter(channel: exporterChannel)).build()) |
| 64 | + .build() |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +basicConfiguration() |
| 69 | + |
| 70 | +// creating a new meter & instrument |
| 71 | +let meter = OpenTelemetry.instance.stableMeterProvider?.meterBuilder(name: "MyMeter").build() |
| 72 | +var gaugeBuilder = meter!.gaugeBuilder(name: "Gauge") |
| 73 | +var gauge = gaugeBuilder.buildWithCallback({ ObservableDoubleMeasurement in |
| 74 | + ObservableDoubleMeasurement.record(value: 1.0, attributes: ["test": AttributeValue.bool(true)]) |
| 75 | +}) |
0 commit comments