Skip to content

Commit

Permalink
Add InstrumentationScope and deprecate InstrumentationLibraryInfo (#2583
Browse files Browse the repository at this point in the history
)
  • Loading branch information
srikanthccv authored Apr 21, 2022
1 parent faf8868 commit 7647a11
Show file tree
Hide file tree
Showing 37 changed files with 420 additions and 305 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
instruments to `NoOpCounter`, `NoOpHistogram`, `NoOpObservableCounter`,
`NoOpObservableGauge`, `NoOpObservableUpDownCounter`, `NoOpUpDownCounter`
([#2616](https://github.com/open-telemetry/opentelemetry-python/pull/2616))
- Deprecate InstrumentationLibraryInfo and Add InstrumentationScope
([#2583](https://github.com/open-telemetry/opentelemetry-python/pull/2583))

## [1.11.0-0.30b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.0-0.30b0) - 2022-04-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

NAME_KEY = "otel.library.name"
VERSION_KEY = "otel.library.version"
_SCOPE_NAME_KEY = "otel.scope.name"
_SCOPE_VERSION_KEY = "otel.scope.version"


def _nsec_to_usec_round(nsec: int) -> int:
Expand Down Expand Up @@ -299,15 +301,22 @@ def _extract_tags(
)
)

# Instrumentation info KeyValues
if span.instrumentation_info:
# Instrumentation scope KeyValues
if span.instrumentation_scope:
name = _get_string_key_value(
NAME_KEY, span.instrumentation_info.name
NAME_KEY, span.instrumentation_scope.name
)
version = _get_string_key_value(
VERSION_KEY, span.instrumentation_info.version
VERSION_KEY, span.instrumentation_scope.version
)
scope_name = _get_string_key_value(
_SCOPE_NAME_KEY, span.instrumentation_scope.name
)
scope_version = _get_string_key_value(
_SCOPE_VERSION_KEY, span.instrumentation_scope.version
)
translated.extend([name, version])
translated.extend([scope_name, scope_version])

# Make sure to add "error" tag if span status is not OK
if not span.status.is_ok:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# pylint:disable=import-error
from opentelemetry.exporter.jaeger.proto.grpc.gen import model_pb2
from opentelemetry.exporter.jaeger.proto.grpc.translate import (
_SCOPE_NAME_KEY,
_SCOPE_VERSION_KEY,
NAME_KEY,
VERSION_KEY,
Translate,
Expand All @@ -39,7 +41,7 @@
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SpanExportResult
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.test.spantestutil import (
get_span_with_dropped_attributes_events_links,
)
Expand Down Expand Up @@ -188,7 +190,7 @@ def test_translate_to_jaeger(self):
context=other_context,
parent=None,
resource=Resource({}),
instrumentation_info=InstrumentationInfo(
instrumentation_scope=InstrumentationScope(
name="name", version="version"
),
),
Expand Down Expand Up @@ -391,6 +393,16 @@ def test_translate_to_jaeger(self):
v_type=model_pb2.ValueType.STRING,
v_str="version",
),
model_pb2.KeyValue(
key=_SCOPE_NAME_KEY,
v_type=model_pb2.ValueType.STRING,
v_str="name",
),
model_pb2.KeyValue(
key=_SCOPE_VERSION_KEY,
v_type=model_pb2.ValueType.STRING,
v_str="version",
),
],
process=model_pb2.Process(
service_name="svc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

NAME_KEY = "otel.library.name"
VERSION_KEY = "otel.library.version"
_SCOPE_NAME_KEY = "otel.scope.name"
_SCOPE_VERSION_KEY = "otel.scope.version"


def _nsec_to_usec_round(nsec: int) -> int:
Expand Down Expand Up @@ -220,12 +222,20 @@ def _extract_tags(self, span: ReadableSpan) -> Sequence[TCollector.Tag]:
)

# Instrumentation info tags
if span.instrumentation_info:
name = _get_string_tag(NAME_KEY, span.instrumentation_info.name)
if span.instrumentation_scope:
name = _get_string_tag(NAME_KEY, span.instrumentation_scope.name)
version = _get_string_tag(
VERSION_KEY, span.instrumentation_info.version
VERSION_KEY, span.instrumentation_scope.version
)
scope_name = _get_string_tag(
_SCOPE_NAME_KEY, span.instrumentation_scope.name
)
scope_version = _get_string_tag(
_SCOPE_VERSION_KEY, span.instrumentation_scope.version
)

translated.extend([name, version])
translated.extend([scope_name, scope_version])

# Make sure to add "error" tag if span status is not OK
if not span.status.is_ok:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from opentelemetry.sdk.resources import SERVICE_NAME
from opentelemetry.sdk.trace import Resource, TracerProvider
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.test.globals_test import TraceGlobalsTest
from opentelemetry.test.spantestutil import (
get_span_with_dropped_attributes_events_links,
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_translate_to_jaeger(self):
context=other_context,
parent=None,
resource=Resource({}),
instrumentation_info=InstrumentationInfo(
instrumentation_scope=InstrumentationScope(
name="name", version="version"
),
),
Expand Down Expand Up @@ -461,6 +461,16 @@ def test_translate_to_jaeger(self):
vType=jaeger.TagType.STRING,
vStr="version",
),
jaeger.Tag(
key=jaeger_exporter.translate._SCOPE_NAME_KEY,
vType=jaeger.TagType.STRING,
vStr="name",
),
jaeger.Tag(
key=jaeger_exporter.translate._SCOPE_VERSION_KEY,
vType=jaeger.TagType.STRING,
vStr="version",
),
],
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
from opentelemetry.proto.collector.logs.v1.logs_service_pb2_grpc import (
LogsServiceStub,
)
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationLibrary
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationScope
from opentelemetry.proto.logs.v1.logs_pb2 import (
InstrumentationLibraryLogs,
ScopeLogs,
ResourceLogs,
)
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord
Expand Down Expand Up @@ -96,44 +96,30 @@ def _translate_data(
) -> ExportLogsServiceRequest:
# pylint: disable=attribute-defined-outside-init

sdk_resource_instrumentation_library_logs = {}
sdk_resource_scope_logs = {}

for log_data in data:
resource = log_data.log_record.resource

instrumentation_library_logs_map = (
sdk_resource_instrumentation_library_logs.get(resource, {})
)
if not instrumentation_library_logs_map:
sdk_resource_instrumentation_library_logs[
resource
] = instrumentation_library_logs_map

instrumentation_library_logs = (
instrumentation_library_logs_map.get(
log_data.instrumentation_info
)
)
if not instrumentation_library_logs:
if log_data.instrumentation_info is not None:
instrumentation_library_logs_map[
log_data.instrumentation_info
] = InstrumentationLibraryLogs(
instrumentation_library=InstrumentationLibrary(
name=log_data.instrumentation_info.name,
version=log_data.instrumentation_info.version,
scope_logs_map = sdk_resource_scope_logs.get(resource, {})
if not scope_logs_map:
sdk_resource_scope_logs[resource] = scope_logs_map

scope_logs = scope_logs_map.get(log_data.instrumentation_scope)
if not scope_logs:
if log_data.instrumentation_scope is not None:
scope_logs_map[log_data.instrumentation_scope] = ScopeLogs(
scope=InstrumentationScope(
name=log_data.instrumentation_scope.name,
version=log_data.instrumentation_scope.version,
)
)
else:
instrumentation_library_logs_map[
log_data.instrumentation_info
] = InstrumentationLibraryLogs()

instrumentation_library_logs = (
instrumentation_library_logs_map.get(
log_data.instrumentation_info
)
)
scope_logs_map[
log_data.instrumentation_scope
] = ScopeLogs()

scope_logs = scope_logs_map.get(log_data.instrumentation_scope)

self._collector_kwargs = {}

Expand All @@ -151,13 +137,13 @@ def _translate_data(
"severity_number"
] = log_data.log_record.severity_number.value

instrumentation_library_logs.log_records.append(
scope_logs.log_records.append(
PB2LogRecord(**self._collector_kwargs)
)

return ExportLogsServiceRequest(
resource_logs=get_resource_data(
sdk_resource_instrumentation_library_logs,
sdk_resource_scope_logs,
ResourceLogs,
"logs",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2_grpc import (
MetricsServiceStub,
)
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationLibrary
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationScope
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_METRICS_INSECURE,
Expand Down Expand Up @@ -81,40 +81,30 @@ def __init__(
def _translate_data(
self, data: Sequence[Metric]
) -> ExportMetricsServiceRequest:
sdk_resource_instrumentation_library_metrics = {}
sdk_resource_scope_metrics = {}

for metric in data:
resource = metric.resource
instrumentation_library_map = (
sdk_resource_instrumentation_library_metrics.get(resource, {})
)
if not instrumentation_library_map:
sdk_resource_instrumentation_library_metrics[
resource
] = instrumentation_library_map

instrumentation_library_metrics = instrumentation_library_map.get(
metric.instrumentation_info
)

if not instrumentation_library_metrics:
if metric.instrumentation_info is not None:
instrumentation_library_map[
metric.instrumentation_info
] = pb2.InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name=metric.instrumentation_info.name,
version=metric.instrumentation_info.version,
scope_map = sdk_resource_scope_metrics.get(resource, {})
if not scope_map:
sdk_resource_scope_metrics[resource] = scope_map

scope_metrics = scope_map.get(metric.instrumentation_scope)

if not scope_metrics:
if metric.instrumentation_scope is not None:
scope_map[metric.instrumentation_scope] = pb2.ScopeMetrics(
scope=InstrumentationScope(
name=metric.instrumentation_scope.name,
version=metric.instrumentation_scope.version,
)
)
else:
instrumentation_library_map[
metric.instrumentation_info
] = pb2.InstrumentationLibraryMetrics()
scope_map[
metric.instrumentation_scope
] = pb2.ScopeMetrics()

instrumentation_library_metrics = instrumentation_library_map.get(
metric.instrumentation_info
)
scope_metrics = scope_map.get(metric.instrumentation_scope)

pbmetric = pb2.Metric(
name=metric.name,
Expand Down Expand Up @@ -167,12 +157,12 @@ def _translate_data(
logger.warn("unsupported datapoint type %s", metric.point)
continue

instrumentation_library_metrics.metrics.append(
scope_metrics.metrics.append(
pbmetric,
)
return ExportMetricsServiceRequest(
resource_metrics=get_resource_data(
sdk_resource_instrumentation_library_metrics,
sdk_resource_scope_metrics,
pb2.ResourceMetrics,
"metrics",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ def _translate_key_values(key: str, value: Any) -> KeyValue:


def get_resource_data(
sdk_resource_instrumentation_library_data: Dict[
SDKResource, ResourceDataT
],
sdk_resource_scope_data: Dict[SDKResource, ResourceDataT],
resource_class: Callable[..., TypingResourceT],
name: str,
) -> List[TypingResourceT]:
Expand All @@ -137,8 +135,8 @@ def get_resource_data(

for (
sdk_resource,
instrumentation_library_data,
) in sdk_resource_instrumentation_library_data.items():
scope_data,
) in sdk_resource_scope_data.items():

collector_resource = Resource()

Expand All @@ -156,9 +154,7 @@ def get_resource_data(
resource_class(
**{
"resource": collector_resource,
"instrumentation_library_{}".format(
name
): instrumentation_library_data.values(),
"scope_{}".format(name): scope_data.values(),
}
)
)
Expand Down
Loading

0 comments on commit 7647a11

Please sign in to comment.