Skip to content

Commit

Permalink
adds b3 as an option for propagation for trace plug
Browse files Browse the repository at this point in the history
  • Loading branch information
castengo committed Aug 3, 2020
1 parent 1814acf commit fb699f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ create new span. By default span name will match request path, you can configure
that by defining `span_name/2` method that will receive `Plug.Conn.t` as a first
argument and plug options as a second.

If using [B3](https://github.com/openzipkin/b3-propagation), add `propagation_format`
argument to your plug:

```elixir
defmodule MyApp.TracePlug do
use Opencensus.Plug.Trace, propagation_format: :b3
end
```

### Metrics

Create metrics module:
Expand Down
32 changes: 26 additions & 6 deletions lib/opencensus/plug/trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ defmodule Opencensus.Plug.Trace do
- `{module, function, args}` - which will prepend `conn` to the given arguments
and call `apply(module, function, [conn | args])`
Finally, you can configure what propagation format to use for tracing by setting
a `propagation_format` argument in `use` that specifies if you are using `:b3` or
`:tracecontext`. If none is given it defaults to `tracecontext`.
Example:
```elixir
defmodule MyAppWeb.TraceWithCustomAttribute do
use Opencensus.Plug.Trace, attributes: [:method]
use Opencensus.Plug.Trace, attributes: [:method], propagation_format: :b3
def method(conn), do: conn.method
end
Expand Down Expand Up @@ -77,7 +81,14 @@ defmodule Opencensus.Plug.Trace do
def init(opts), do: opts

def call(conn, opts) do
parent_span_ctx = :oc_propagation_http_tracecontext.from_headers(conn.req_headers)
propagation_format = Keyword.get(opts, :propagation_format, :tracecontext)

parent_span_ctx =
case propagation_format do
:tracecontext -> :oc_propagation_http_tracecontext.from_headers(conn.req_headers)
:b3 -> :oc_propagation_http_b3.from_headers(conn.req_headers)
end

:ocp.with_span_ctx(parent_span_ctx)

user_agent =
Expand Down Expand Up @@ -105,7 +116,7 @@ defmodule Opencensus.Plug.Trace do

conn
|> Plug.Conn.put_private(:opencensus_span_ctx, span_ctx)
|> unquote(__MODULE__).put_ctx_resp_header(span_ctx)
|> unquote(__MODULE__).put_ctx_resp_header(span_ctx, propagation_format)
|> Plug.Conn.register_before_send(fn conn ->
{status, msg} = span_status(conn, opts)

Expand Down Expand Up @@ -139,7 +150,7 @@ defmodule Opencensus.Plug.Trace do

@doc false
def set_logger_metadata(span) do
trace_id = List.to_string(:io_lib.format("~32.16.0b", [ctx(span, :trace_id)]))
trace_id = List.to_string(:io_lib.format("~.16b", [ctx(span, :trace_id)]))
span_id = List.to_string(:io_lib.format("~16.16.0b", [ctx(span, :span_id)]))

Logger.metadata(
Expand All @@ -152,10 +163,19 @@ defmodule Opencensus.Plug.Trace do
end

@doc false
def put_ctx_resp_header(conn, span_ctx) do
def put_ctx_resp_header(conn, span_ctx, :tracecontext) do
headers =
for {k, v} <- :oc_propagation_http_tracecontext.to_headers(span_ctx) do
{k, List.to_string(v)}
{String.downcase(k), List.to_string(v)}
end

Plug.Conn.prepend_resp_headers(conn, headers)
end

def put_ctx_resp_header(conn, span_ctx, :b3) do
headers =
for {k, v} <- :oc_propagation_http_b3.to_headers(span_ctx) do
{String.downcase(k), List.to_string(v)}
end

Plug.Conn.prepend_resp_headers(conn, headers)
Expand Down

0 comments on commit fb699f1

Please sign in to comment.