Beispiel #1
0
    def test_export(self):
        trace_id = "6e0c63257de34c92bf9efcd03927272e"
        span_id = "95bb5edabd45950f"
        span_datas = [
            Span(
                name="span_name",
                context=SpanContext(
                    trace_id=int(trace_id, 16),
                    span_id=int(span_id, 16),
                    is_remote=False,
                ),
                parent=None,
                kind=SpanKind.INTERNAL,
            )
        ]

        cloud_trace_spans = {
            "name":
            "projects/{}/traces/{}/spans/{}".format(self.project_id, trace_id,
                                                    span_id),
            "span_id":
            span_id,
            "parent_span_id":
            None,
            "display_name":
            TruncatableString(value="span_name", truncated_byte_count=0),
            "attributes":
            ProtoSpan.Attributes(
                attribute_map={
                    "g.co/agent":
                    _format_attribute_value(
                        "opentelemetry-python {}; google-cloud-trace-exporter {}"
                        .format(
                            pkg_resources.get_distribution(
                                "opentelemetry-sdk").version,
                            cloud_trace_version,
                        ))
                }),
            "links":
            None,
            "status":
            None,
            "time_events":
            None,
            "start_time":
            None,
            "end_time":
            None,
        }

        client = mock.Mock()

        exporter = CloudTraceSpanExporter(self.project_id, client=client)

        exporter.export(span_datas)

        client.create_span.assert_called_with(**cloud_trace_spans)
        self.assertTrue(client.create_span.called)
Beispiel #2
0
    def test_export(self):
        trace_id = "6e0c63257de34c92bf9efcd03927272e"
        span_id = "95bb5edabd45950f"

        # Create span and associated data.
        resource_info = Resource(
            {
                "cloud.account.id": 123,
                "host.id": "host",
                "cloud.zone": "US",
                "cloud.provider": "gcp",
                "gcp.resource_type": "gce_instance",
            }
        )
        span = Span(
            name="span_name",
            context=SpanContext(
                trace_id=int(trace_id, 16),
                span_id=int(span_id, 16),
                is_remote=False,
            ),
            parent=None,
            kind=SpanKind.INTERNAL,
            resource=resource_info,
            attributes={"attr_key": "attr_value"},
        )

        # pylint: disable=protected-access
        span._start_time = int(time_ns() - (60 * 1e9))
        span._end_time = time_ns()
        span_data = [span]

        # Setup the trace exporter.
        channel = grpc.insecure_channel(self.address)
        transport = trace_service_grpc_transport.TraceServiceGrpcTransport(
            channel=channel
        )
        client = TraceServiceClient(transport=transport)
        trace_exporter = CloudTraceSpanExporter(self.project_id, client=client)

        # Export the spans and verify the results.
        result = trace_exporter.export(span_data)
        self.assertEqual(result, SpanExportResult.SUCCESS)
    def test_export(self):
        trace_id = "6e0c63257de34c92bf9efcd03927272e"
        span_id = "95bb5edabd45950f"
        resource_info = Resource(
            {
                "cloud.account.id": 123,
                "host.id": "host",
                "cloud.zone": "US",
                "cloud.provider": "gcp",
                "gcp.resource_type": "gce_instance",
            }
        )
        span_datas = [
            Span(
                name="span_name",
                context=SpanContext(
                    trace_id=int(trace_id, 16),
                    span_id=int(span_id, 16),
                    is_remote=False,
                ),
                parent=None,
                kind=SpanKind.INTERNAL,
                resource=resource_info,
                attributes={"attr_key": "attr_value"},
            )
        ]

        cloud_trace_spans = {
            "name": "projects/{}/traces/{}/spans/{}".format(
                self.project_id, trace_id, span_id
            ),
            "span_id": span_id,
            "parent_span_id": None,
            "display_name": TruncatableString(
                value="span_name", truncated_byte_count=0
            ),
            "attributes": ProtoSpan.Attributes(
                attribute_map={
                    "g.co/r/gce_instance/zone": _format_attribute_value("US"),
                    "g.co/r/gce_instance/instance_id": _format_attribute_value(
                        "host"
                    ),
                    "g.co/r/gce_instance/project_id": _format_attribute_value(
                        "123"
                    ),
                    "g.co/agent": _format_attribute_value(
                        "opentelemetry-python {}; google-cloud-trace-exporter {}".format(
                            _strip_characters(
                                pkg_resources.get_distribution(
                                    "opentelemetry-sdk"
                                ).version
                            ),
                            _strip_characters(cloud_trace_version),
                        )
                    ),
                    "attr_key": _format_attribute_value("attr_value"),
                }
            ),
            "links": None,
            "status": None,
            "time_events": None,
            "start_time": None,
            "end_time": None,
        }

        client = mock.Mock()

        exporter = CloudTraceSpanExporter(self.project_id, client=client)

        exporter.export(span_datas)

        self.assertTrue(client.batch_write_spans.called)
        client.batch_write_spans.assert_called_with(
            "projects/{}".format(self.project_id), [cloud_trace_spans]
        )
    def test_export(self):
        resource_info = Resource({
            "cloud.account.id": 123,
            "host.id": "host",
            "cloud.zone": "US",
            "cloud.provider": "gcp",
            "gcp.resource_type": "gce_instance",
        })
        span_datas = [
            Span(
                name="span_name",
                context=SpanContext(
                    trace_id=int(self.example_trace_id, 16),
                    span_id=int(self.example_span_id, 16),
                    is_remote=False,
                ),
                parent=None,
                kind=SpanKind.INTERNAL,
                resource=resource_info,
                attributes={"attr_key": "attr_value"},
            )
        ]

        cloud_trace_spans = {
            "name":
            "projects/{}/traces/{}/spans/{}".format(self.project_id,
                                                    self.example_trace_id,
                                                    self.example_span_id),
            "span_id":
            self.example_span_id,
            "parent_span_id":
            None,
            "display_name":
            TruncatableString(value="span_name", truncated_byte_count=0),
            "attributes":
            ProtoSpan.Attributes(
                attribute_map={
                    "g.co/r/gce_instance/zone":
                    _format_attribute_value("US"),
                    "g.co/r/gce_instance/instance_id":
                    _format_attribute_value("host"),
                    "g.co/r/gce_instance/project_id":
                    _format_attribute_value("123"),
                    "g.co/agent":
                    self.agent_code,
                    "attr_key":
                    _format_attribute_value("attr_value"),
                }),
            "links":
            None,
            "status":
            Status(code=StatusCode.UNSET.value),
            "time_events":
            None,
            "start_time":
            None,
            "end_time":
            None,
            # pylint: disable=no-member
            "span_kind":
            ProtoSpan.SpanKind.INTERNAL,
        }

        client = mock.Mock()

        exporter = CloudTraceSpanExporter(self.project_id, client=client)

        exporter.export(span_datas)

        self.assertTrue(client.batch_write_spans.called)
        client.batch_write_spans.assert_called_with(
            "projects/{}".format(self.project_id), [cloud_trace_spans])