Skip to content

Commit

Permalink
Add common utilities for OTLP
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Jun 1, 2023
1 parent 3d04f4b commit 5750e54
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions newrelic/common/otlp_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module provides common utilities for interacting with OTLP protocol buffers."""

import logging
_logger = logging.getLogger(__name__)

try:
from newrelic.packages.opentelemetry_proto.common_pb2 import AnyValue, KeyValue
except ImportError:
create_key_value, create_key_values_from_iterable = None, None
else:
def create_key_value(key, value):
if isinstance(value, bool):
return KeyValue(key=key, value=AnyValue(bool_value=value))
elif isinstance(value, int):
return KeyValue(key=key, value=AnyValue(int_value=value))
elif isinstance(value, float):
return KeyValue(key=key, value=AnyValue(double_value=value))
elif isinstance(value, str):
return KeyValue(key=key, value=AnyValue(string_value=value))
# Technically AnyValue accepts array, kvlist, and bytes however, since
# those are not valid custom attribute types according to our api spec,
# we will not bother to support them here either.
else:
_logger.warn("Unsupported ML event attribute value type %s: %s." % (key, value))


def create_key_values_from_iterable(iterable):
if isinstance(iterable, dict):
iterable = iterable.items()

return list(
filter(
lambda i: i is not None,
(create_key_value(key, value) for key, value in iterable),
)
)

0 comments on commit 5750e54

Please sign in to comment.