chore: add semantic conventions to phoenix (#121)

* fix: redix missing dep

* chore: add sc dep

* chore: use semantic conventions in phoenix
This commit is contained in:
Yordis Prieto 2022-12-12 15:51:54 -05:00 committed by GitHub
parent f04340aff4
commit d1ec189362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 27 deletions

View File

@ -22,11 +22,13 @@ defmodule OpentelemetryPhoenix do
end
"""
require OpenTelemetry.Tracer
alias OpenTelemetry.SemanticConventions
alias OpenTelemetry.Span
alias OpentelemetryPhoenix.Reason
require SemanticConventions.Trace
require OpenTelemetry.Tracer
@tracer_id __MODULE__
@typedoc "Setup options"
@ -107,18 +109,18 @@ defmodule OpentelemetryPhoenix do
peer_ip = Map.get(peer_data, :address)
attributes = %{
"http.client_ip": client_ip(conn),
"http.flavor": http_flavor(adapter),
"http.host": conn.host,
"http.method": conn.method,
"http.scheme": "#{conn.scheme}",
"http.target": conn.request_path,
"http.user_agent": user_agent,
"net.host.ip": to_string(:inet_parse.ntoa(conn.remote_ip)),
"net.host.port": conn.port,
"net.peer.ip": to_string(:inet_parse.ntoa(peer_ip)),
"net.peer.port": peer_data.port,
"net.transport": :"IP.TCP"
SemanticConventions.Trace.http_client_ip() => client_ip(conn),
SemanticConventions.Trace.http_flavor() => http_flavor(adapter),
SemanticConventions.Trace.http_method() => conn.method,
SemanticConventions.Trace.http_scheme() => "#{conn.scheme}",
SemanticConventions.Trace.http_target() => conn.request_path,
SemanticConventions.Trace.http_user_agent() => user_agent,
SemanticConventions.Trace.net_host_name() => conn.host,
SemanticConventions.Trace.net_sock_host_addr() => to_string(:inet_parse.ntoa(conn.remote_ip)),
SemanticConventions.Trace.net_host_port() => conn.port,
SemanticConventions.Trace.net_sock_peer_addr() => to_string(:inet_parse.ntoa(peer_ip)),
SemanticConventions.Trace.net_peer_port() => peer_data.port,
SemanticConventions.Trace.net_transport() => :"IP.TCP"
}
# start the span with a default name. Route name isn't known until router dispatch
@ -133,7 +135,7 @@ defmodule OpentelemetryPhoenix do
# ensure the correct span is current and update the status
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta)
Span.set_attribute(ctx, :"http.status_code", conn.status)
Span.set_attribute(ctx, SemanticConventions.Trace.http_status_code(), conn.status)
if conn.status >= 500 do
Span.set_status(ctx, OpenTelemetry.status(:error, ""))
@ -146,9 +148,9 @@ defmodule OpentelemetryPhoenix do
@doc false
def handle_router_dispatch_start(_event, _measurements, meta, _config) do
attributes = %{
"phoenix.plug": meta.plug,
"phoenix.action": meta.plug_opts,
"http.route": meta.route
:"phoenix.plug" => meta.plug,
:"phoenix.action" => meta.plug_opts,
SemanticConventions.Trace.http_route() => meta.route
}
# Add more info that we now know about but don't close the span

View File

@ -60,6 +60,7 @@ defmodule OpentelemetryPhoenix.MixProject do
[
{:opentelemetry_api, "~> 1.0"},
{:opentelemetry_telemetry, "~> 1.0"},
{:opentelemetry_semantic_conventions, "~> 0.1.0"},
{:telemetry, "~> 0.4 or ~> 1.0"},
{:plug, ">= 1.11.0"},
{:cowboy_telemetry, "~> 0.4", only: [:dev, :test]},

View File

@ -21,6 +21,7 @@
"opentelemetry": {:hex, :opentelemetry, "1.1.2", "77ba2fd2fee67bebde590851a4afeda45b3f298310aa410a2a3804b364cb598a", [:rebar3], [{:opentelemetry_api, "~> 1.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "5c60be189d6aed64a9fd17055f72c93eab144be441e625276c3c95533e6bb4c7"},
"opentelemetry_api": {:hex, :opentelemetry_api, "1.1.1", "3b43877c456c8a7f5448a95d9bf4fb4bb8cc2abbbea2c62d5f8e8c538b4af14f", [:mix, :rebar3], [], "hexpm", "a9554b3208b60a70043318d051ea78fbbc7a1b8f4c418ebc16ccb40015995675"},
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.2.2", "3966c56656627ef7db6c34c4ce28d44ac8629dcd065a310d7c33712fc2a1cfe3", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.1", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.11", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "5c11adeda19e0d203a04efe92cdd7a183da4b09ae8acaba7608e9303fa258e74"},
"opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "0.1.0", "0a7463c0ec4541d9c8e09ee99bd7b5480fa6f90f40313a6d071fd7521b5a712f", [:mix, :rebar3], [], "hexpm", "eccf41bc7afec299484075aff570bf504a2f7e5c38fbcdaceb33a6d6b901448d"},
"opentelemetry_telemetry": {:hex, :opentelemetry_telemetry, "1.0.0", "d5982a319e725fcd2305b306b65c18a86afdcf7d96821473cf0649ff88877615", [:mix, :rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_registry, "~> 0.3.0", [hex: :telemetry_registry, repo: "hexpm", optional: false]}], "hexpm", "3401d13a1d4b7aa941a77e6b3ec074f0ae77f83b5b2206766ce630123a9291a9"},
"plug": {:hex, :plug, "1.14.0", "ba4f558468f69cbd9f6b356d25443d0b796fbdc887e03fa89001384a9cac638f", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bf020432c7d4feb7b3af16a0c2701455cbbbb95e5b6866132cb09eb0c29adc14"},
"plug_cowboy": {:hex, :plug_cowboy, "2.6.0", "d1cf12ff96a1ca4f52207c5271a6c351a4733f413803488d75b70ccf44aebec2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "073cf20b753ce6682ed72905cd62a2d4bd9bad1bf9f7feb02a1b8e525bd94fa6"},

View File

@ -53,16 +53,16 @@ defmodule OpentelemetryPhoenixTest do
assert %{
"http.client_ip": "10.211.55.2",
"http.flavor": :"1.1",
"http.host": "localhost",
"net.host.name": "localhost",
"http.method": "GET",
"http.route": "/users/:user_id",
"http.scheme": "http",
"http.status_code": 200,
"http.target": "/users/123",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.host.ip": "10.211.55.2",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.peer.ip": "10.211.55.2",
"net.sock.peer.addr": "10.211.55.2",
"net.peer.port": 64291,
"net.transport": :"IP.TCP",
"phoenix.action": :user,
@ -132,16 +132,16 @@ defmodule OpentelemetryPhoenixTest do
assert %{
"http.client_ip": "10.211.55.2",
"http.flavor": :"1.1",
"http.host": "localhost",
"net.host.name": "localhost",
"http.method": "GET",
"http.route": "/users/:user_id/exception",
"http.scheme": "http",
"http.status_code": 500,
"http.target": "/users/123/exception",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.host.ip": "10.211.55.2",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.peer.ip": "10.211.55.2",
"net.sock.peer.addr": "10.211.55.2",
"net.peer.port": 64291,
"net.transport": :"IP.TCP",
"phoenix.action": :code_exception,
@ -239,16 +239,16 @@ defmodule OpentelemetryPhoenixTest do
assert %{
"http.client_ip": "10.211.55.2",
"http.flavor": :"1.1",
"http.host": "localhost",
"net.host.name": "localhost",
"http.method": "GET",
"http.route": "/users/:user_id/exception",
"http.scheme": "http",
"http.status_code": 500,
"http.target": "/users/123/exception",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.host.ip": "10.211.55.2",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.peer.ip": "10.211.55.2",
"net.sock.peer.addr": "10.211.55.2",
"net.peer.port": 64291,
"net.transport": :"IP.TCP",
"phoenix.action": :code_exception,