Exemplo n.º 1
0
    def test_translate_updowncounter_export_record(self, mock_time_ns):
        mock_time_ns.configure_mock(**{"return_value": 1})

        counter_export_record = ExportRecord(
            UpDownCounter("c", "d", "e", int, self.meter),
            [("g", "h")],
            SumAggregator(),
            self.resource,
        )

        counter_export_record.aggregator.checkpoint = 1
        counter_export_record.aggregator.initial_checkpoint_timestamp = 1
        counter_export_record.aggregator.last_update_timestamp = 1

        expected = ExportMetricsServiceRequest(resource_metrics=[
            ResourceMetrics(
                resource=OTLPResource(attributes=[
                    KeyValue(key="a", value=AnyValue(int_value=1)),
                    KeyValue(key="b", value=AnyValue(bool_value=False)),
                ]),
                instrumentation_library_metrics=[
                    InstrumentationLibraryMetrics(
                        instrumentation_library=InstrumentationLibrary(
                            name="name",
                            version="version",
                        ),
                        metrics=[
                            OTLPMetric(
                                name="c",
                                description="d",
                                unit="e",
                                int_sum=IntSum(
                                    data_points=[
                                        IntDataPoint(
                                            labels=[
                                                StringKeyValue(key="g",
                                                               value="h")
                                            ],
                                            value=1,
                                            time_unix_nano=1,
                                            start_time_unix_nano=1,
                                        )
                                    ],
                                    aggregation_temporality=(
                                        AggregationTemporality.
                                        AGGREGATION_TEMPORALITY_CUMULATIVE),
                                ),
                            )
                        ],
                    )
                ],
            )
        ])

        # pylint: disable=protected-access
        actual = self.exporter._translate_data([counter_export_record])

        self.assertEqual(expected, actual)
 def test_translate_sum_double(self):
     expected = ExportMetricsServiceRequest(
         resource_metrics=[
             pb2.ResourceMetrics(
                 resource=OTLPResource(
                     attributes=[
                         KeyValue(key="a", value=AnyValue(int_value=1)),
                         KeyValue(
                             key="b", value=AnyValue(bool_value=False)
                         ),
                     ]
                 ),
                 instrumentation_library_metrics=[
                     pb2.InstrumentationLibraryMetrics(
                         instrumentation_library=InstrumentationLibrary(
                             name="first_name", version="first_version"
                         ),
                         metrics=[
                             pb2.Metric(
                                 name="sum_double",
                                 unit="s",
                                 description="foo",
                                 sum=pb2.Sum(
                                     data_points=[
                                         pb2.NumberDataPoint(
                                             attributes=[
                                                 KeyValue(
                                                     key="a",
                                                     value=AnyValue(
                                                         int_value=1
                                                     ),
                                                 ),
                                                 KeyValue(
                                                     key="b",
                                                     value=AnyValue(
                                                         bool_value=True
                                                     ),
                                                 ),
                                             ],
                                             start_time_unix_nano=1641946015139533244,
                                             time_unix_nano=1641946016139533244,
                                             as_double=2.98,
                                         )
                                     ],
                                     aggregation_temporality=AggregationTemporality.CUMULATIVE,
                                     is_monotonic=True,
                                 ),
                             )
                         ],
                     )
                 ],
             )
         ]
     )
     # pylint: disable=protected-access
     actual = self.exporter._translate_data([self.metrics["sum_double"]])
     self.assertEqual(expected, actual)
 def test_translate_gauge_double(self):
     expected = ExportMetricsServiceRequest(
         resource_metrics=[
             pb2.ResourceMetrics(
                 resource=OTLPResource(
                     attributes=[
                         KeyValue(key="a", value=AnyValue(int_value=1)),
                         KeyValue(
                             key="b", value=AnyValue(bool_value=False)
                         ),
                     ]
                 ),
                 instrumentation_library_metrics=[
                     pb2.InstrumentationLibraryMetrics(
                         instrumentation_library=InstrumentationLibrary(
                             name="first_name", version="first_version"
                         ),
                         metrics=[
                             pb2.Metric(
                                 name="gauge_double",
                                 unit="s",
                                 description="foo",
                                 gauge=pb2.Gauge(
                                     data_points=[
                                         pb2.NumberDataPoint(
                                             attributes=[
                                                 KeyValue(
                                                     key="a",
                                                     value=AnyValue(
                                                         int_value=1
                                                     ),
                                                 ),
                                                 KeyValue(
                                                     key="b",
                                                     value=AnyValue(
                                                         bool_value=True
                                                     ),
                                                 ),
                                             ],
                                             time_unix_nano=1641946016139533244,
                                             as_double=52.028,
                                         )
                                     ],
                                 ),
                             )
                         ],
                     )
                 ],
             )
         ]
     )
     # pylint: disable=protected-access
     actual = self.exporter._translate_data([self.metrics["gauge_double"]])
     self.assertEqual(expected, actual)
