Skip to content

Commit 1a47ccb

Browse files
committed
Modify logic to accomodate OTEL breaking changes
1 parent f8f1fcd commit 1a47ccb

File tree

7 files changed

+70
-68
lines changed

7 files changed

+70
-68
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Some of the key concepts for the Azure monitor exporter include:
8888

8989
* [LogRecord][log_record]: Represents a log record emitted from a supported logging library.
9090

91-
* [Logger][logger]: Converts a `LogRecord` into `ReadableLogRecord`, and will be pushed through the SDK to be exported.
91+
* [Logger][logger]: Converts a `LogRecord` into a readable `LogData`, and will be pushed through the SDK to be exported.
9292

9393
* [Logger Provider][logger_provider]: Provides a `Logger` for the given instrumentation library.
9494

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_quickpulse/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dataclasses import dataclass, fields
55
from typing import Dict, no_type_check
66

7-
from opentelemetry.sdk._logs import LogRecord
7+
from opentelemetry._logs import LogRecord
88
from opentelemetry.sdk.trace import Event, ReadableSpan
99
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes
1010
from opentelemetry.semconv.attributes.http_attributes import (
@@ -177,7 +177,7 @@ def _from_log_record(log_record: LogRecord):
177177

178178
@staticmethod
179179
@no_type_check
180-
def _from_span_event(span_event: LogRecord.event_name):
180+
def _from_span_event(span_event: LogRecord):
181181
return _ExceptionData(
182182
message=str(span_event.attributes.get(SpanAttributes.EXCEPTION_MESSAGE, "")),
183183
stack_trace=str(span_event.attributes.get(SpanAttributes.EXCEPTION_STACKTRACE, "")),

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/logs/_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def _convert_log_to_envelope(readable_log_record: ReadableLogRecord) -> Telemetr
121121
log_record = readable_log_record.log_record
122122
time_stamp = log_record.timestamp if log_record.timestamp is not None else log_record.observed_timestamp
123123
envelope = _utils._create_telemetry_item(time_stamp)
124-
envelope.tags.update(_utils._populate_part_a_fields(log_record.resource)) # type: ignore
124+
envelope.tags.update(_utils._populate_part_a_fields(readable_log_record.resource)) # type: ignore
125125
envelope.tags[ContextTagKeys.AI_OPERATION_ID] = "{:032x}".format( # type: ignore
126126
log_record.trace_id or _DEFAULT_TRACE_ID
127127
)

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/logs/test_logs.py

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
EXCEPTION_TYPE,
1616
)
1717
from opentelemetry.sdk import _logs
18+
from opentelemetry._logs import LogRecord
1819
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
1920
from opentelemetry.sdk.resources import Resource
2021
from opentelemetry.sdk._logs.export import LogRecordExportResult
@@ -73,194 +74,194 @@ def setUpClass(cls):
7374
span = NonRecordingSpan(span_context)
7475
ctx = set_span_in_context(span)
7576
cls._log_data = _logs.ReadWriteLogRecord(
76-
_logs.LogRecord(
77+
LogRecord(
7778
timestamp=1646865018558419456,
7879
context=ctx,
7980
severity_text="WARNING",
8081
severity_number=SeverityNumber.WARN,
8182
body="Test message",
82-
resource=Resource.create(attributes={"asd": "test_resource"}),
8383
attributes={"test": "attribute", "ai.operation.name": "TestOperationName"},
8484
),
85-
InstrumentationScope("test_name"),
85+
resource=Resource.create(attributes={"asd": "test_resource"}),
86+
instrumentation_scope=InstrumentationScope("test_name"),
8687
)
8788
cls._log_data_empty = _logs.ReadWriteLogRecord(
88-
_logs.LogRecord(
89+
LogRecord(
8990
timestamp=1646865018558419456,
9091
context=ctx,
9192
severity_text="WARNING",
9293
severity_number=SeverityNumber.WARN,
9394
body="",
94-
resource=Resource.create(attributes={"asd": "test_resource"}),
9595
attributes={"test": "attribute", "ai.operation.name": "TestOperationName"},
9696
),
97-
InstrumentationScope("test_name"),
97+
resource=Resource.create(attributes={"asd": "test_resource"}),
98+
instrumentation_scope=InstrumentationScope("test_name"),
9899
)
99100
cls._log_data_none = _logs.ReadWriteLogRecord(
100-
_logs.LogRecord(
101+
LogRecord(
101102
timestamp=1646865018558419456,
102103
context=ctx,
103104
severity_text="WARNING",
104105
severity_number=SeverityNumber.WARN,
105106
body=None,
106-
resource=Resource.create(attributes={"asd": "test_resource"}),
107107
attributes={"test": "attribute"},
108108
),
109-
InstrumentationScope("test_name"),
109+
resource=Resource.create(attributes={"asd": "test_resource"}),
110+
instrumentation_scope=InstrumentationScope("test_name"),
110111
)
111112
cls._log_data_complex_body = _logs.ReadWriteLogRecord(
112-
_logs.LogRecord(
113+
LogRecord(
113114
timestamp=1646865018558419456,
114115
context=ctx,
115116
severity_text="WARNING",
116117
severity_number=SeverityNumber.WARN,
117118
body={"foo": {"bar": "baz", "qux": 42}},
118-
resource=Resource.create(attributes={"asd": "test_resource"}),
119119
attributes={"test": "attribute", "ai.operation.name": "TestOperationName"},
120120
),
121-
InstrumentationScope("test_name"),
121+
resource=Resource.create(attributes={"asd": "test_resource"}),
122+
instrumentation_scope=InstrumentationScope("test_name"),
122123
)
123124
cls._log_data_complex_body_not_serializeable = _logs.ReadWriteLogRecord(
124-
_logs.LogRecord(
125+
LogRecord(
125126
timestamp=1646865018558419456,
126127
context=ctx,
127128
severity_text="WARNING",
128129
severity_number=SeverityNumber.WARN,
129130
body=NotSerializeableClass(),
130-
resource=Resource.create(attributes={"asd": "test_resource"}),
131131
attributes={"test": "attribute"},
132132
),
133-
InstrumentationScope("test_name"),
133+
resource=Resource.create(attributes={"asd": "test_resource"}),
134+
instrumentation_scope=InstrumentationScope("test_name"),
134135
)
135136
cls._log_data_empty_with_whitespaces = _logs.ReadWriteLogRecord(
136-
_logs.LogRecord(
137+
LogRecord(
137138
timestamp=1646865018558419456,
138139
context=ctx,
139140
severity_text="WARNING",
140141
severity_number=SeverityNumber.WARN,
141142
body=" ",
142-
resource=Resource.create(attributes={"asd": "test_resource"}),
143143
attributes={"test": "attribute"},
144144
),
145-
InstrumentationScope("test_name"),
145+
resource=Resource.create(attributes={"asd": "test_resource"}),
146+
instrumentation_scope=InstrumentationScope("test_name"),
146147
)
147148
cls._log_data_event = _logs.ReadWriteLogRecord(
148-
_logs.LogRecord(
149+
LogRecord(
149150
timestamp=1646865018558419456,
150151
context=ctx,
151152
severity_text="INFO",
152153
severity_number=SeverityNumber.INFO,
153154
body="Test Event",
154-
resource=Resource.create(attributes={"asd": "test_resource"}),
155155
attributes={
156156
"event_key": "event_attribute",
157157
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE: True,
158158
},
159159
),
160-
InstrumentationScope("test_name"),
160+
resource=Resource.create(attributes={"asd": "test_resource"}),
161+
instrumentation_scope=InstrumentationScope("test_name"),
161162
)
162163
cls._log_data_event_complex_body = _logs.ReadWriteLogRecord(
163-
_logs.LogRecord(
164+
LogRecord(
164165
timestamp=1646865018558419456,
165166
context=ctx,
166167
severity_text="INFO",
167168
severity_number=SeverityNumber.INFO,
168169
body={"foo": {"bar": "baz", "qux": 42}},
169-
resource=Resource.create(attributes={"asd": "test_resource"}),
170170
attributes={
171171
"event_key": "event_attribute",
172172
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE: True,
173173
},
174174
),
175-
InstrumentationScope("test_name"),
175+
resource=Resource.create(attributes={"asd": "test_resource"}),
176+
instrumentation_scope=InstrumentationScope("test_name"),
176177
)
177178
cls._log_data_event_complex_body_not_serializeable = _logs.ReadWriteLogRecord(
178-
_logs.LogRecord(
179+
LogRecord(
179180
timestamp=1646865018558419456,
180181
severity_text="INFO",
181182
severity_number=SeverityNumber.INFO,
182183
body=NotSerializeableClass(),
183-
resource=Resource.create(attributes={"asd": "test_resource"}),
184184
attributes={
185185
"event_key": "event_attribute",
186186
_APPLICATION_INSIGHTS_EVENT_MARKER_ATTRIBUTE: True,
187187
},
188188
),
189-
InstrumentationScope("test_name"),
189+
resource=Resource.create(attributes={"asd": "test_resource"}),
190+
instrumentation_scope=InstrumentationScope("test_name"),
190191
)
191192
cls._log_data_custom_event = _logs.ReadWriteLogRecord(
192-
_logs.LogRecord(
193+
LogRecord(
193194
timestamp=1646865018558419456,
194195
context=ctx,
195196
severity_text="INFO",
196197
severity_number=SeverityNumber.INFO,
197198
body="Test Event",
198-
resource=Resource.create(attributes={"asd": "test_resource"}),
199199
attributes={
200200
"event_key": "event_attribute",
201201
_MICROSOFT_CUSTOM_EVENT_NAME: "event_name",
202202
"client.address": "192.168.1.1",
203203
},
204204
),
205-
InstrumentationScope("test_name"),
205+
resource=Resource.create(attributes={"asd": "test_resource"}),
206+
instrumentation_scope=InstrumentationScope("test_name"),
206207
)
207208
cls._exc_data = _logs.ReadWriteLogRecord(
208-
_logs.LogRecord(
209+
LogRecord(
209210
timestamp=1646865018558419456,
210211
context=ctx,
211212
severity_text="EXCEPTION",
212213
severity_number=SeverityNumber.FATAL,
213214
body="Test message",
214-
resource=Resource.create(attributes={"asd": "test_resource"}),
215215
attributes={
216216
"test": "attribute",
217217
EXCEPTION_TYPE: "ZeroDivisionError",
218218
EXCEPTION_MESSAGE: "division by zero",
219219
EXCEPTION_STACKTRACE: 'Traceback (most recent call last):\n File "test.py", line 38, in <module>\n raise ZeroDivisionError()\nZeroDivisionError\n',
220220
},
221221
),
222-
InstrumentationScope("test_name"),
222+
resource=Resource.create(attributes={"asd": "test_resource"}),
223+
instrumentation_scope=InstrumentationScope("test_name"),
223224
)
224225
cls._exc_data_with_exc_body = _logs.ReadWriteLogRecord(
225-
_logs.LogRecord(
226+
LogRecord(
226227
timestamp=1646865018558419456,
227228
context=ctx,
228229
severity_text="EXCEPTION",
229230
severity_number=SeverityNumber.FATAL,
230231
body=Exception("test exception message"),
231-
resource=Resource.create(attributes={"asd": "test_resource"}),
232232
attributes={
233233
"test": "attribute",
234234
EXCEPTION_TYPE: "ZeroDivisionError",
235235
EXCEPTION_MESSAGE: "division by zero",
236236
EXCEPTION_STACKTRACE: 'Traceback (most recent call last):\n File "test.py", line 38, in <module>\n raise ZeroDivisionError()\nZeroDivisionError\n',
237237
},
238238
),
239-
InstrumentationScope("test_name"),
239+
resource=Resource.create(attributes={"asd": "test_resource"}),
240+
instrumentation_scope=InstrumentationScope("test_name"),
240241
)
241242
cls._exc_data_blank_exception = _logs.ReadWriteLogRecord(
242-
_logs.LogRecord(
243+
LogRecord(
243244
timestamp=1646865018558419456,
244245
context=ctx,
245246
severity_text="EXCEPTION",
246247
severity_number=SeverityNumber.FATAL,
247248
body="test exception",
248-
resource=Resource.create(attributes={"asd": "test_resource"}),
249249
attributes={"test": "attribute", EXCEPTION_TYPE: "", EXCEPTION_MESSAGE: "", EXCEPTION_STACKTRACE: ""},
250250
),
251-
InstrumentationScope("test_name"),
251+
resource=Resource.create(attributes={"asd": "test_resource"}),
252+
instrumentation_scope=InstrumentationScope("test_name"),
252253
)
253254
cls._exc_data_empty = _logs.ReadWriteLogRecord(
254-
_logs.LogRecord(
255+
LogRecord(
255256
timestamp=1646865018558419456,
256257
context=ctx,
257258
severity_text="EXCEPTION",
258259
severity_number=SeverityNumber.FATAL,
259260
body="",
260-
resource=Resource.create(attributes={"asd": "test_resource"}),
261261
attributes={"test": "attribute", EXCEPTION_TYPE: "", EXCEPTION_MESSAGE: "", EXCEPTION_STACKTRACE: ""},
262262
),
263-
InstrumentationScope("test_name"),
263+
resource=Resource.create(attributes={"asd": "test_resource"}),
264+
instrumentation_scope=InstrumentationScope("test_name"),
264265
)
265266

266267
@classmethod
@@ -339,15 +340,15 @@ def test_export_not_retryable(self):
339340

340341
def test_log_to_envelope_partA(self):
341342
exporter = self._exporter
342-
old_resource = self._log_data.log_record.resource
343+
old_resource = self._log_data.resource
343344
resource = Resource(
344345
{
345346
"service.name": "testServiceName",
346347
"service.namespace": "testServiceNamespace",
347348
"service.instance.id": "testServiceInstanceId",
348349
}
349350
)
350-
self._log_data.log_record.resource = resource
351+
self._log_data.resource = resource
351352
envelope = exporter._log_to_envelope(self._log_data)
352353

353354
self.assertEqual(envelope.instrumentation_key, "1234abcd-5678-4efa-8abc-1234567890ab")
@@ -373,22 +374,22 @@ def test_log_to_envelope_partA(self):
373374
self.assertEqual(envelope.tags.get(ContextTagKeys.AI_OPERATION_ID), "{:032x}".format(trace_id))
374375
span_id = self._log_data.log_record.span_id
375376
self.assertEqual(envelope.tags.get(ContextTagKeys.AI_OPERATION_PARENT_ID), "{:016x}".format(span_id))
376-
self._log_data.log_record.resource = old_resource
377+
self._log_data.resource = old_resource
377378
self.assertEqual(envelope.tags.get(ContextTagKeys.AI_OPERATION_NAME), "TestOperationName")
378379

379380
def test_log_to_envelope_partA_default(self):
380381
exporter = self._exporter
381-
old_resource = self._log_data.log_record.resource
382+
old_resource = self._log_data.resource
382383
resource = Resource({"service.name": "testServiceName"})
383-
self._log_data.log_record.resource = resource
384+
self._log_data.resource = resource
384385
envelope = exporter._log_to_envelope(self._log_data)
385386
self.assertEqual(envelope.tags.get(ContextTagKeys.AI_CLOUD_ROLE), "testServiceName")
386387
self.assertEqual(envelope.tags.get(ContextTagKeys.AI_CLOUD_ROLE_INSTANCE), platform.node())
387388
self.assertEqual(
388389
envelope.tags.get(ContextTagKeys.AI_INTERNAL_NODE_NAME),
389390
envelope.tags.get(ContextTagKeys.AI_CLOUD_ROLE_INSTANCE),
390391
)
391-
self._log_data.log_record.resource = old_resource
392+
self._log_data.resource = old_resource
392393

393394
def test_log_to_envelope_log(self):
394395
exporter = self._exporter
@@ -575,19 +576,19 @@ def test_log_to_envelope_synthetic_source(self):
575576
span = NonRecordingSpan(span_context)
576577
ctx = set_span_in_context(span)
577578
log_data = _logs.ReadWriteLogRecord(
578-
_logs.LogRecord(
579+
LogRecord(
579580
timestamp=1646865018558419456,
580581
context=ctx,
581582
severity_text="WARNING",
582583
severity_number=SeverityNumber.WARN,
583584
body="Test message",
584-
resource=resource,
585585
attributes={
586586
"test": "attribute",
587587
"user_agent.synthetic.type": "bot",
588588
},
589589
),
590-
InstrumentationScope("test_name"),
590+
resource=resource,
591+
instrumentation_scope=InstrumentationScope("test_name"),
591592
)
592593
envelope = exporter._log_to_envelope(log_data)
593594

@@ -613,19 +614,19 @@ def test_log_to_envelope_synthetic_load_always_on(self):
613614
span = NonRecordingSpan(span_context)
614615
ctx = set_span_in_context(span)
615616
log_data = _logs.ReadWriteLogRecord(
616-
_logs.LogRecord(
617+
LogRecord(
617618
timestamp=1646865018558419456,
618619
context=ctx,
619620
severity_text="WARNING",
620621
severity_number=SeverityNumber.WARN,
621622
body="Test message",
622-
resource=resource,
623623
attributes={
624624
"test": "attribute",
625625
"http.user_agent": "Azure-Load-Testing/1.0 AlwaysOn",
626626
},
627627
),
628-
InstrumentationScope("test_name"),
628+
resource=resource,
629+
instrumentation_scope=InstrumentationScope("test_name"),
629630
)
630631
envelope = exporter._log_to_envelope(log_data)
631632

0 commit comments

Comments
 (0)