Skip to content
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

adds b3 as an option for propagation for trace plug #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
34 changes: 28 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 All @@ -69,6 +73,7 @@ defmodule Opencensus.Plug.Trace do

defmacro __using__(opts) do
attributes = Keyword.get(opts, :attributes, [])
propagation_format = Keyword.get(opts, :propagation_format, :tracecontext)

quote do
@behaviour Plug
Expand All @@ -77,7 +82,12 @@ 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)
parent_span_ctx =
case unquote(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 +115,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, unquote(propagation_format))
|> Plug.Conn.register_before_send(fn conn ->
{status, msg} = span_status(conn, opts)

Expand Down Expand Up @@ -139,7 +149,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 +162,22 @@ 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
cond do
is_list(v) -> {String.downcase(k), List.to_string(v)}
true -> {String.downcase(k), v}
end
end

Plug.Conn.prepend_resp_headers(conn, headers)
Expand Down
20 changes: 18 additions & 2 deletions test/opencensus/plug/trace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule Opencensus.Plug.TraceTest do
use Subject
end

defmodule SamplePlugB3 do
use Subject, propagation_format: :b3
end

setup do: [conn: conn(:get, "/")]

test "sets 'traceparent' response header", %{conn: conn} do
Expand All @@ -27,6 +31,12 @@ defmodule Opencensus.Plug.TraceTest do
assert [_] = get_resp_header(conn, "traceparent")
end

test "sets 'x-b3-traceid' response header", %{conn: conn} do
conn = SamplePlugB3.call(conn, [])

assert [_] = get_resp_header(conn, "x-b3-traceid")
end

test "span longs as long as request", %{conn: conn} do
conn = SamplePlug.call(conn, [])
refute :undefined == :ocp.current_span_ctx()
Expand Down Expand Up @@ -75,10 +85,16 @@ defmodule Opencensus.Plug.TraceTest do
[conn: conn(:get, "/"), ctx: :ocp.current_span_ctx()]
end

test "sets response header", %{conn: conn, ctx: ctx} do
conn = Subject.put_ctx_resp_header(conn, ctx)
test "sets response header in trace context propagation format", %{conn: conn, ctx: ctx} do
conn = Subject.put_ctx_resp_header(conn, ctx, :tracecontext)

assert [_] = get_resp_header(conn, "traceparent")
end

test "sets response header", %{conn: conn, ctx: ctx} do
conn = Subject.put_ctx_resp_header(conn, ctx, :b3)

assert [_] = get_resp_header(conn, "x-b3-traceid")
end
end
end