Exemplo n.º 4
0
    def test_translate_log_data(self):

        expected = ExportLogsServiceRequest(resource_logs=[
            ResourceLogs(
                resource=OTLPResource(attributes=[
                    KeyValue(key="key", value=AnyValue(string_value="value")),
                ]),
                instrumentation_library_logs=[
                    InstrumentationLibraryLogs(
                        instrumentation_library=InstrumentationLibrary(
                            name="first_name", version="first_version"),
                        logs=[
                            PB2LogRecord(
                                # pylint: disable=no-member
                                name="name",
                                time_unix_nano=self.log_data_1.log_record.
                                timestamp,
                                severity_number=self.log_data_1.log_record.
                                severity_number.value,
                                severity_text="WARNING",
                                span_id=int.to_bytes(5213367945872657620, 8,
                                                     "big"),
                                trace_id=int.to_bytes(
                                    2604504634922341076776623263868986797,
                                    16,
                                    "big",
                                ),
                                body=_translate_value(
                                    "Zhengzhou, We have a heaviest rains in 1000 years"
                                ),
                                attributes=[
                                    KeyValue(
                                        key="a",
                                        value=AnyValue(int_value=1),
                                    ),
                                    KeyValue(
                                        key="b",
                                        value=AnyValue(string_value="c"),
                                    ),
                                ],
                                flags=int(
                                    self.log_data_1.log_record.trace_flags),
                            )
                        ],
                    )
                ],
            ),
        ])

        # pylint: disable=protected-access
        self.assertEqual(expected,
                         self.exporter._translate_data([self.log_data_1]))
