Skip to content

Commit d638ad2

Browse files
committed
Added MetricSdkProcessor
1 parent 149e449 commit d638ad2

File tree

5 files changed

+63
-16
lines changed

5 files changed

+63
-16
lines changed

Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ struct MetricsAdapter {
127127
var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntHistogramDataPoint()
128128
protoDataPoint.sum = Int64(summaryData.sum)
129129
protoDataPoint.count = UInt64(summaryData.count)
130-
// protoDataPoint.explicitBounds = [summaryData.min, summaryData.max]
131-
130+
protoDataPoint.bucketCounts = [UInt64(summaryData.min), UInt64(summaryData.max)]
132131
protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds
133132
protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds
134133

Sources/OpenTelemetrySdk/Metrics/Configuration/MeterSdkProvider.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public class MeterSdkProvider: MeterProvider {
3535
metricExporter: MetricExporter,
3636
metricPushInterval: TimeInterval = MeterSdkProvider.defaultPushInterval,
3737
resource: Resource = EnvVarResource.resource) {
38-
self.meterSharedState = MeterSharedState(metricProcessor: metricProcessor,
39-
metricExporter: metricExporter,
40-
metricPushInterval: metricPushInterval,
41-
resource: resource)
38+
self.meterSharedState = MeterSharedState(metricProcessor:metricProcessor, metricPushInterval: metricPushInterval, resource: resource)
4239

4340
defaultMeter = MeterSdk(meterSharedState: self.meterSharedState, instrumentationLibraryInfo: InstrumentationLibraryInfo())
4441

Sources/OpenTelemetrySdk/Metrics/Configuration/MeterSharedState.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515

1616
import Foundation
1717

18-
public struct MeterSharedState {
18+
public class MeterSharedState {
19+
1920
/// Configures metric processor. (aka batcher).
2021
public private(set) var metricProcessor: MetricProcessor
21-
/// Configures Metric Exporter.
22-
public private(set) var metricExporter: MetricExporter
2322
/// Sets the push interval.
2423
public private(set) var metricPushInterval: TimeInterval
2524
public private(set) var resource: Resource
2625

27-
public init(metricProcessor: MetricProcessor,
28-
metricExporter: MetricExporter,
29-
metricPushInterval: TimeInterval ,
30-
resource: Resource) {
26+
27+
public init(metricProcessor: MetricProcessor, metricPushInterval: TimeInterval, resource: Resource) {
3128
self.metricProcessor = metricProcessor
32-
self.metricExporter = metricExporter
3329
self.metricPushInterval = metricPushInterval
34-
self.resource = resource
30+
self.resource = resource
3531
}
32+
3633
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
17+
import Foundation
18+
19+
public class MetricSdkProcessor : MetricProcessor {
20+
private let lock : Lock
21+
var metrics : [Metric]
22+
23+
public init() {
24+
metrics = [Metric]()
25+
lock = Lock()
26+
}
27+
28+
/// Finish the current collection cycle and return the metrics it holds.
29+
/// This is called at the end of one collection cycle by the Controller.
30+
/// MetricProcessor can use this to clear its Metrics (in case of stateless).
31+
/// - Returns: The list of metrics from this cycle, which are to be exported.
32+
public func finishCollectionCycle() -> [Metric] {
33+
lock.lock()
34+
defer {
35+
lock.unlock()
36+
}
37+
return metrics
38+
}
39+
40+
/// Process the metric. This method is called once every collection interval.
41+
/// - Parameters:
42+
/// - metric: the metric record.
43+
public func process(metric: Metric) {
44+
lock.lock()
45+
defer {
46+
lock.unlock()
47+
}
48+
49+
metrics.append(metric)
50+
51+
}
52+
53+
54+
}

Tests/OpenTelemetrySdkTests/Metrics/PushControllerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class PushControllerTests: XCTestCase {
3838
// Setup 2 meters whose Collect will increment the collect count.
3939
var meter1CollectCount = 0
4040
var meter2CollectCount = 0
41-
let meterSharedState = MeterSharedState(metricProcessor: testProcessor, metricExporter: NoopMetricExporter(), metricPushInterval: MeterSdkProvider.defaultPushInterval, resource: Resource())
41+
let meterSharedState = MeterSharedState(metricProcessor: testProcessor, metricPushInterval: MeterSdkProvider.defaultPushInterval, resource: Resource())
4242

4343
let meterInstrumentationLibrary1 = InstrumentationLibraryInfo(name:"meter1")
4444

0 commit comments

Comments
 (0)