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

Allow customizing the span name #4

Open
wants to merge 3 commits 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
14 changes: 12 additions & 2 deletions lib/opencensus_tesla.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ defmodule OpencensusTesla.Middleware do
plug OpencensusTesla.Middleware
end
```

### Options

Following Middleware options are accepted:
- `span_name_override` - if provided, will be used as the
span name. Otherwise request path is used by default.
A function taking the request path as argument and
returning a string to use as span name is expected.
"""

import Opencensus.Trace

def call(env, next, _options) do
def call(env, next, options) do
uri = %URI{path: path} = URI.parse(env.url)
span_name_function = Keyword.get(options, :span_name_override)
span_name = if span_name_function, do: span_name_function.(path), else: path

with_child_span(path, http_attributes(env, uri)) do
with_child_span(span_name, http_attributes(env, uri)) do
span_ctx = :ocp.current_span_ctx()
headers = :oc_propagation_http_tracecontext.to_headers(span_ctx)

Expand Down
32 changes: 30 additions & 2 deletions test/opencensus_tesla_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,48 @@ defmodule TestRequestClient do
end)
end

defmodule TestRequestClientCustomized do
use Tesla

plug(OpencensusTesla.Middleware, span_name_override: fn path -> path <> "test" end)

adapter(fn env ->
{status, headers, body} =
case env.url do
_ ->
{200, [], ""}
end

{:ok, %{env | status: status, headers: headers, body: body}}
end)
end

defmodule OpencensusTeslaTest do
use ExUnit.Case

test "span is reported after request is complete" do
setup do
Application.load(:opencensus)
Application.put_env(:opencensus, :send_interval_ms, 1)
Application.put_env(:opencensus, :reporters, [{:oc_reporter_pid, self()}])

{:ok, _} = Application.ensure_all_started(:opencensus)
{:ok, _} = Application.ensure_all_started(:opencensus_elixir)

on_exit(fn -> Application.stop(:opencensus) end)
:ok
end

test "span is reported after request is complete" do
{:ok, env} = TestRequestClient.post("/", "")
assert 200 = env.status

assert_receive {:span, _}, 1_000
assert_receive {:span, {:span, "/", _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}}, 1_000
end

test "can override span name" do
{:ok, env} = TestRequestClientCustomized.post("/", "")
assert 200 = env.status

assert_receive {:span, {:span, "/test", _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}}, 1_000
end
end