diff --git a/opentracing-shim/src/opentracingshim/__init__.py b/opentracing-shim/src/opentracingshim/__init__.py index c7c641f480..c0f59de937 100644 --- a/opentracing-shim/src/opentracingshim/__init__.py +++ b/opentracing-shim/src/opentracingshim/__init__.py @@ -61,12 +61,11 @@ def set_operation_name(self, operation_name): self._otel_span.update_name(operation_name) return self - def finish(self, finish_time=None): - self._otel_span.end() - # TODO: Handle finish_time. The OpenTelemetry API doesn't currently - # support setting end time on a span and we cannot assume that all - # OpenTelemetry Tracer implementations have an `end_time` field. - # https://github.com/open-telemetry/opentelemetry-python/issues/134 + def finish(self, finish_time: float = None): + end_time = finish_time + if end_time is not None: + end_time = util.time_seconds_to_ns(finish_time) + self._otel_span.end(end_time=end_time) def set_tag(self, key, value): self._otel_span.set_attribute(key, value) diff --git a/opentracing-shim/tests/test_shim.py b/opentracing-shim/tests/test_shim.py index 9017df3644..c9a9c06a72 100644 --- a/opentracing-shim/tests/test_shim.py +++ b/opentracing-shim/tests/test_shim.py @@ -119,6 +119,16 @@ def test_explicit_start_time(self): result = util.time_seconds_from_ns(scope.span.unwrap().start_time) self.assertEqual(result, now) + def test_explicit_end_time(self): + """Test `end_time` argument of `finish()` method.""" + + span = self.shim.start_span("TestSpan") + now = time.time() + span.finish(now) + + end_time = util.time_seconds_from_ns(span.unwrap().end_time) + self.assertEqual(end_time, now) + def test_explicit_span_activation(self): """Test manual activation and deactivation of a span."""