Skip to content

Commit

Permalink
Group metrics by instrumentation scopes in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed May 12, 2022
1 parent 436dfff commit 5e7d981
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,19 @@ def _translate_data(

instrumentation_scope = scope_metrics.scope

if instrumentation_scope not in (
instrumentation_scope_pb2_scope_metrics
):

# instrumentation_scope comes from the meter, it is never
# None.
instrumentation_scope_pb2_scope_metrics[
instrumentation_scope
] = pb2.ScopeMetrics(
scope=InstrumentationScope(
name=instrumentation_scope.name,
version=instrumentation_scope.version,
)
# The SDK groups metrics in instrumentation scopes already so
# there is no need to check for existing instrumentation scopes
# here.
pb2_scope_metrics = pb2.ScopeMetrics(
scope=InstrumentationScope(
name=instrumentation_scope.name,
version=instrumentation_scope.version,
)
)

pb2_scope_metrics = instrumentation_scope_pb2_scope_metrics[
instrumentation_scope_pb2_scope_metrics[
instrumentation_scope
]
] = pb2_scope_metrics

for metric in scope_metrics.metrics:
pb2_metric = pb2.Metric(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def setUp(self):
version="first_version",
schema_url="insrumentation_scope_schema_url",
),
metrics=[histogram],
metrics=[histogram, histogram],
schema_url="instrumentation_scope_schema_url",
),
ScopeMetrics(
Expand All @@ -280,15 +280,6 @@ def setUp(self):
metrics=[histogram],
schema_url="instrumentation_scope_schema_url",
),
ScopeMetrics(
scope=SDKInstrumentationScope(
name="first_name",
version="first_version",
schema_url="insrumentation_scope_schema_url",
),
metrics=[histogram],
schema_url="instrumentation_scope_schema_url",
),
ScopeMetrics(
scope=SDKInstrumentationScope(
name="third_name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
SdkConfiguration,
)
from opentelemetry.sdk._metrics._internal.view import View
from opentelemetry.sdk.util.instrumentation import InstrumentationScope

_logger = getLogger(__name__)

Expand Down Expand Up @@ -131,6 +132,10 @@ def collect(self) -> MetricsData:

scope_metrics: List[ScopeMetrics] = []

instrumentation_scope_scope_metrics: (
Dict[InstrumentationScope, ScopeMetrics]
) = {}

for (
instrument,
view_instrument_matches,
Expand Down Expand Up @@ -194,13 +199,22 @@ def collect(self) -> MetricsData:
data=data,
)
)
scope_metrics.append(
ScopeMetrics(

if instrument.instrumentation_scope not in (
instrumentation_scope_scope_metrics
):
instrumentation_scope_scope_metrics[
instrument.instrumentation_scope
] = ScopeMetrics(
scope=instrument.instrumentation_scope,
metrics=metrics,
schema_url=instrument.instrumentation_scope.schema_url,
)
)

scope_metrics = instrumentation_scope_scope_metrics[
instrument.instrumentation_scope
]
scope_metrics.metrics.extend(metrics)

return MetricsData(
resource_metrics=[
Expand Down

0 comments on commit 5e7d981

Please sign in to comment.