Skip to content

Commit f83552e

Browse files
list out the aliases as fully qualified; use the semantic conventions for matches in tests
1 parent 80ab02c commit f83552e

File tree

4 files changed

+104
-70
lines changed

4 files changed

+104
-70
lines changed

instrumentation/opentelemetry_finch/lib/opentelemetry_finch.ex

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,21 @@ defmodule OpentelemetryFinch do
6161
```
6262
"""
6363

64-
alias OpenTelemetry.SemConv.{
65-
ErrorAttributes,
66-
HTTPAttributes,
67-
ServerAttributes,
68-
URLAttributes,
69-
NetworkAttributes,
70-
UserAgentAttributes,
71-
Incubating
72-
}
64+
alias OpenTelemetry.SemConv.ErrorAttributes
65+
alias OpenTelemetry.SemConv.Incubating.HTTPAttributes
66+
alias OpenTelemetry.SemConv.Incubating.URLAttributes
67+
alias OpenTelemetry.SemConv.NetworkAttributes
68+
alias OpenTelemetry.SemConv.ServerAttributes
69+
alias OpenTelemetry.SemConv.UserAgentAttributes
7370

7471
require OpenTelemetry.Tracer
7572

7673
opt_ins = [
77-
Incubating.HTTPAttributes.http_request_body_size(),
78-
Incubating.HTTPAttributes.http_response_body_size(),
74+
HTTPAttributes.http_request_body_size(),
75+
HTTPAttributes.http_response_body_size(),
7976
NetworkAttributes.network_transport(),
80-
Incubating.URLAttributes.url_scheme(),
81-
Incubating.URLAttributes.url_template(),
77+
URLAttributes.url_scheme(),
78+
URLAttributes.url_template(),
8279
UserAgentAttributes.user_agent_original()
8380
]
8481

@@ -117,11 +114,11 @@ defmodule OpentelemetryFinch do
117114
)
118115

119116
@type opt_in_attr() ::
120-
unquote(Incubating.HTTPAttributes.http_request_body_size())
121-
| unquote(Incubating.HTTPAttributes.http_response_body_size())
117+
unquote(HTTPAttributes.http_request_body_size())
118+
| unquote(HTTPAttributes.http_response_body_size())
122119
| unquote(NetworkAttributes.network_transport())
123-
| unquote(Incubating.URLAttributes.url_scheme())
124-
| unquote(Incubating.URLAttributes.url_template())
120+
| unquote(URLAttributes.url_scheme())
121+
| unquote(URLAttributes.url_template())
125122
| unquote(UserAgentAttributes.user_agent_original())
126123

127124
@type opt_in_attrs() :: [opt_in_attr()]
@@ -205,10 +202,10 @@ defmodule OpentelemetryFinch do
205202

206203
defp add_opt_in_req_attrs(attrs, request, %{opt_in_attrs: [_ | _] = opt_in_attrs} = otel_config) do
207204
%{
208-
Incubating.HTTPAttributes.http_request_body_size() => extract_request_body_size(request),
205+
HTTPAttributes.http_request_body_size() => extract_request_body_size(request),
209206
NetworkAttributes.network_transport() => :tcp,
210207
URLAttributes.url_scheme() => request.scheme,
211-
Incubating.URLAttributes.url_template() => extract_url_template(otel_config),
208+
URLAttributes.url_template() => extract_url_template(otel_config),
212209
UserAgentAttributes.user_agent_original() => extract_user_agent(request)
213210
}
214211
|> Map.take(opt_in_attrs)
@@ -232,7 +229,7 @@ defmodule OpentelemetryFinch do
232229

233230
defp add_opt_in_resp_attrs(attrs, response, %{opt_in_attrs: [_ | _] = opt_in_attrs}) do
234231
%{
235-
Incubating.HTTPAttributes.http_response_body_size() => extract_response_body_size(response)
232+
HTTPAttributes.http_response_body_size() => extract_response_body_size(response)
236233
}
237234
|> Map.take(opt_in_attrs)
238235
|> then(&Map.merge(attrs, &1))

instrumentation/opentelemetry_finch/mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ defmodule OpentelemetryFinch.MixProject do
5959
{:opentelemetry_semantic_conventions, "~> 1.27"},
6060
{:opentelemetry, "~> 1.5", only: [:dev, :test]},
6161
{:opentelemetry_exporter, "~> 1.8", only: [:dev, :test]},
62+
{:otel_http, "~> 0.2"},
6263
{:ex_doc, "~> 0.38", only: [:dev], runtime: false},
6364
{:finch, "~> 0.19", only: [:dev, :test]},
6465
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},

instrumentation/opentelemetry_finch/mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"opentelemetry_api": {:hex, :opentelemetry_api, "1.4.0", "63ca1742f92f00059298f478048dfb826f4b20d49534493d6919a0db39b6db04", [:mix, :rebar3], [], "hexpm", "3dfbbfaa2c2ed3121c5c483162836c4f9027def469c41578af5ef32589fcfc58"},
2828
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.8.0", "5d546123230771ef4174e37bedfd77e3374913304cd6ea3ca82a2add49cd5d56", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.5.0", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.18", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "a1f9f271f8d3b02b81462a6bfef7075fd8457fdb06adff5d2537df5e2264d9af"},
2929
"opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "1.27.0", "acd0194a94a1e57d63da982ee9f4a9f88834ae0b31b0bd850815fe9be4bbb45f", [:mix, :rebar3], [], "hexpm", "9681ccaa24fd3d810b4461581717661fd85ff7019b082c2dff89c7d5b1fc2864"},
30+
"otel_http": {:hex, :otel_http, "0.2.0", "b17385986c7f1b862f5d577f72614ecaa29de40392b7618869999326b9a61d8a", [:rebar3], [], "hexpm", "f2beadf922c8cfeb0965488dd736c95cc6ea8b9efce89466b3904d317d7cc717"},
3031
"plug": {:hex, :plug, "1.15.3", "712976f504418f6dff0a3e554c40d705a9bcf89a7ccef92fc6a5ef8f16a30a97", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cc4365a3c010a56af402e0809208873d113e9c38c401cabd88027ef4f5c01fd2"},
3132
"plug_cowboy": {:hex, :plug_cowboy, "2.7.0", "3ae9369c60641084363b08fe90267cbdd316df57e3557ea522114b30b63256ea", [:mix], [{:cowboy, "~> 2.7.0 or ~> 2.8.0 or ~> 2.9.0 or ~> 2.10.0", [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", "d85444fb8aa1f2fc62eabe83bbe387d81510d773886774ebdcb429b3da3c1a4a"},
3233
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},

instrumentation/opentelemetry_finch/test/opentelemetry_finch_test.exs

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ defmodule OpentelemetryFinchTest do
77
require OpenTelemetry.Span
88
require Record
99

10-
alias OpenTelemetry.SemConv.{NetworkAttributes, UserAgentAttributes, Incubating}
10+
alias OpenTelemetry.SemConv.ErrorAttributes
11+
alias OpenTelemetry.SemConv.Incubating.HTTPAttributes
12+
alias OpenTelemetry.SemConv.Incubating.URLAttributes
13+
alias OpenTelemetry.SemConv.NetworkAttributes
14+
alias OpenTelemetry.SemConv.ServerAttributes
15+
alias OpenTelemetry.SemConv.UserAgentAttributes
1116

1217
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
1318
Record.defrecord(name, spec)
@@ -46,13 +51,19 @@ defmodule OpentelemetryFinchTest do
4651
attributes: attributes
4752
)}
4853

49-
assert %{
50-
"server.address": "localhost",
51-
"server.port": bypass.port,
52-
"http.request.method": "GET",
53-
"url.full": url,
54-
"http.response.status_code": 200
55-
} == :otel_attributes.map(attributes)
54+
attrs = :otel_attributes.map(attributes)
55+
56+
expected_attrs = [
57+
{ServerAttributes.server_address(), "localhost"},
58+
{ServerAttributes.server_port(), bypass.port},
59+
{HTTPAttributes.http_request_method(), "GET"},
60+
{URLAttributes.url_full(), url},
61+
{HTTPAttributes.http_response_status_code(), 200}
62+
]
63+
64+
for {attr, val} <- expected_attrs do
65+
assert Map.get(attrs, attr) == val, " expected #{attr} to equal #{val}"
66+
end
5667
end
5768

5869
test "records span on requests with custom span name", %{bypass: bypass} do
@@ -86,11 +97,11 @@ defmodule OpentelemetryFinchTest do
8697

8798
otel_config = %{
8899
opt_in_attrs: [
89-
Incubating.HTTPAttributes.http_request_body_size(),
90-
Incubating.HTTPAttributes.http_response_body_size(),
100+
HTTPAttributes.http_request_body_size(),
101+
HTTPAttributes.http_response_body_size(),
91102
NetworkAttributes.network_transport(),
92-
Incubating.URLAttributes.url_scheme(),
93-
Incubating.URLAttributes.url_template(),
103+
URLAttributes.url_scheme(),
104+
URLAttributes.url_template(),
94105
UserAgentAttributes.user_agent_original()
95106
],
96107
url_template: "/users/:user_id"
@@ -108,19 +119,25 @@ defmodule OpentelemetryFinchTest do
108119
attributes: attributes
109120
)}
110121

111-
assert %{
112-
"server.address": "localhost",
113-
"server.port": bypass.port,
114-
"http.request.method": "GET",
115-
"url.full": endpoint_url(bypass.port),
116-
"http.response.status_code": 200,
117-
"http.request.body.size": 0,
118-
"http.response.body.size": 0,
119-
"network.transport": :tcp,
120-
"url.scheme": :http,
121-
"url.template": "/users/:user_id",
122-
"user_agent.original": ""
123-
} == :otel_attributes.map(attributes)
122+
attrs = :otel_attributes.map(attributes)
123+
124+
expected_attrs = [
125+
{ServerAttributes.server_address(), "localhost"},
126+
{ServerAttributes.server_port(), bypass.port},
127+
{HTTPAttributes.http_request_method(), "GET"},
128+
{URLAttributes.url_full(), endpoint_url(bypass.port)},
129+
{HTTPAttributes.http_response_status_code(), 200},
130+
{HTTPAttributes.http_request_body_size(), 0},
131+
{HTTPAttributes.http_response_body_size(), 0},
132+
{NetworkAttributes.network_transport(), :tcp},
133+
{URLAttributes.url_scheme(), :http},
134+
{URLAttributes.url_template(), "/users/:user_id"},
135+
{UserAgentAttributes.user_agent_original(), ""}
136+
]
137+
138+
for {attr, val} <- expected_attrs do
139+
assert Map.get(attrs, attr) == val, " expected #{attr} to equal #{val}"
140+
end
124141
end
125142

126143
test "adds request and response headers to span when request_header_attrs and response_header_attrs are set",
@@ -144,16 +161,22 @@ defmodule OpentelemetryFinchTest do
144161
attributes: attributes
145162
)}
146163

147-
assert %{
148-
"server.address": "localhost",
149-
"server.port": bypass.port,
150-
"http.request.method": "GET",
151-
"url.full": endpoint_url(bypass.port),
152-
"http.response.status_code": 200,
153-
"http.response.header.content-length": ["0"],
154-
"http.response.header.server": ["Cowboy"],
155-
"http.request.header.authorization": ["Bearer token"]
156-
} == :otel_attributes.map(attributes)
164+
attrs = :otel_attributes.map(attributes)
165+
166+
expected_attrs = [
167+
{ServerAttributes.server_address(), "localhost"},
168+
{ServerAttributes.server_port(), bypass.port},
169+
{HTTPAttributes.http_request_method(), "GET"},
170+
{URLAttributes.url_full(), endpoint_url(bypass.port)},
171+
{HTTPAttributes.http_response_status_code(), 200},
172+
{String.to_atom("#{HTTPAttributes.http_response_header()}.content-length"), ["0"]},
173+
{String.to_atom("#{HTTPAttributes.http_response_header()}.server"), ["Cowboy"]},
174+
{String.to_atom("#{HTTPAttributes.http_request_header()}.authorization"), ["Bearer token"]}
175+
]
176+
177+
for {attr, val} <- expected_attrs do
178+
assert Map.get(attrs, attr) == val, " expected #{attr} to equal #{val}"
179+
end
157180
end
158181

159182
test "records span on requests failed", %{bypass: _} do
@@ -167,13 +190,19 @@ defmodule OpentelemetryFinchTest do
167190
attributes: attributes
168191
)}
169192

170-
assert %{
171-
"server.address": "localhost",
172-
"server.port": 3333,
173-
"http.request.method": "GET",
174-
"url.full": "http://localhost:3333/",
175-
"error.type": "connection refused"
176-
} == :otel_attributes.map(attributes)
193+
attrs = :otel_attributes.map(attributes)
194+
195+
expected_attrs = [
196+
{ServerAttributes.server_address(), "localhost"},
197+
{ServerAttributes.server_port(), 3333},
198+
{HTTPAttributes.http_request_method(), "GET"},
199+
{URLAttributes.url_full(), "http://localhost:3333/"},
200+
{ErrorAttributes.error_type(), "connection refused"}
201+
]
202+
203+
for {attr, val} <- expected_attrs do
204+
assert Map.get(attrs, attr) == val, " expected #{attr} to equal #{val}"
205+
end
177206
end
178207

179208
test "records span on request response with 4xx status code", %{bypass: bypass} do
@@ -189,14 +218,20 @@ defmodule OpentelemetryFinchTest do
189218
attributes: attributes
190219
)}
191220

192-
assert %{
193-
"server.address": "localhost",
194-
"server.port": bypass.port,
195-
"http.request.method": "GET",
196-
"url.full": endpoint_url(bypass.port),
197-
"http.response.status_code": 404,
198-
"error.type": "404"
199-
} == :otel_attributes.map(attributes)
221+
attrs = :otel_attributes.map(attributes)
222+
223+
expected_attrs = [
224+
{ServerAttributes.server_address(), "localhost"},
225+
{ServerAttributes.server_port(), bypass.port},
226+
{HTTPAttributes.http_request_method(), "GET"},
227+
{URLAttributes.url_full(), endpoint_url(bypass.port)},
228+
{HTTPAttributes.http_response_status_code(), 404},
229+
{ErrorAttributes.error_type(), "404"}
230+
]
231+
232+
for {attr, val} <- expected_attrs do
233+
assert Map.get(attrs, attr) == val, " expected #{attr} to equal #{val}"
234+
end
200235
end
201236

202237
test "logs error and doesn't record span if invalid otel option is passed", %{bypass: bypass} do

0 commit comments

Comments
 (0)