-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d04f4b
commit 5750e54
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
) |