def log_kv(self, key_values: Attributes, timestamp: float = None) -> "SpanShim": """Logs an event for the wrapped OpenTelemetry span. Note: The OpenTracing API defines the values of *key_values* to be of any type. However, the OpenTelemetry API requires that the values be any one of the types defined in ``opentelemetry.trace.util.Attributes`` therefore, only these types are supported as values. Args: key_values: A dictionary as specified in ``opentelemetry.trace.util.Attributes``. timestamp: Timestamp of the OpenTelemetry event, will be generated automatically if omitted. Returns: Returns this :class:`SpanShim` instance to allow call chaining. """ if timestamp is not None: event_timestamp = util.time_seconds_to_ns(timestamp) else: event_timestamp = None event_name = util.event_name_from_kv(key_values) self._otel_span.add_event(event_name, key_values, event_timestamp) return self
def test_event_name_from_kv(self): # Test basic behavior. event_name = "send HTTP request" res = util.event_name_from_kv({"event": event_name, "foo": "bar"}) self.assertEqual(res, event_name) # Test None. res = util.event_name_from_kv(None) self.assertEqual(res, util.DEFAULT_EVENT_NAME) # Test empty dict. res = util.event_name_from_kv({}) self.assertEqual(res, util.DEFAULT_EVENT_NAME) # Test missing `event` field. res = util.event_name_from_kv({"foo": "bar"}) self.assertEqual(res, util.DEFAULT_EVENT_NAME)