Skip to content

Commit

Permalink
Don't set Logger metadata (#69)
Browse files Browse the repository at this point in the history
* Don't set Logger metadata

The OpenTelemetry API/SDK does that consistently for us since 1.1.0.

* update CHANGELOG

* update CHANGELOG

Co-authored-by: Cristiano Piemontese <[email protected]>
  • Loading branch information
LeartS and cpiemontese authored Sep 21, 2022
1 parent 3245cec commit 93fe9a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.1.0] - 2022-09-21

### Changed

* Teleplug does not set opentelemetry-related Logger metadata anymore, because
The OpenTelemetry API/SDK itself [does that automatically since 1.1.0](https://github.com/open-telemetry/opentelemetry-erlang/pull/394).
If you're upgrading to Teleplug 1.1.0, it is therefore recommended to also upgrade to OpenTelemetry API 1.1.0

[Unreleased]: https://github.com/primait/teleplug/compare/1.1.0...HEAD
[1.1.0]: https://github.com/primait/teleplug/releases/tag/1.1.0
27 changes: 0 additions & 27 deletions lib/teleplug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ defmodule Teleplug do

Tracer.set_current_span(new_ctx)

set_logger_metadata(new_ctx)

Conn.register_before_send(conn, fn conn ->
Tracer.set_attribute("http.status_code", conn.status)
Tracer.end_span()

Tracer.set_current_span(parent_ctx)
set_logger_metadata(parent_ctx)
conn
end)
end
Expand Down Expand Up @@ -116,28 +113,4 @@ defmodule Teleplug do
client
end
end

defp set_logger_metadata(:undefined), do: Logger.metadata(trace_id: nil, span_id: nil, dd: nil)

defp set_logger_metadata(span_ctx) do
trace_id = :otel_span.trace_id(span_ctx)
span_id = :otel_span.span_id(span_ctx)

Logger.metadata(
trace_id: trace_id,
span_id: span_id,
dd: [
trace_id: datadog_trace_id(trace_id),
span_id: datadog_span_id(span_id)
]
)
end

# converts trace_id to a datadog understandable format (taking the 2nd half of the 128 bits string)
defp datadog_trace_id(trace_id) do
<<_::64, datadog_trace_id::64>> = <<trace_id::128>>
to_string(datadog_trace_id)
end

defp datadog_span_id(span_id), do: to_string(span_id)
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Teleplug.MixProject do
use Mix.Project

@source_url "https://github.com/primait/teleplug"
@version "1.0.1"
@version "1.1.0"

def project do
[
Expand All @@ -25,7 +25,7 @@ defmodule Teleplug.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:opentelemetry_api, "~> 1.0"},
{:opentelemetry_api, "~> 1.1"},
{:plug, "~> 1.11"},
{:telemetry, "~> 0.4 or ~> 1.0.0"}
] ++ dev_deps()
Expand Down Expand Up @@ -62,6 +62,7 @@ defmodule Teleplug.MixProject do
defp docs do
[
extras: [
"CHANGELOG.md": [title: "Changelog"],
"LICENSE.md": [title: "License"],
"README.md": [title: "Overview"]
],
Expand Down
26 changes: 12 additions & 14 deletions test/teleplug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,19 @@ defmodule TeleplugTest do
end)
end

assert_receive {:span, span(attributes: attributes)}, 1_000
assert_receive {:span, span(attributes: attributes_record)}, 1_000
assert {:attributes, _, _, _, attributes} = attributes_record

assert Enum.all?(
[
{"http.status_code", nil},
{"http.method", "GET"},
{"http.route", "/"},
{"http.target", "/"},
{"http.host", ""},
{"http.scheme", :http},
{"http.client_ip", "127.0.0.1"},
{"net.host.port", 80}
],
&(&1 in elem(attributes, 4))
)
assert %{
"http.status_code" => nil,
"http.method" => "GET",
"http.route" => "/",
"http.target" => "/",
"http.host" => "",
"http.scheme" => :http,
"http.client_ip" => "127.0.0.1",
"net.host.port" => 80
} = attributes
end

def flush_mailbox do
Expand Down

0 comments on commit 93fe9a3

Please sign in to comment.