Skip to content

Commit

Permalink
Support new options on opentelemetry_tesla
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Cribbs committed Nov 26, 2023
1 parent 9a10dcd commit e28d486
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,22 @@ end
)
```

`Hardhat` wraps each request in an [OpenTelemetry](`Tesla.Middleware.OpenTelemetry`) span and propagates the trace context to the destination host. It does not currently expose the ability to change the span name in the trace, but it will observe any [path parameters](`Hardhat.Middleware.PathParams`) you interpolate into the URL so that similar spans can be easily aggregated.
`Hardhat` wraps each request in an [OpenTelemetry](`Tesla.Middleware.OpenTelemetry`) span and propagates the trace context to the destination host. It will observe any [path parameters](`Hardhat.Middleware.PathParams`) you interpolate into the URL so that similar spans can be easily aggregated. To disable propagation or change other behavior of the tracing, override the `opentelemetry_opts/0` function:

```elixir
defmodule NoPropagationClient do
use Hardhat

def opentelemety_opts do
[
# Disable trace propagation for talking to a third-party API
propagator: :none
]
end
end
```

For more information about the options available to tracing, see `Hardhat.Defaults.opentelemetry_opts/0` or `Tesla.Middleware.OpenTelemetry`.

## Failure detection

Expand Down
18 changes: 16 additions & 2 deletions lib/hardhat/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ defmodule Hardhat.Builder do
[]
end

@doc false
def opentelemetry_opts() do
[]
end

@doc false
def install_regulator() do
Regulator.install(
Expand All @@ -103,7 +108,8 @@ defmodule Hardhat.Builder do
retry_opts: 0,
should_retry: 1,
regulator_opts: 0,
should_regulate: 1
should_regulate: 1,
opentelemetry_opts: 0
end
end

Expand Down Expand Up @@ -155,7 +161,15 @@ defmodule Hardhat.Builder do

unquote(circuit_breaker)
plug(Tesla.Middleware.Telemetry)
plug(Tesla.Middleware.OpenTelemetry)

plug(
Tesla.Middleware.OpenTelemetry,
Keyword.merge(
Hardhat.Defaults.opentelemetry_opts(),
__MODULE__.opentelemetry_opts()
)
)

plug(Hardhat.Middleware.PathParams)
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/hardhat/defaults.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,21 @@ defmodule Hardhat.Defaults do
def should_regulate({:ok, %Tesla.Env{} = env}) do
env.status >= 500 || env.status == 429
end

@doc """
Default options for the `Tesla.Middleware.OpenTelemetry` middleware.
The options include:
* `:span_name` - override span name. Can be a `String` for a static span name,
or a function that takes the `Tesla.Env` and returns a `String`. The
default span name is chosen by the middleware.
* `:propagator` - configures trace headers propagators. Setting it to `:none`
disables propagation. Any module that implements `:otel_propagator_text_map`
can be used. Defaults to calling `:opentelemetry.get_text_map_injector/0`
* `:mark_status_ok` - configures spans with a list of expected HTTP error codes to be
marked as ok, not as an error-containing spans. The default is empty.
"""
def opentelemetry_opts do
[]
end
end
23 changes: 23 additions & 0 deletions test/middleware/opentelemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ defmodule Hardhat.OpentelemetryTest do
use Hardhat
end

defmodule NoPropagationClient do
use Hardhat

def opentelemetry_opts do
[propagator: :none]
end
end

for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
Record.defrecord(name, spec)
end
Expand All @@ -17,6 +25,7 @@ defmodule Hardhat.OpentelemetryTest do
setup do
bypass = Bypass.open()
pool = start_supervised!(TestClient)
start_supervised!(NoPropagationClient)
:application.stop(:opentelemetry)
:application.set_env(:opentelemetry, :tracer, :otel_tracer_default)
:application.set_env(:opentelemetry, :traces_exporter, {:otel_exporter_pid, self()})
Expand Down Expand Up @@ -54,4 +63,18 @@ defmodule Hardhat.OpentelemetryTest do

assert_receive {:span, span(name: "/user/:id", attributes: _)}, 1000
end

test "does not propagate trace when disabled", %{bypass: bypass} do
parent = self()

Bypass.expect_once(bypass, fn conn ->
send(parent, {:traceheader, Plug.Conn.get_req_header(conn, "traceparent")})
Plug.Conn.resp(conn, 200, "Hello, world")
end)

assert {:ok, _conn} =
NoPropagationClient.get("http://localhost:#{bypass.port}/")

assert_receive {:traceheader, []}, 1000
end
end

0 comments on commit e28d486

Please sign in to comment.