Exemplo n.º 1
0
 def test_no_service_name(self):
     with self.assertRaises(InvalidConfigurationError):
         with self.assertLogs(logger=_logger, level="ERROR") as log:
             # service_name is set here as None in order to override any
             # possible LS_SERVICE_NAME environment variable that may be set
             configure_opentelemetry(service_name=None)
             self.assertIn("service name missing", log.output[0])
Exemplo n.º 2
0
    def test_hostname(self, mock_resource, mock_gethostname):

        mock_gethostname.return_value = "the_hostname"

        configure_opentelemetry(
            service_name="service_name",
            access_token="a" * 104,
        )

        mock_resource.assert_called_with({
            "telemetry.sdk.language": "python",
            "telemetry.sdk.version": __version__,
            "service.name": "service_name",
            _ATTRIBUTE_HOST_NAME: "the_hostname",
        })

        configure_opentelemetry(
            service_name="service_name",
            access_token="a" * 104,
            resource_attributes="{}=other_hostname".format(
                _ATTRIBUTE_HOST_NAME),
        )

        mock_resource.assert_called_with({
            "service.name":
            "service_name",
            _ATTRIBUTE_HOST_NAME:
            "other_hostname",
        })
Exemplo n.º 3
0
 def test_no_token(self):
     with self.assertRaises(InvalidConfigurationError):
         with self.assertLogs(logger=_logger, level="ERROR") as log:
             # access_token is set here as None in order to override any
             # possible LS_ACCES_TOKEN environment variable that may be set
             configure_opentelemetry(service_name="service-123",
                                     access_token=None)
             self.assertIn("token missing", log.output[0])
Exemplo n.º 4
0
    def test_headers(self, mock_otlp_span_exporter):

        configure_opentelemetry(service_name="service_123",
                                access_token="a" * 104)

        mock_otlp_span_exporter.assert_called_with(
            endpoint="ingest.lightstep.com:443",
            credentials=ANY,
            headers=(("lightstep-access-token", "a" * 104), ),
        )
Exemplo n.º 5
0
    def test_log_level(self):

        with self.assertLogs(logger=_logger, level=DEBUG):
            configure_opentelemetry(
                service_name="service_123",
                access_token="a" * 104,
                log_level="DEBUG",
            )

        with self.assertRaises(AssertionError):
            with self.assertLogs(logger=_logger, level=WARNING):
                configure_opentelemetry(
                    service_name="service_123",
                    access_token="a" * 104,
                    log_level="WARNING",
                )

        with self.assertLogs(logger=_logger, level=DEBUG):
            configure_opentelemetry(
                service_name="service_123",
                access_token="a" * 104,
                log_level="DeBuG",
            )

        with self.assertRaises(AssertionError):
            with self.assertLogs(logger=_logger, level=WARNING):
                configure_opentelemetry(
                    service_name="service_123",
                    access_token="a" * 104,
                    log_level="WaRNiNG",
                )
Exemplo n.º 6
0
    def test_only_service_name_and_token(self, mock_otlp_span_exporter):

        configure_opentelemetry(service_name="service_123",
                                access_token="a" * 104)

        with trace.get_tracer_provider().get_tracer(
                __name__).start_as_current_span("name"):
            pass

        # The worker thread in MockBatchExportSpanProcessor is configured to
        # wait 1ms before exporting. Sleeping here to give enough time to the
        # export method mock to be called.
        sleep(0.002)

        mock_otlp_span_exporter.assert_called()
Exemplo n.º 7
0
    def test_propagation_baggage(self):
        configure_opentelemetry(
            service_name="service_name",
            service_version="service_version",
            access_token="a" * 104,
            propagators="baggage",
        )

        with trace.get_tracer(__name__).start_as_current_span("test"):
            ctx = baggage.set_baggage("abc", "def")
            prop = get_global_textmap()
            carrier = {}
            prop.inject(dict.__setitem__, carrier, context=ctx)
            self.assertIsNone(carrier.get("x-b3-traceid"))
            self.assertEqual(carrier.get("baggage"), "abc=def")
Exemplo n.º 8
0
    def test_propagation_tracecontext(self):
        configure_opentelemetry(
            service_name="service_name",
            service_version="service_version",
            access_token="a" * 104,
            propagators="tracecontext",
        )

        with trace.get_tracer(__name__).start_as_current_span("test") as span:
            ctx = baggage.set_baggage("abc", "def")
            prop = get_global_textmap()
            carrier = {}
            prop.inject(dict.__setitem__, carrier, context=ctx)
            self.assertIn(
                "00-{}".format(format(span.get_span_context().trace_id,
                                      "032x")),
                carrier.get("traceparent"),
            )
Exemplo n.º 9
0
 def test_no_token_other_endpoint(self):
     # no exception is thrown
     configure_opentelemetry(
         service_name="service-123",
         span_exporter_endpoint="localhost:1234",
     )