|
| 1 | +# Copyright 2020, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""OpenTelemetry Collector Metrics Exporter.""" |
| 16 | + |
| 17 | +import logging |
| 18 | +from typing import Sequence |
| 19 | + |
| 20 | +import grpc |
| 21 | +from opencensus.proto.agent.metrics.v1 import ( |
| 22 | + metrics_service_pb2, |
| 23 | + metrics_service_pb2_grpc, |
| 24 | +) |
| 25 | +from opencensus.proto.metrics.v1 import metrics_pb2 |
| 26 | + |
| 27 | +import opentelemetry.ext.otcollector.util as utils |
| 28 | +from opentelemetry.sdk.metrics import Counter, Metric |
| 29 | +from opentelemetry.sdk.metrics.export import ( |
| 30 | + MetricRecord, |
| 31 | + MetricsExporter, |
| 32 | + MetricsExportResult, |
| 33 | + aggregate, |
| 34 | +) |
| 35 | + |
| 36 | +DEFAULT_ENDPOINT = "localhost:55678" |
| 37 | + |
| 38 | +logger = logging.getLogger(__name__) |
| 39 | + |
| 40 | + |
| 41 | +# pylint: disable=no-member |
| 42 | +class CollectorMetricsExporter(MetricsExporter): |
| 43 | + """OpenTelemetry Collector metrics exporter. |
| 44 | +
|
| 45 | + Args: |
| 46 | + endpoint: OpenTelemetry Collector OpenCensus receiver endpoint. |
| 47 | + service_name: Name of Collector service. |
| 48 | + host_name: Host name. |
| 49 | + client: MetricsService client stub. |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + endpoint: str = DEFAULT_ENDPOINT, |
| 55 | + service_name: str = None, |
| 56 | + host_name: str = None, |
| 57 | + client: metrics_service_pb2_grpc.MetricsServiceStub = None, |
| 58 | + ): |
| 59 | + self.endpoint = endpoint |
| 60 | + if client is None: |
| 61 | + channel = grpc.insecure_channel(self.endpoint) |
| 62 | + self.client = metrics_service_pb2_grpc.MetricsServiceStub( |
| 63 | + channel=channel |
| 64 | + ) |
| 65 | + else: |
| 66 | + self.client = client |
| 67 | + |
| 68 | + self.node = utils.get_node(service_name, host_name) |
| 69 | + |
| 70 | + def export( |
| 71 | + self, metric_records: Sequence[MetricRecord] |
| 72 | + ) -> MetricsExportResult: |
| 73 | + try: |
| 74 | + responses = self.client.Export( |
| 75 | + self.generate_metrics_requests(metric_records) |
| 76 | + ) |
| 77 | + |
| 78 | + # Read response |
| 79 | + for _ in responses: |
| 80 | + pass |
| 81 | + |
| 82 | + except grpc.RpcError: |
| 83 | + return MetricsExportResult.FAILED_RETRYABLE |
| 84 | + |
| 85 | + return MetricsExportResult.SUCCESS |
| 86 | + |
| 87 | + def shutdown(self) -> None: |
| 88 | + pass |
| 89 | + |
| 90 | + def generate_metrics_requests( |
| 91 | + self, metrics: Sequence[MetricRecord] |
| 92 | + ) -> metrics_service_pb2.ExportMetricsServiceRequest: |
| 93 | + collector_metrics = translate_to_collector(metrics) |
| 94 | + service_request = metrics_service_pb2.ExportMetricsServiceRequest( |
| 95 | + node=self.node, metrics=collector_metrics |
| 96 | + ) |
| 97 | + yield service_request |
| 98 | + |
| 99 | + |
| 100 | +# pylint: disable=too-many-branches |
| 101 | +def translate_to_collector( |
| 102 | + metric_records: Sequence[MetricRecord], |
| 103 | +) -> Sequence[metrics_pb2.Metric]: |
| 104 | + collector_metrics = [] |
| 105 | + for metric_record in metric_records: |
| 106 | + |
| 107 | + label_values = [] |
| 108 | + label_keys = [] |
| 109 | + for label_tuple in metric_record.label_set.labels: |
| 110 | + label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0])) |
| 111 | + label_values.append( |
| 112 | + metrics_pb2.LabelValue( |
| 113 | + has_value=label_tuple[1] is not None, value=label_tuple[1] |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + metric_descriptor = metrics_pb2.MetricDescriptor( |
| 118 | + name=metric_record.metric.name, |
| 119 | + description=metric_record.metric.description, |
| 120 | + unit=metric_record.metric.unit, |
| 121 | + type=get_collector_metric_type(metric_record.metric), |
| 122 | + label_keys=label_keys, |
| 123 | + ) |
| 124 | + |
| 125 | + timeseries = metrics_pb2.TimeSeries( |
| 126 | + label_values=label_values, |
| 127 | + points=[get_collector_point(metric_record)], |
| 128 | + ) |
| 129 | + collector_metrics.append( |
| 130 | + metrics_pb2.Metric( |
| 131 | + metric_descriptor=metric_descriptor, timeseries=[timeseries] |
| 132 | + ) |
| 133 | + ) |
| 134 | + return collector_metrics |
| 135 | + |
| 136 | + |
| 137 | +# pylint: disable=no-else-return |
| 138 | +def get_collector_metric_type(metric: Metric) -> metrics_pb2.MetricDescriptor: |
| 139 | + if isinstance(metric, Counter): |
| 140 | + if metric.value_type == int: |
| 141 | + return metrics_pb2.MetricDescriptor.CUMULATIVE_INT64 |
| 142 | + elif metric.value_type == float: |
| 143 | + return metrics_pb2.MetricDescriptor.CUMULATIVE_DOUBLE |
| 144 | + return metrics_pb2.MetricDescriptor.UNSPECIFIED |
| 145 | + |
| 146 | + |
| 147 | +def get_collector_point(metric_record: MetricRecord) -> metrics_pb2.Point: |
| 148 | + point = metrics_pb2.Point( |
| 149 | + timestamp=utils.proto_timestamp_from_time_ns( |
| 150 | + metric_record.metric.bind( |
| 151 | + metric_record.label_set |
| 152 | + ).last_update_timestamp |
| 153 | + ) |
| 154 | + ) |
| 155 | + if metric_record.metric.value_type == int: |
| 156 | + point.int64_value = metric_record.aggregator.checkpoint |
| 157 | + elif metric_record.metric.value_type == float: |
| 158 | + point.double_value = metric_record.aggregator.checkpoint |
| 159 | + else: |
| 160 | + raise TypeError( |
| 161 | + "Unsupported metric type: {}".format( |
| 162 | + metric_record.metric.value_type |
| 163 | + ) |
| 164 | + ) |
| 165 | + return point |
0 commit comments