2525from os import environ
2626from threading import Lock
2727from time import time_ns
28- from typing import Any , Callable , Tuple , Union , cast # noqa
28+ from typing import Any , Callable , Tuple , Union , cast , overload # noqa
2929
3030from opentelemetry ._logs import Logger as APILogger
3131from opentelemetry ._logs import LoggerProvider as APILoggerProvider
3838 std_to_otel ,
3939)
4040from opentelemetry .attributes import _VALID_ANY_VALUE_TYPES , BoundedAttributes
41+ from opentelemetry .context import get_current
42+ from opentelemetry .context .context import Context
4143from opentelemetry .sdk .environment_variables import (
4244 OTEL_ATTRIBUTE_COUNT_LIMIT ,
4345 OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT ,
@@ -81,6 +83,18 @@ class LogDroppedAttributesWarning(UserWarning):
8183warnings .simplefilter ("once" , LogDroppedAttributesWarning )
8284
8385
86+ class LogDeprecatedInitWarning (UserWarning ):
87+ """Custom warning to indicate deprecated LogRecord init was used.
88+
89+ This class is used to filter and handle these specific warnings separately
90+ from other warnings, ensuring that they are only shown once without
91+ interfering with default user warnings.
92+ """
93+
94+
95+ warnings .simplefilter ("once" , LogDeprecatedInitWarning )
96+
97+
8498class LogLimits :
8599 """This class is based on a SpanLimits class in the Tracing module.
86100
@@ -180,6 +194,21 @@ class LogRecord(APILogRecord):
180194 pertinent to the event being logged.
181195 """
182196
197+ @overload
198+ def __init__ (
199+ self ,
200+ timestamp : int | None = None ,
201+ observed_timestamp : int | None = None ,
202+ context : Context | None = None ,
203+ severity_text : str | None = None ,
204+ severity_number : SeverityNumber | None = None ,
205+ body : AnyValue | None = None ,
206+ resource : Resource | None = None ,
207+ attributes : _ExtendedAttributes | None = None ,
208+ limits : LogLimits | None = _UnsetLogLimits ,
209+ ): ...
210+
211+ @overload
183212 def __init__ (
184213 self ,
185214 timestamp : int | None = None ,
@@ -193,11 +222,46 @@ def __init__(
193222 resource : Resource | None = None ,
194223 attributes : _ExtendedAttributes | None = None ,
195224 limits : LogLimits | None = _UnsetLogLimits ,
225+ ): ...
226+
227+ def __init__ (
228+ self ,
229+ timestamp : int | None = None ,
230+ observed_timestamp : int | None = None ,
231+ context : Context | None = None ,
232+ trace_id : int | None = None ,
233+ span_id : int | None = None ,
234+ trace_flags : TraceFlags | None = None ,
235+ severity_text : str | None = None ,
236+ severity_number : SeverityNumber | None = None ,
237+ body : AnyValue | None = None ,
238+ resource : Resource | None = None ,
239+ attributes : _ExtendedAttributes | None = None ,
240+ limits : LogLimits | None = _UnsetLogLimits ,
196241 ):
242+ if trace_id or span_id or trace_flags :
243+ warnings .warn (
244+ "LogRecord init with `trace_id`, `span_id`, and/or `trace_flags` is deprecated. Use `context` instead." ,
245+ LogDeprecatedInitWarning ,
246+ stacklevel = 2 ,
247+ )
248+
249+ if not context :
250+ context = get_current ()
251+
252+ if context is not None :
253+ span = get_current_span (context )
254+ span_context = span .get_span_context ()
255+ if span_context .is_valid :
256+ trace_id = span_context .trace_id
257+ span_id = span_context .span_id
258+ trace_flags = span_context .trace_flags
259+
197260 super ().__init__ (
198261 ** {
199262 "timestamp" : timestamp ,
200263 "observed_timestamp" : observed_timestamp ,
264+ "context" : context ,
201265 "trace_id" : trace_id ,
202266 "span_id" : span_id ,
203267 "trace_flags" : trace_flags ,
0 commit comments