Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize access race in HistogramMetric #609

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Sources/OpenTelemetrySdk/Metrics/HistogramMetricSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ internal class HistogramMetricSdk<T: SignedNumeric & Comparable>: HistogramMetri
public private(set) var boundInstruments = [LabelSet: BoundHistogramMetricSdkBase<T>]()
let metricName: String
let explicitBoundaries: Array<T>?
let bindUnbindLock = Lock()

init(name: String, explicitBoundaries: Array<T>? = nil) {
metricName = name
self.explicitBoundaries = explicitBoundaries
}

func bind(labelset: LabelSet) -> BoundHistogramMetric<T> {
var boundInstrument = boundInstruments[labelset]
if boundInstrument == nil {
boundInstrument = createMetric()
boundInstruments[labelset] = boundInstrument!
bindUnbindLock.withLock {
var boundInstrument = boundInstruments[labelset]
if boundInstrument == nil {
boundInstrument = createMetric()
boundInstruments[labelset] = boundInstrument!
}

return boundInstrument!
}
return boundInstrument!
}

func bind(labels: [String: String]) -> BoundHistogramMetric<T> {
Expand Down
18 changes: 11 additions & 7 deletions Sources/OpenTelemetrySdk/Metrics/MeterSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,18 @@ class MeterSdk: Meter {
let metricName = histogram.key
let measureInstrument = histogram.value
var metric = Metric(namespace: meterName, name: metricName, desc: meterName + metricName, type: AggregationType.doubleHistogram, resource: resource, instrumentationScopeInfo: instrumentationScopeInfo)
measureInstrument.boundInstruments.forEach { boundInstrument in
let labelSet = boundInstrument.key
let aggregator = boundInstrument.value.getAggregator()
aggregator.checkpoint()
var metricData = aggregator.toMetricData()
metricData.labels = labelSet.labels
metric.data.append(metricData)

measureInstrument.bindUnbindLock.withLock {
measureInstrument.boundInstruments.forEach { boundInstrument in
let labelSet = boundInstrument.key
let aggregator = boundInstrument.value.getAggregator()
aggregator.checkpoint()
var metricData = aggregator.toMetricData()
metricData.labels = labelSet.labels
metric.data.append(metricData)
}
}

metricProcessor.process(metric: metric)
}

Expand Down
Loading