From 93fe9a3e185bfc2f73540f774ae5bc6946597282 Mon Sep 17 00:00:00 2001 From: Leonardo Donelli Date: Wed, 21 Sep 2022 16:01:46 +0100 Subject: [PATCH] Don't set Logger metadata (#69) * 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 --- CHANGELOG.md | 19 +++++++++++++++++++ lib/teleplug.ex | 27 --------------------------- mix.exs | 5 +++-- test/teleplug_test.exs | 26 ++++++++++++-------------- 4 files changed, 34 insertions(+), 43 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4a9a690 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/lib/teleplug.ex b/lib/teleplug.ex index 620c11c..af64d05 100644 --- a/lib/teleplug.ex +++ b/lib/teleplug.ex @@ -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 @@ -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>> = <> - to_string(datadog_trace_id) - end - - defp datadog_span_id(span_id), do: to_string(span_id) end diff --git a/mix.exs b/mix.exs index 7e0f593..fbc6653 100644 --- a/mix.exs +++ b/mix.exs @@ -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 [ @@ -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() @@ -62,6 +62,7 @@ defmodule Teleplug.MixProject do defp docs do [ extras: [ + "CHANGELOG.md": [title: "Changelog"], "LICENSE.md": [title: "License"], "README.md": [title: "Overview"] ], diff --git a/test/teleplug_test.exs b/test/teleplug_test.exs index 066fa07..ba7237c 100644 --- a/test/teleplug_test.exs +++ b/test/teleplug_test.exs @@ -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