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

Logging: Defensively access provider resource #890

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.9.1-0.28b1...HEAD)

### Fixed

- `opentelemetry-instrumentation-logging` retrieves service name defensively.
([#890](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/890))

## [1.9.1-0.28b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.9.1-0.28b1) - 2022-01-29


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,29 @@ def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
service_name = ""
provider = kwargs.get("tracer_provider", None) or get_tracer_provider()
resource = provider.resource if provider else None
if resource:
service_name = resource.attributes.get("service.name")

provider = kwargs.get("tracer_provider", None) or get_tracer_provider()
old_factory = logging.getLogRecordFactory()
LoggingInstrumentor._old_factory = old_factory

service_name = None

def record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)

record.otelSpanID = "0"
record.otelTraceID = "0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably out of scope, what do you think about setting service name to "" here similar to span, traceid and defensively access resource on the below span instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about accessing resource from span as span API does not define resource at all. ReadableSpan defines resource which should only be used by the SDK. I know somewhat similar argument applies to provider but for some reason I feel doing it with provider is a lesser evil. Happy to hear other ideas/counter-arguments but may be we shold open an issue and discuss there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should discuss it in the next SIG call. Not span vs provider but the fact that an instrumentation depends on an SDK feature which shouldn't be the case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fact that an instrumentation depends on an SDK feature which shouldn't be the case.

Isn't this an exception and I remember there was some discussion around if instrumentation-logging should actually be an instrumentation. I am not aware of any other instrumentation depending sdk feature.


nonlocal service_name
if service_name is None:
resource = getattr(provider, "resource", None)
if resource:
service_name = (
resource.attributes.get("service.name") or ""
)
else:
service_name = ""

record.otelServiceName = service_name

span = get_current_span()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import logging
from typing import Optional
from unittest import mock

import pytest
Expand All @@ -22,7 +23,45 @@
LoggingInstrumentor,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer
from opentelemetry.trace import ProxyTracer, get_tracer


class FakeTracerProvider:
def get_tracer( # pylint: disable=no-self-use
self,
instrumenting_module_name: str,
instrumenting_library_version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> ProxyTracer:
return ProxyTracer(
instrumenting_module_name,
instrumenting_library_version,
schema_url,
)


class TestLoggingInstrumentorProxyTracerProvider(TestBase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self.caplog = caplog # pylint: disable=attribute-defined-outside-init

def setUp(self):
super().setUp()
LoggingInstrumentor().instrument(tracer_provider=FakeTracerProvider())

def tearDown(self):
super().tearDown()
LoggingInstrumentor().uninstrument()

def test_trace_context_injection(self):
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, "0")
self.assertEqual(record.otelTraceID, "0")
self.assertEqual(record.otelServiceName, "")


class TestLoggingInstrumentor(TestBase):
Expand Down