From 6943e8ea73a9fe82795f190691a2eb96376cf9d5 Mon Sep 17 00:00:00 2001 From: Troels Thomsen <19824+tt@users.noreply.github.com> Date: Tue, 25 Jun 2024 19:05:06 +0200 Subject: [PATCH] Adopt latest semantic conventions (#174) --- lib/telepoison.ex | 28 ++++++++++++++++++++++------ test/telepoison_test.exs | 26 ++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/telepoison.ex b/lib/telepoison.ex index 6e661c2..ee6f527 100644 --- a/lib/telepoison.ex +++ b/lib/telepoison.ex @@ -19,11 +19,18 @@ defmodule Telepoison do alias OpenTelemetry.Tracer alias Telepoison.Configuration - @http_url Atom.to_string(Conventions.http_url()) @http_method Atom.to_string(Conventions.http_method()) + @http_request_method "http.request.method" + @http_response_status_code "http.response.status_code" @http_route Atom.to_string(Conventions.http_route()) @http_status_code Atom.to_string(Conventions.http_status_code()) + @http_url Atom.to_string(Conventions.http_url()) @net_peer_name Atom.to_string(Conventions.net_peer_name()) + @server_address "server.address" + @server_port "server.port" + @url_full "url.full" + @url_scheme "url.scheme" + @url_template "url.template" @doc ~S""" Configures Telepoison using the provided `opts` `Keyword list`. @@ -151,7 +158,7 @@ defmodule Telepoison do span_name = Keyword.get_lazy(opts, :ot_span_name, fn -> default_span_name(request) end) - %URI{host: host} = request.url |> process_request_url() |> URI.parse() + %URI{scheme: scheme, host: host, port: port} = request.url |> process_request_url() |> URI.parse() resource_route_attribute = opts @@ -159,14 +166,14 @@ defmodule Telepoison do |> get_resource_route(request) |> case do resource_route when is_binary(resource_route) -> - [{@http_route, resource_route}] + [{@http_route, resource_route}, {@url_template, resource_route}] nil -> [] end ot_attributes = - get_standard_ot_attributes(request, host) ++ + get_standard_ot_attributes(request, scheme, host, port) ++ get_ot_attributes(opts) ++ resource_route_attribute @@ -195,6 +202,7 @@ defmodule Telepoison do Tracer.set_status(:error, "") end + Tracer.set_attribute(@http_response_status_code, status_code) Tracer.set_attribute(@http_status_code, status_code) end_span() status_code @@ -220,14 +228,22 @@ defmodule Telepoison do Tracer.set_current_span(ctx) end - defp get_standard_ot_attributes(request, host) do + defp get_standard_ot_attributes(request, scheme, host, port) do [ {@http_method, request.method |> Atom.to_string() |> String.upcase()}, + {@http_request_method, + request.method + |> Atom.to_string() + |> String.upcase()}, {@http_url, strip_uri_credentials(request.url)}, - {@net_peer_name, host} + {@net_peer_name, host}, + {@server_address, host}, + {@server_port, port}, + {@url_full, strip_uri_credentials(request.url)}, + {@url_scheme, scheme} ] end diff --git a/test/telepoison_test.exs b/test/telepoison_test.exs index 501a271..fb0db0d 100644 --- a/test/telepoison_test.exs +++ b/test/telepoison_test.exs @@ -25,11 +25,28 @@ defmodule TelepoisonTest do assert_receive {:span, span(attributes: attributes_record, name: "GET")} attributes = elem(attributes_record, 4) - assert ["http.method", "http.status_code", "http.url", "net.peer.name"] == + assert [ + "http.method", + "http.request.method", + "http.response.status_code", + "http.status_code", + "http.url", + "net.peer.name", + "server.address", + "server.port", + "url.full", + "url.scheme" + ] == attributes |> Map.keys() |> Enum.sort() assert {"http.method", "GET"} in attributes + assert {"http.request.method", "GET"} in attributes + assert {"http.response.status_code", 200} in attributes assert {"net.peer.name", "localhost"} in attributes + assert {"server.address", "localhost"} in attributes + assert {"server.port", 8000} in attributes + assert {"url.full", "http://localhost:8000"} in attributes + assert {"url.scheme", "http"} in attributes end test "traceparent header is injected when no headers" do @@ -57,11 +74,12 @@ defmodule TelepoisonTest do assert "atom" in Enum.map(headers, &elem(&1, 0)) end - test "http.url doesn't contain credentials" do + test "http.url and url.full don't contain credentials" do Telepoison.get!("http://user:pass@localhost:8000/user/edit/24") assert_receive {:span, span(attributes: attributes)}, 1000 assert confirm_attributes(attributes, {"http.url", "http://localhost:8000/user/edit/24"}) + assert confirm_attributes(attributes, {"url.full", "http://localhost:8000/user/edit/24"}) end end @@ -78,6 +96,7 @@ defmodule TelepoisonTest do assert_receive {:span, span(attributes: attributes)}, 1000 assert confirm_attributes(attributes, {"http.route", "/user/edit"}) + assert confirm_attributes(attributes, {"url.template", "/user/edit"}) end test "resource route can be explicitly passed to Telepoison invocation as a function" do @@ -87,6 +106,7 @@ defmodule TelepoisonTest do assert_receive {:span, span(attributes: attributes)}, 1000 assert confirm_attributes(attributes, {"http.route", "/user/edit/24"}) + assert confirm_attributes(attributes, {"url.template", "/user/edit/24"}) end test "resource route inference can be explicitly ignored" do @@ -121,6 +141,7 @@ defmodule TelepoisonTest do assert_receive {:span, span(attributes: attributes)}, 1000 assert confirm_attributes(attributes, {"http.route", "/user/edit"}) + assert confirm_attributes(attributes, {"url.template", "/user/edit"}) assert confirm_attributes(attributes, {"app.callname", "mariorossi"}) end end @@ -297,6 +318,7 @@ defmodule TelepoisonTest do defp confirm_http_route_attribute(attributes, value) do confirm_attributes(attributes, {"http.route", value}) + confirm_attributes(attributes, {"url.template", value}) end defp confirm_http_route_attribute(attributes) do