Skip to content

Commit

Permalink
[exporter/otlp] add support for Sum (#2370)
Browse files Browse the repository at this point in the history
* [exporter/otlp] add support for Sum

Adding support for exporting Sum datapoints.

* update based on feedback

* update changelog

* clean up imports

* fix lint

* fix lint

* feedback update
  • Loading branch information
Alex Boten authored Jan 18, 2022
1 parent 1a52e9d commit 06aa564
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 41 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
([#2364](https://github.com/open-telemetry/opentelemetry-python/pull/2364))
- [api] Add `NoOpTracer` and `NoOpTracerProvider`. Marking `_DefaultTracer` and `_DefaultTracerProvider` as deprecated.
([#2363](https://github.com/open-telemetry/opentelemetry-python/pull/2363))
- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add Sum to OTLPMetricExporter
([#2370](https://github.com/open-telemetry/opentelemetry-python/pull/2370))

## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def _translate_data(
self._translate_trace_flags(log_data)
self._translate_body(log_data)
self._translate_severity_text(log_data)
self._translate_attributes(log_data.log_record.attributes)
self._collector_kwargs["attributes"] = self._translate_attributes(
log_data.log_record.attributes
)

self._collector_kwargs[
"severity_number"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from os import environ
from typing import Optional, Sequence
from grpc import ChannelCredentials, Compression
Expand All @@ -25,23 +26,24 @@
MetricsServiceStub,
)
from opentelemetry.proto.common.v1.common_pb2 import InstrumentationLibrary
from opentelemetry.proto.metrics.v1.metrics_pb2 import (
InstrumentationLibraryMetrics,
ResourceMetrics,
)
from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric as PB2Metric
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_METRICS_INSECURE,
)
from opentelemetry.sdk._metrics.point import (
Gauge,
Histogram,
Metric,
Sum,
)

from opentelemetry.sdk._metrics.export import (
MetricExporter,
MetricExportResult,
)

logger = logging.getLogger(__name__)


class OTLPMetricExporter(
MetricExporter,
Expand Down Expand Up @@ -80,7 +82,6 @@ def _translate_data(
self, data: Sequence[Metric]
) -> ExportMetricsServiceRequest:
sdk_resource_instrumentation_library_metrics = {}
self._collector_metric_kwargs = {}

for metric in data:
resource = metric.resource
Expand All @@ -100,7 +101,7 @@ def _translate_data(
if metric.instrumentation_info is not None:
instrumentation_library_map[
metric.instrumentation_info
] = InstrumentationLibraryMetrics(
] = pb2.InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name=metric.instrumentation_info.name,
version=metric.instrumentation_info.version,
Expand All @@ -109,19 +110,56 @@ def _translate_data(
else:
instrumentation_library_map[
metric.instrumentation_info
] = InstrumentationLibraryMetrics()
] = pb2.InstrumentationLibraryMetrics()

instrumentation_library_metrics = instrumentation_library_map.get(
metric.instrumentation_info
)

pbmetric = pb2.Metric(
name=metric.name,
description=metric.description,
unit=metric.unit,
)
if isinstance(metric.point, Gauge):
# TODO: implement gauge
pbmetric.gauge = pb2.Gauge(
data_points=[],
)
elif isinstance(metric.point, Histogram):
# TODO: implement histogram
pbmetric.histogram = pb2.Histogram(
data_points=[],
)
elif isinstance(metric.point, Sum):
pt = pb2.NumberDataPoint(
attributes=self._translate_attributes(metric.attributes),
start_time_unix_nano=metric.point.start_time_unix_nano,
time_unix_nano=metric.point.time_unix_nano,
)
if isinstance(metric.point.value, int):
pt.as_int = metric.point.value
else:
pt.as_double = metric.point.value
# note that because sum is a message type, the fields must be
# set individually rather than instantiating a pb2.Sum and setting
# it once
pbmetric.sum.aggregation_temporality = (
metric.point.aggregation_temporality
)
pbmetric.sum.is_monotonic = metric.point.is_monotonic
pbmetric.sum.data_points.append(pt)
else:
logger.warn("unsupported datapoint type %s", metric.point)
continue

instrumentation_library_metrics.metrics.append(
PB2Metric(**self._collector_metric_kwargs)
pbmetric,
)
return ExportMetricsServiceRequest(
resource_metrics=get_resource_data(
sdk_resource_instrumentation_library_metrics,
ResourceMetrics,
pb2.ResourceMetrics,
"metrics",
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,16 @@ def _translate_data(
) -> ExportServiceRequestT:
pass

def _translate_attributes(self, attributes) -> None:
def _translate_attributes(self, attributes) -> TypingSequence[KeyValue]:
output = []
if attributes:

self._collector_kwargs["attributes"] = []

for key, value in attributes.items():

try:
self._collector_kwargs["attributes"].append(
_translate_key_values(key, value)
)
output.append(_translate_key_values(key, value))
except Exception as error: # pylint: disable=broad-except
logger.exception(error)
return output

def _export(self, data: TypingSequence[SDKDataT]) -> ExportResultT:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ def _translate_data(
self._translate_trace_id(sdk_span)
self._translate_parent(sdk_span)
self._translate_context_trace_state(sdk_span)
self._translate_attributes(sdk_span.attributes)
self._collector_kwargs["attributes"] = self._translate_attributes(
sdk_span.attributes
)
self._translate_events(sdk_span)
self._translate_links(sdk_span)
self._translate_status(sdk_span)
Expand Down
Loading

0 comments on commit 06aa564

Please sign in to comment.