Exemplo n.º 5
0
def _translate_key_values(key: Text, value: Any) -> KeyValue:

    if isinstance(value, bool):
        any_value = AnyValue(bool_value=value)

    elif isinstance(value, str):
        any_value = AnyValue(string_value=value)

    elif isinstance(value, int):
        any_value = AnyValue(int_value=value)

    elif isinstance(value, float):
        any_value = AnyValue(double_value=value)

    elif isinstance(value, Sequence):
        any_value = AnyValue(array_value=value)

    elif isinstance(value, Mapping):
        any_value = AnyValue(kvlist_value=value)

    else:
        raise Exception("Invalid type {} of value {}".format(
            type(value), value))

    return KeyValue(key=key, value=any_value)
    def test_translate_metrics(self, mock_time_ns):
        # pylint: disable=no-member

        mock_time_ns.configure_mock(**{"return_value": 1})

        self.counter_metric_record.aggregator.checkpoint = 1
        self.counter_metric_record.aggregator.initial_checkpoint_timestamp = 1
        self.counter_metric_record.aggregator.last_update_timestamp = 1

        expected = ExportMetricsServiceRequest(resource_metrics=[
            ResourceMetrics(
                resource=OTLPResource(attributes=[
                    KeyValue(key="a", value=AnyValue(int_value=1)),
                    KeyValue(key="b", value=AnyValue(bool_value=False)),
                ]),
                instrumentation_library_metrics=[
                    InstrumentationLibraryMetrics(metrics=[
                        OTLPMetric(
                            name="c",
                            description="d",
                            unit="e",
                            int_sum=IntSum(
                                data_points=[
                                    IntDataPoint(
                                        labels=[
                                            StringKeyValue(key="g", value="h")
                                        ],
                                        value=1,
                                        time_unix_nano=1,
                                        start_time_unix_nano=1,
                                    )
                                ],
                                aggregation_temporality=(
                                    AggregationTemporality.
                                    AGGREGATION_TEMPORALITY_DELTA),
                                is_monotonic=True,
                            ),
                        )
                    ])
                ],
            )
        ])

        # pylint: disable=protected-access
        actual = self.exporter._translate_data([self.counter_metric_record])

        self.assertEqual(expected, actual)
    def test_translate_metrics(self):
        # pylint: disable=no-member

        self.counter_metric_record.instrument.add(1, OrderedDict([("a", "b")]))

        expected = ExportMetricsServiceRequest(resource_metrics=[
            ResourceMetrics(
                resource=OTLPResource(attributes=[
                    KeyValue(key="a", value=AnyValue(int_value=1)),
                    KeyValue(key="b", value=AnyValue(bool_value=False)),
                ]),
                instrumentation_library_metrics=[
                    InstrumentationLibraryMetrics(metrics=[
                        OTLPMetric(
                            name="a",
                            description="b",
                            unit="c",
                            int_sum=IntSum(
                                data_points=[
                                    IntDataPoint(
                                        labels=[
                                            StringKeyValue(key="a", value="b")
                                        ],
                                        value=1,
                                    )
                                ],
                                aggregation_temporality=(
                                    AggregationTemporality.
                                    AGGREGATION_TEMPORALITY_DELTA),
                                is_monotonic=True,
                            ),
                        )
                    ])
                ],
            )
        ])

        # pylint: disable=protected-access
        actual = self.exporter._translate_data([self.counter_metric_record])

        self.assertEqual(expected, actual)
    def test_translate_metrics(self):
        # pylint: disable=no-member

        self.counter_metric_record.instrument.add(1, OrderedDict([("a", "b")]))

        expected = ExportMetricsServiceRequest(resource_metrics=[
            ResourceMetrics(
                resource=CollectorResource(attributes=[
                    KeyValue(key="a", value=AnyValue(int_value=1)),
                    KeyValue(key="b", value=AnyValue(bool_value=False)),
                ]),
                instrumentation_library_metrics=[
                    InstrumentationLibraryMetrics(metrics=[
                        CollectorMetric(
                            metric_descriptor=MetricDescriptor(
                                name="a",
                                description="b",
                                unit="c",
                                type=MetricDescriptor.Type.INT64,
                                temporality=(
                                    MetricDescriptor.Temporality.DELTA),
                            ),
                            int64_data_points=[
                                Int64DataPoint(
                                    labels=[
                                        StringKeyValue(key="a", value="b")
                                    ],
                                    value=1,
                                )
                            ],
                        )
                    ])
                ],
            )
        ])

        # pylint: disable=protected-access
        actual = self.exporter._translate_data([self.counter_metric_record])

        self.assertEqual(expected, actual)
