Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add otelTraceSampled to instrumetation-logging #1773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make ASGI request span attributes available for `start_span`.
([#1762](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1762))
- `opentelemetry-instrumentation-celery` Add support for anonymous tasks.
([#1407](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1407)
([#1407](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1407))
- `opentelemetry-instrumentation-logging` Add `otelTraceSampled` to instrumetation-logging
([#1773](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1773))


### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def record_factory(*args, **kwargs):

record.otelSpanID = "0"
record.otelTraceID = "0"
record.otelTraceSampled = False

nonlocal service_name
if service_name is None:
Expand All @@ -113,6 +114,7 @@ def record_factory(*args, **kwargs):
if ctx != INVALID_SPAN_CONTEXT:
record.otelSpanID = format(ctx.span_id, "016x")
record.otelTraceID = format(ctx.trace_id, "032x")
record.otelTraceSampled = ctx.trace_flags.sampled
if callable(LoggingInstrumentor._log_hook):
try:
LoggingInstrumentor._log_hook( # pylint: disable=E1102
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DEFAULT_LOGGING_FORMAT = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s] - %(message)s"
DEFAULT_LOGGING_FORMAT = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s trace_sampled=%(otelTraceSampled)s] - %(message)s"


_MODULE_DOC = """
Expand All @@ -27,6 +27,7 @@
- ``otelSpanID``
- ``otelTraceID``
- ``otelServiceName``
- ``otelTraceSampled``

The integration uses the following logging format by default:

Expand Down Expand Up @@ -113,7 +114,7 @@

.. code-block::

%(otelSpanID)s %(otelTraceID)s %(otelServiceName)s
%(otelSpanID)s %(otelTraceID)s %(otelServiceName)s %(otelTraceSampled)s



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_trace_context_injection(self):
self.assertEqual(record.otelSpanID, "0")
self.assertEqual(record.otelTraceID, "0")
self.assertEqual(record.otelServiceName, "")
self.assertEqual(record.otelTraceSampled, False)


def log_hook(span, record):
Expand All @@ -82,24 +83,28 @@ def tearDown(self):
super().tearDown()
LoggingInstrumentor().uninstrument()

def assert_trace_context_injected(self, span_id, trace_id):
def assert_trace_context_injected(self, span_id, trace_id, trace_sampled):
with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")
self.assertEqual(len(self.caplog.records), 1)
record = self.caplog.records[0]
self.assertEqual(record.otelSpanID, span_id)
self.assertEqual(record.otelTraceID, trace_id)
self.assertEqual(record.otelTraceSampled, trace_sampled)
self.assertEqual(record.otelServiceName, "unknown_service")

def test_trace_context_injection(self):
with self.tracer.start_as_current_span("s1") as span:
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assert_trace_context_injected(span_id, trace_id)
trace_sampled = span.get_span_context().trace_flags.sampled
self.assert_trace_context_injected(
span_id, trace_id, trace_sampled
)

def test_trace_context_injection_without_span(self):
self.assert_trace_context_injected("0", "0")
self.assert_trace_context_injected("0", "0", False)

@mock.patch("logging.basicConfig")
def test_basic_config_called(self, basic_config_mock):
Expand Down Expand Up @@ -163,6 +168,7 @@ def test_log_hook(self):
with self.tracer.start_as_current_span("s1") as span:
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
trace_sampled = span.get_span_context().trace_flags.sampled
with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")
Expand All @@ -171,6 +177,7 @@ def test_log_hook(self):
self.assertEqual(record.otelSpanID, span_id)
self.assertEqual(record.otelTraceID, trace_id)
self.assertEqual(record.otelServiceName, "unknown_service")
self.assertEqual(record.otelTraceSampled, trace_sampled)
self.assertEqual(
record.custom_user_attribute_from_log_hook, "some-value"
)
Expand All @@ -179,14 +186,18 @@ def test_uninstrumented(self):
with self.tracer.start_as_current_span("s1") as span:
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
self.assert_trace_context_injected(span_id, trace_id)
trace_sampled = span.get_span_context().trace_flags.sampled
self.assert_trace_context_injected(
span_id, trace_id, trace_sampled
)

LoggingInstrumentor().uninstrument()

self.caplog.clear()
with self.tracer.start_as_current_span("s1") as span:
span_id = format(span.get_span_context().span_id, "016x")
trace_id = format(span.get_span_context().trace_id, "032x")
trace_sampled = span.get_span_context().trace_flags.sampled
with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")
Expand All @@ -195,3 +206,4 @@ def test_uninstrumented(self):
self.assertFalse(hasattr(record, "otelSpanID"))
self.assertFalse(hasattr(record, "otelTraceID"))
self.assertFalse(hasattr(record, "otelServiceName"))
self.assertFalse(hasattr(record, "otelTraceSampled"))