@@ -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