Skip to content

Commit 591960f

Browse files
authored
support multiple instrument callback (#113)
* support multiple instrument callback * add extra assert
1 parent a81b124 commit 591960f

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/api/src/metric/instruments.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export Measurement,
44
Histogram,
55
ObservableGauge,
66
UpDownCounter,
7-
ObservableUpDownCounter
7+
ObservableUpDownCounter,
8+
ObservableMultiInstrument
89

910
"""
1011
Measurement(value, [attributes=BoundedAttributes()])
@@ -225,3 +226,23 @@ struct ObservableUpDownCounter{T,F} <: AbstractAsyncInstrument{T}
225226
c
226227
end
227228
end
229+
230+
231+
"""
232+
ObservableMultiInstrument(callback, instruments)
233+
234+
The number of measurements returned by the `callback` should be the same as the number of `instruments`.
235+
See also [#112](https://github.com/oolong-dev/OpenTelemetry.jl/issues/112)
236+
"""
237+
struct ObservableMultiInstrument <: AbstractAsyncInstrument{Any}
238+
callback::Any
239+
instruments::AbstractVector{<:AbstractSyncInstrument}
240+
end
241+
242+
function (ins::ObservableMultiInstrument)()
243+
measurements = ins.callback()
244+
@assert length(measurements) == length(ins.instruments) "length mismatch"
245+
for (x, m) in zip(ins.instruments, measurements)
246+
x(m)
247+
end
248+
end

src/api/src/trace/tracer_basic.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Base.getindex(s::TraceState, key) = s.kv[key]
9494
Base.haskey(s::TraceState, key) = haskey(s.kv, key)
9595
Base.haskey(s::TraceState, key::String) = haskey(s, Symbol(key))
9696
Base.length(s::TraceState) = length(s.kv)
97+
Base.isempty(s::TraceState) = isempty(s.kv)
9798

9899
function Base.show(io::IO, ts::TraceState)
99100
for (i, (k, v)) in enumerate(pairs(ts.kv))

src/api/test/metric.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@
4545
rand(1:10)
4646
end
4747
obs_up_down_counter()
48+
49+
obs_multi_ins = ObservableMultiInstrument([counter, up_down_counter]) do
50+
(rand(1:10), rand(-10:10))
51+
end
52+
obs_multi_ins()
4853
end
4954
end

src/api/test/trace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@test isempty(parse(TraceState, ""))
2323
@test !isempty(parse(TraceState, "a=b,c=d"))
24-
@test !isempty(parse(TraceState, "a=b,ss=dwdw=dwdwd,c=d"))
24+
@test length(parse(TraceState, "a=b,ss=dwdw=dwdwd,c=d")) == 2
2525
end
2626
end
2727

0 commit comments

Comments
 (0)