open-telemetry / opentelemetry-swift

OpenTelemetry API for Swift
https://opentelemetry.io/docs/instrumentation/swift/
Apache License 2.0
222 stars 145 forks source link

Wrong `InstrumentType` cause failure of creating CounterObservable(s) #471

Closed hunguyenaxon closed 1 year ago

hunguyenaxon commented 1 year ago

Issue happens on DoubleCounterMeterBuilderSdk, DoubleUpDownCounterBuilderSdk, LongCounterMeterBuilderSdk and LongUpDownCounterBuilderSdk.

The buildWithCallback(..) need to provide the correct InstrumentType of asynchronous counter, instead of synchronous counter. Otherwise the registerObservableMeasurement(...) will crash with forced cast.

Screenshot 2023-10-09 at 9 52 45 PM
public class DoubleCounterMeterBuilderSdk : DoubleCounterBuilder, InstrumentBuilder {
    let type: InstrumentType = .counter    
    let valueType: InstrumentValueType = .double
    ...
     public func buildWithCallback(_ callback: @escaping (OpenTelemetryApi.ObservableDoubleMeasurement) -> Void) -> OpenTelemetryApi.ObservableDoubleCounter {
        registerDoubleAsynchronousInstrument(type: .counter, updater: callback) // <~~~ supposed to be .observableCounter
    }
}