Skip to content
This repository was archived by the owner on Sep 4, 2022. It is now read-only.

Commit 28bf7ef

Browse files
authored
Merge pull request #99 from cisco-open/fix/aiohttp-url/SDK-668
fix(aiohttp-attributes): add http.url attribute
2 parents 68b44db + b0b2c85 commit 28bf7ef

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

cisco_telescope/instrumentations/aiohttp/__init__.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
StatusCode,
4141
)
4242
from opentelemetry import trace
43+
from opentelemetry.util.http import remove_url_credentials
4344
from cisco_opentelemetry_specifications import SemanticAttributes
4445

4546
from ..utils import Utils
@@ -109,22 +110,20 @@ async def on_request_start(
109110

110111
http_method = params.method.upper()
111112
request_span_name = f"HTTP {http_method}"
113+
request_url = remove_url_credentials(str(params.url))
114+
115+
span_attributes = {
116+
SpanAttributes.HTTP_METHOD: http_method,
117+
SpanAttributes.HTTP_URL: request_url,
118+
}
112119

113120
trace_config_ctx.span = trace_config_ctx.tracer.start_span(
114-
request_span_name,
115-
kind=SpanKind.CLIENT,
121+
request_span_name, kind=SpanKind.CLIENT, attributes=span_attributes
116122
)
117123

118124
if callable(request_hook):
119125
request_hook(trace_config_ctx.span, params)
120126

121-
if trace_config_ctx.span.is_recording():
122-
attributes = {
123-
SpanAttributes.HTTP_METHOD: http_method,
124-
}
125-
for key, value in attributes.items():
126-
trace_config_ctx.span.set_attribute(key, value)
127-
128127
trace_config_ctx.token = context_api.attach(
129128
trace.set_span_in_context(trace_config_ctx.span)
130129
)

cisco_telescope/options.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__(
123123
exporter_type=Consts.DEFAULT_EXPORTER_TYPE,
124124
collector_endpoint=Consts.DEFAULT_COLLECTOR_ENDPOINT,
125125
custom_headers={
126-
Consts.TOKEN_HEADER_KEY: verify_token(self.cisco_token)
126+
Consts.TOKEN_HEADER_KEY: _verify_token(self.cisco_token)
127127
},
128128
)
129129
]
@@ -137,6 +137,7 @@ def __str__(self):
137137
f"token: {self.cisco_token},\n\t"
138138
f"service_name:{self.service_name},\n\t"
139139
f"max_payload_size: {self.max_payload_size},\n\t"
140+
f"disable_instrumentations: {self.disable_instrumentations},\n\t"
140141
f"exporters: \n\t{', '.join(map(str, self.exporters))})"
141142
)
142143

@@ -172,7 +173,7 @@ def _set_debug(self):
172173
)
173174

174175

175-
def verify_token(token: str) -> str:
176+
def _verify_token(token: str) -> str:
176177
auth_prefix = "Bearer "
177178
if not token:
178179
return ""

tests/instrumentations/test_aiohttp.py

+21-26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from unittest import IsolatedAsyncioTestCase
2121

2222
from cisco_opentelemetry_specifications import SemanticAttributes
23+
from opentelemetry.semconv.trace import SpanAttributes
2324
from cisco_telescope.configuration import Configuration
2425
from cisco_telescope.instrumentations.aiohttp import AiohttpInstrumentorWrapper
2526
from .base_http_test import BaseHttpTest
@@ -43,20 +44,7 @@ async def test_get_request_sanity(self):
4344
headers=self.request_headers(),
4445
chunked=True,
4546
) as resp:
46-
self.assertEqual(resp.status, 200)
47-
spans = self.memory_exporter.get_finished_spans()
48-
self.assertEqual(len(spans), 1)
49-
span = spans[0]
50-
self.assert_captured_headers(
51-
span,
52-
SemanticAttributes.HTTP_REQUEST_HEADER,
53-
self.request_headers(),
54-
)
55-
self.assert_captured_headers(
56-
span,
57-
SemanticAttributes.HTTP_RESPONSE_HEADER,
58-
self.response_headers(),
59-
)
47+
self._assert_basic_attributes_and_headers(resp)
6048

6149
async def test_post_request_sanity(self):
6250
Configuration().payloads_enabled = True
@@ -67,21 +55,10 @@ async def test_post_request_sanity(self):
6755
chunked=True,
6856
data=self.request_body(),
6957
) as resp:
70-
self.assertEqual(resp.status, 200)
58+
self._assert_basic_attributes_and_headers(resp)
7159
spans = self.memory_exporter.get_finished_spans()
72-
self.assertEqual(len(spans), 1)
7360
span = spans[0]
7461

75-
self.assert_captured_headers(
76-
span,
77-
SemanticAttributes.HTTP_REQUEST_HEADER,
78-
self.request_headers(),
79-
)
80-
self.assert_captured_headers(
81-
span,
82-
SemanticAttributes.HTTP_RESPONSE_HEADER,
83-
self.response_headers(),
84-
)
8562
self.assertEqual(
8663
span.attributes[SemanticAttributes.HTTP_REQUEST_BODY],
8764
self.request_body(),
@@ -167,3 +144,21 @@ async def test_response_content_unharmed(self):
167144
span.attributes[SemanticAttributes.HTTP_RESPONSE_BODY],
168145
resp_body,
169146
)
147+
148+
def _assert_basic_attributes_and_headers(self, resp):
149+
self.assertEqual(resp.status, 200)
150+
spans = self.memory_exporter.get_finished_spans()
151+
self.assertEqual(len(spans), 1)
152+
span = spans[0]
153+
self.assertEqual(span.attributes[SpanAttributes.HTTP_METHOD], resp.method)
154+
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], str(resp.url))
155+
self.assert_captured_headers(
156+
span,
157+
SemanticAttributes.HTTP_REQUEST_HEADER,
158+
self.request_headers(),
159+
)
160+
self.assert_captured_headers(
161+
span,
162+
SemanticAttributes.HTTP_RESPONSE_HEADER,
163+
self.response_headers(),
164+
)

0 commit comments

Comments
 (0)