Skip to content

Commit

Permalink
feat: add extract_from and extract_span_context (#109)
Browse files Browse the repository at this point in the history
* add extract_from and extract_span_context

* add generate_w3c*

* up
  • Loading branch information
krynju authored Apr 24, 2024
1 parent 8e9ecc2 commit 67c83d2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/api/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OpenTelemetryAPI"
uuid = "4f63ef3d-5940-44e7-a611-2d97696cffc9"
authors = ["Jun Tian <[email protected]>"]
version = "0.5.1"
version = "0.5.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
4 changes: 0 additions & 4 deletions src/api/ext/OpenTelemetryAPIHTTPExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,4 @@ function OpenTelemetryAPI.uninstrument!(::Val{:HTTP})
)
end

function __init__()
println("$(@__MODULE__) is loaded!")
end

end
104 changes: 63 additions & 41 deletions src/api/src/propagator/trace_context_textmap_propagator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ This propagator follows the [W3C format](https://www.w3.org/TR/trace-context/#tr
"""
struct TraceContextTextMapPropagator <: AbstractPropagator end

function generate_w3c_traceparent(trace_id, span_id, trace_flag_sampled)
return "00-$(string(trace_id, base=16, pad=32))-$(string(span_id, base=16,pad=16))-$(trace_flag_sampled ? "01" : "00")"
end

function generate_w3c_traceparent(sc::SpanContext)
return generate_w3c_traceparent(sc.trace_id, sc.span_id, sc.trace_flag.sampled)
end

function generate_w3c_context(sc::SpanContext)
return generate_w3c_traceparent(sc), string(sc.trace_state)
end

function inject_context!(
carrier::Union{
AbstractVector{<:Pair{<:AbstractString,<:AbstractString}},
AbstractDict{<:AbstractString,<:AbstractString},
},
propagator::TraceContextTextMapPropagator,
ctx::Context = current_context(),
ctx::Context = current_context()
)
sc = span_context(ctx)
if !isnothing(sc)
s_trace_parent = "00-$(string(sc.trace_id, base=16, pad=32))-$(string(sc.span_id, base=16,pad=16))-$(sc.trace_flag.sampled ? "01" : "00")"
s_trace_parent, s_trace_state = generate_w3c_context(sc)
push!(carrier, "traceparent" => s_trace_parent)
s_trace_state = string(sc.trace_state)
if !isempty(s_trace_state)
push!(carrier, "tracestate" => s_trace_state)
end
Expand All @@ -31,7 +42,7 @@ end
function inject_context!(
carrier::T,
::TraceContextTextMapPropagator,
ctx::Context = current_context(),
ctx::Context = current_context()
) where {T}
@warn "unknown carrier type $T"
carrier
Expand All @@ -43,10 +54,53 @@ TRACEPARENT_HEADER_FORMAT =
r"^[ \t]*(?P<version>[0-9a-f]{2})-(?P<trace_id>[0-9a-f]{32})-(?P<span_id>[0-9a-f]{16})-(?P<trace_flag>[0-9a-f]{2})(?P<rest>-.*)?[ \t]*$"

function extract_context(
carrier::AbstractDict{<:AbstractString,<:AbstractString},
carrier::Union{
AbstractDict{<:AbstractString,<:AbstractString},
AbstractVector{<:Pair{<:AbstractString,<:AbstractString}}
},
propagator::TraceContextTextMapPropagator,
ctx::Context = current_context(),
)
trace_id, span_id, trace_flag, trace_state = extract_from(carrier)
return if (
trace_id === nothing ||
span_id === nothing ||
trace_flag === nothing
)
ctx
else
merge(
ctx,
Context(Dict(
SPAN_KEY_IN_CONTEXT => NonRecordingSpan(
"",
SpanContext(span_id, trace_id, false, trace_flag, trace_state),
nothing,
)
)),
)
end
end

function extract_span_context(
carrier::Union{
AbstractDict{<:AbstractString,<:AbstractString},
AbstractVector{<:Pair{<:AbstractString,<:AbstractString}}
}
)
trace_id, span_id, trace_flag, trace_state = extract_from(carrier)
return if (
trace_id === nothing ||
span_id === nothing ||
trace_flag === nothing
)
nothing
else
SpanContext(span_id, trace_id, false, trace_flag, trace_state)
end
end

function extract_from(carrier::AbstractDict{<:AbstractString,<:AbstractString})
trace_id = nothing
span_id = nothing
trace_flag = nothing
Expand All @@ -70,15 +124,10 @@ function extract_context(
end
end
end

return _extract_context(trace_id, span_id, trace_flag, trace_state, propagator, ctx)
return trace_id, span_id, trace_flag, trace_state
end

function extract_context(
carrier::AbstractVector{<:Pair{<:AbstractString,<:AbstractString}},
propagator::TraceContextTextMapPropagator,
ctx::Context = current_context(),
)
function extract_from(carrier::AbstractVector{<:Pair{<:AbstractString,<:AbstractString}})
trace_id = nothing
span_id = nothing
trace_flag = nothing
Expand All @@ -100,37 +149,9 @@ function extract_context(
end
end
end

return _extract_context(trace_id, span_id, trace_flag, trace_state, propagator, ctx)
return trace_id, span_id, trace_flag, trace_state
end

function _extract_context(
trace_id,
span_id,
trace_flag,
trace_state,
propagator::TraceContextTextMapPropagator,
ctx::Context = current_context(),
)
return if (
trace_id === nothing ||
span_id === nothing ||
trace_flag === nothing
)
ctx
else
merge(
ctx,
Context(Dict(
SPAN_KEY_IN_CONTEXT => NonRecordingSpan(
"",
SpanContext(span_id, trace_id, false, trace_flag, trace_state),
nothing,
)
)),
)
end
end

# fallback
function extract_context(
Expand All @@ -141,3 +162,4 @@ function extract_context(
@warn "unknown carrier type $T"
ctx
end

2 comments on commit 67c83d2

@krynju
Copy link
Collaborator Author

@krynju krynju commented on 67c83d2 Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir=src/api

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105539

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a api-v0.5.2 -m "<description of version>" 67c83d2795928b9324ed8162728bc422a8615af9
git push origin api-v0.5.2

Please sign in to comment.