Exemplo n.º 9
0
    def test_translate_spans(self):

        expected = ExportTraceServiceRequest(
            resource_spans=[
                ResourceSpans(
                    resource=CollectorResource(
                        attributes=[
                            KeyValue(key="a", value=AnyValue(int_value=1)),
                            KeyValue(
                                key="b", value=AnyValue(bool_value=False)
                            ),
                        ]
                    ),
                    instrumentation_library_spans=[
                        InstrumentationLibrarySpans(
                            spans=[
                                CollectorSpan(
                                    # pylint: disable=no-member
                                    name="a",
                                    start_time_unix_nano=self.span.start_time,
                                    end_time_unix_nano=self.span.end_time,
                                    trace_state="a=b,c=d",
                                    span_id=int.to_bytes(
                                        10217189687419569865, 8, "big"
                                    ),
                                    trace_id=int.to_bytes(
                                        67545097771067222548457157018666467027,
                                        16,
                                        "big",
                                    ),
                                    parent_span_id=(
                                        b"\000\000\000\000\000\00009"
                                    ),
                                    kind=CollectorSpan.SpanKind.INTERNAL,
                                    attributes=[
                                        KeyValue(
                                            key="a",
                                            value=AnyValue(int_value=1),
                                        ),
                                        KeyValue(
                                            key="b",
                                            value=AnyValue(bool_value=True),
                                        ),
                                    ],
                                    events=[
                                        CollectorSpan.Event(
                                            name="a",
                                            time_unix_nano=1591240820506462784,
                                            attributes=[
                                                KeyValue(
                                                    key="a",
                                                    value=AnyValue(
                                                        int_value=1
                                                    ),
                                                ),
                                                KeyValue(
                                                    key="b",
                                                    value=AnyValue(
                                                        bool_value=False
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                    status=Status(code=0, message=""),
                                    links=[
                                        CollectorSpan.Link(
                                            trace_id=int.to_bytes(
                                                1, 16, "big"
                                            ),
                                            span_id=int.to_bytes(2, 8, "big"),
                                            attributes=[
                                                KeyValue(
                                                    key="a",
                                                    value=AnyValue(
                                                        int_value=1
                                                    ),
                                                ),
                                                KeyValue(
                                                    key="b",
                                                    value=AnyValue(
                                                        bool_value=False
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                )
                            ]
                        )
                    ],
                ),
            ]
        )

        # pylint: disable=protected-access
        self.assertEqual(expected, self.exporter._translate_data([self.span]))
Exemplo n.º 10
0
    def test_translate_multiple_logs(self):
        expected = ExportLogsServiceRequest(resource_logs=[
            ResourceLogs(
                resource=OTLPResource(attributes=[
                    KeyValue(key="key", value=AnyValue(string_value="value")),
                ]),
                instrumentation_library_logs=[
                    InstrumentationLibraryLogs(
                        instrumentation_library=InstrumentationLibrary(
                            name="first_name", version="first_version"),
                        logs=[
                            PB2LogRecord(
                                # pylint: disable=no-member
                                name="name",
                                time_unix_nano=self.log_data_1.log_record.
                                timestamp,
                                severity_number=self.log_data_1.log_record.
                                severity_number.value,
                                severity_text="WARNING",
                                span_id=int.to_bytes(5213367945872657620, 8,
                                                     "big"),
                                trace_id=int.to_bytes(
                                    2604504634922341076776623263868986797,
                                    16,
                                    "big",
                                ),
                                body=_translate_value(
                                    "Zhengzhou, We have a heaviest rains in 1000 years"
                                ),
                                attributes=[
                                    KeyValue(
                                        key="a",
                                        value=AnyValue(int_value=1),
                                    ),
                                    KeyValue(
                                        key="b",
                                        value=AnyValue(string_value="c"),
                                    ),
                                ],
                                flags=int(
                                    self.log_data_1.log_record.trace_flags),
                            )
                        ],
                    ),
                    InstrumentationLibraryLogs(
                        instrumentation_library=InstrumentationLibrary(
                            name="second_name", version="second_version"),
                        logs=[
                            PB2LogRecord(
                                # pylint: disable=no-member
                                name="info name",
                                time_unix_nano=self.log_data_2.log_record.
                                timestamp,
                                severity_number=self.log_data_2.log_record.
                                severity_number.value,
                                severity_text="INFO",
                                span_id=int.to_bytes(5213367945872657623, 8,
                                                     "big"),
                                trace_id=int.to_bytes(
                                    2604504634922341076776623263868986799,
                                    16,
                                    "big",
                                ),
                                body=_translate_value(
                                    "Sydney, Opera House is closed"),
                                attributes=[
                                    KeyValue(
                                        key="custom_attr",
                                        value=_translate_value([1, 2, 3]),
                                    ),
                                ],
                                flags=int(
                                    self.log_data_2.log_record.trace_flags),
                            )
                        ],
                    ),
                ],
            ),
            ResourceLogs(
                resource=OTLPResource(attributes=[
                    KeyValue(
                        key="service",
                        value=AnyValue(string_value="myapp"),
                    ),
                ]),
                instrumentation_library_logs=[
                    InstrumentationLibraryLogs(
                        instrumentation_library=InstrumentationLibrary(
                            name="third_name", version="third_version"),
                        logs=[
                            PB2LogRecord(
                                # pylint: disable=no-member
                                name="error name",
                                time_unix_nano=self.log_data_3.log_record.
                                timestamp,
                                severity_number=self.log_data_3.log_record.
                                severity_number.value,
                                severity_text="ERROR",
                                span_id=int.to_bytes(5213367945872657628, 8,
                                                     "big"),
                                trace_id=int.to_bytes(
                                    2604504634922341076776623263868986800,
                                    16,
                                    "big",
                                ),
                                body=_translate_value(
                                    "Mumbai, Boil water before drinking"),
                                attributes=[],
                                flags=int(
                                    self.log_data_3.log_record.trace_flags),
                            )
                        ],
                    )
                ],
            ),
        ])

        # pylint: disable=protected-access
        self.assertEqual(
            expected,
            self.exporter._translate_data(
                [self.log_data_1, self.log_data_2, self.log_data_3]),
        )
 def test_translate_histogram(self):
     expected = ExportMetricsServiceRequest(
         resource_metrics=[
             pb2.ResourceMetrics(
                 resource=OTLPResource(
                     attributes=[
                         KeyValue(key="a", value=AnyValue(int_value=1)),
                         KeyValue(
                             key="b", value=AnyValue(bool_value=False)
                         ),
                     ]
                 ),
                 instrumentation_library_metrics=[
                     pb2.InstrumentationLibraryMetrics(
                         instrumentation_library=InstrumentationLibrary(
                             name="first_name", version="first_version"
                         ),
                         metrics=[
                             pb2.Metric(
                                 name="histogram",
                                 unit="s",
                                 description="foo",
                                 histogram=pb2.Histogram(
                                     data_points=[
                                         pb2.HistogramDataPoint(
                                             attributes=[
                                                 KeyValue(
                                                     key="a",
                                                     value=AnyValue(
                                                         int_value=1
                                                     ),
                                                 ),
                                                 KeyValue(
                                                     key="b",
                                                     value=AnyValue(
                                                         bool_value=True
                                                     ),
                                                 ),
                                             ],
                                             start_time_unix_nano=1641946016139533244,
                                             time_unix_nano=1641946016139533244,
                                             count=5,
                                             sum=67,
                                             bucket_counts=[1, 4],
                                             explicit_bounds=[10.0, 20.0],
                                             exemplars=[],
                                             flags=pb2.DataPointFlags.FLAG_NONE,
                                         )
                                     ],
                                     aggregation_temporality=AggregationTemporality.DELTA,
                                 ),
                             )
                         ],
                     )
                 ],
             )
         ]
     )
     # pylint: disable=protected-access
     actual = self.exporter._translate_data([self.metrics["histogram"]])
     self.assertEqual(expected, actual)
Exemplo n.º 12
0
def _translate_key_values(key: str, value: Any) -> KeyValue:
    return KeyValue(key=key, value=_translate_value(value))