Skip to content

support multiple instrument callback #113

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

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
23 changes: 22 additions & 1 deletion src/api/src/metric/instruments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export Measurement,
Histogram,
ObservableGauge,
UpDownCounter,
ObservableUpDownCounter
ObservableUpDownCounter,
ObservableMultiInstrument

"""
Measurement(value, [attributes=BoundedAttributes()])
Expand Down Expand Up @@ -225,3 +226,23 @@ struct ObservableUpDownCounter{T,F} <: AbstractAsyncInstrument{T}
c
end
end


"""
ObservableMultiInstrument(callback, instruments)

The number of measurements returned by the `callback` should be the same as the number of `instruments`.
See also [#112](https://github.com/oolong-dev/OpenTelemetry.jl/issues/112)
"""
struct ObservableMultiInstrument <: AbstractAsyncInstrument{Any}
callback::Any
instruments::AbstractVector{<:AbstractSyncInstrument}
end

function (ins::ObservableMultiInstrument)()
measurements = ins.callback()
@assert length(measurements) == length(ins.instruments) "length mismatch"
for (x, m) in zip(ins.instruments, measurements)
x(m)
end
end
1 change: 1 addition & 0 deletions src/api/src/trace/tracer_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Base.getindex(s::TraceState, key) = s.kv[key]
Base.haskey(s::TraceState, key) = haskey(s.kv, key)
Base.haskey(s::TraceState, key::String) = haskey(s, Symbol(key))
Base.length(s::TraceState) = length(s.kv)
Base.isempty(s::TraceState) = isempty(s.kv)

function Base.show(io::IO, ts::TraceState)
for (i, (k, v)) in enumerate(pairs(ts.kv))
Expand Down
5 changes: 5 additions & 0 deletions src/api/test/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@
rand(1:10)
end
obs_up_down_counter()

obs_multi_ins = ObservableMultiInstrument([counter, up_down_counter]) do
(rand(1:10), rand(-10:10))
end
obs_multi_ins()
end
end
2 changes: 1 addition & 1 deletion src/api/test/trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@test isempty(parse(TraceState, ""))
@test !isempty(parse(TraceState, "a=b,c=d"))
@test !isempty(parse(TraceState, "a=b,ss=dwdw=dwdwd,c=d"))
@test length(parse(TraceState, "a=b,ss=dwdw=dwdwd,c=d")) == 2
end
end

Expand Down
Loading