Skip to content

Commit

Permalink
Merge branch 'main' into feat/3073
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jun 28, 2023
2 parents 61f0411 + 6559e07 commit abedc9b
Show file tree
Hide file tree
Showing 43 changed files with 540 additions and 705 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
# Otherwise, set variable to the commit of your branch on
# opentelemetry-python-contrib which is compatible with these Core repo
# changes.
CONTRIB_REPO_SHA: 2edd017c22edb4896e182c934bc199d716495ce6
CONTRIB_REPO_SHA: a5ed4da478c4360fd6e24893f7574b150431b7ee
# This is needed because we do not clone the core repo in contrib builds anymore.
# When running contrib builds as part of core builds, we use actions/checkout@v2 which
# does not set an environment variable (simply just runs tox), which is different when
Expand Down
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Add max_scale option to Exponential Bucket Histogram Aggregation
([#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
- Use BoundedAttributes instead of raw dict to extract attributes from LogRecord and Support dropped_attributes_count in LogRecord
([#3310](https://github.com/open-telemetry/opentelemetry-python/pull/3310))

- Add max_scale option to Exponential Bucket Histogram Aggregation
([#3323](https://github.com/open-telemetry/opentelemetry-python/pull/3323))
- Use BoundedAttributes instead of raw dict to extract attributes from LogRecord
([#3310](https://github.com/open-telemetry/opentelemetry-python/pull/3310))
- Support dropped_attributes_count in LogRecord and exporters
([#3351](https://github.com/open-telemetry/opentelemetry-python/pull/3351))
- Add unit to view instrument selection criteria
([#3341](https://github.com/open-telemetry/opentelemetry-python/pull/3341))
- Upgrade opentelemetry-proto to 0.20 and regen
[#3355](https://github.com/open-telemetry/opentelemetry-python/pull/3355))

## Version 1.18.0/0.39b0 (2023-05-04)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Approvers ([@open-telemetry/python-approvers](https://github.com/orgs/open-telem
- [Aaron Abbott](https://github.com/aabmass), Google
- [Jeremy Voss](https://github.com/jeremydvoss), Microsoft
- [Sanket Mehta](https://github.com/sanketmehta28), Cisco
- [Shalev Roda](https://github.com/shalevr), Cisco

Emeritus Approvers

Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ mypy-protobuf~=3.0.0
markupsafe==2.0.1
bleach==4.1.0 # This dependency was updated to a breaking version.
codespell==2.1.0
requests==2.28.1
requests==2.31.0
ruamel.yaml==0.17.21
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _encode_log(log_data: LogData) -> PB2LogRecord:
body=_encode_value(log_data.log_record.body),
severity_text=log_data.log_record.severity_text,
attributes=_encode_attributes(log_data.log_record.attributes),
dropped_attributes_count=log_data.log_record.dropped_attributes,
severity_number=log_data.log_record.severity_number.value,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as PB2Resource,
)
from opentelemetry.sdk._logs import LogData
from opentelemetry.sdk._logs import LogData, LogLimits
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
Expand All @@ -51,6 +51,19 @@ def test_encode(self):
sdk_logs, expected_encoding = self.get_test_logs()
self.assertEqual(encode_logs(sdk_logs), expected_encoding)

def test_dropped_attributes_count(self):
sdk_logs = self._get_test_logs_dropped_attributes()
encoded_logs = encode_logs(sdk_logs)
self.assertTrue(hasattr(sdk_logs[0].log_record, "dropped_attributes"))
self.assertEqual(
# pylint:disable=no-member
encoded_logs.resource_logs[0]
.scope_logs[0]
.log_records[0]
.dropped_attributes_count,
2,
)

@staticmethod
def _get_sdk_log_data() -> List[LogData]:
log1 = LogData(
Expand Down Expand Up @@ -251,3 +264,42 @@ def get_test_logs(
)

return sdk_logs, pb2_service_request

@staticmethod
def _get_test_logs_dropped_attributes() -> List[LogData]:
log1 = LogData(
log_record=SDKLogRecord(
timestamp=1644650195189786880,
trace_id=89564621134313219400156819398935297684,
span_id=1312458408527513268,
trace_flags=TraceFlags(0x01),
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Do not go gentle into that good night. Rage, rage against the dying of the light",
resource=SDKResource({"first_resource": "value"}),
attributes={"a": 1, "b": "c", "user_id": "B121092"},
limits=LogLimits(max_attributes=1),
),
instrumentation_scope=InstrumentationScope(
"first_name", "first_version"
),
)

log2 = LogData(
log_record=SDKLogRecord(
timestamp=1644650249738562048,
trace_id=0,
span_id=0,
trace_flags=TraceFlags.DEFAULT,
severity_text="WARN",
severity_number=SeverityNumber.WARN,
body="Cooper, this is no time for caution!",
resource=SDKResource({"second_resource": "CASE"}),
attributes={},
),
instrumentation_scope=InstrumentationScope(
"second_name", "second_version"
),
)

return [log1, log2]
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ def test_encode_histogram(self):
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
max=18.0,
min=8.0,
)
Expand Down Expand Up @@ -554,7 +553,6 @@ def test_encode_multiple_scope_histogram(self):
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
max=18.0,
min=8.0,
)
Expand Down Expand Up @@ -590,7 +588,6 @@ def test_encode_multiple_scope_histogram(self):
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
max=18.0,
min=8.0,
)
Expand Down Expand Up @@ -633,7 +630,6 @@ def test_encode_multiple_scope_histogram(self):
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
max=18.0,
min=8.0,
)
Expand Down Expand Up @@ -676,7 +672,6 @@ def test_encode_multiple_scope_histogram(self):
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
max=18.0,
min=8.0,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ def _translate_data(
def export(self, batch: Sequence[LogData]) -> LogExportResult:
return self._export(batch)

def shutdown(self) -> None:
pass
def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
OTLPExporterMixin.shutdown(self, timeout_millis=timeout_millis)

def force_flush(self, timeout_millis: float = 10_000) -> bool:
"""Nothing is buffered in this exporter, so this method does nothing."""
return True

@property
def _exporting(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@

import threading
from abc import ABC, abstractmethod
from collections.abc import Sequence
from collections.abc import Sequence # noqa: F401
from logging import getLogger
from os import environ
from time import sleep
from typing import Any, Callable, Dict, Generic, List, Optional, Tuple, Union
from typing import ( # noqa: F401
Any,
Callable,
Dict,
Generic,
List,
Optional,
Tuple,
Union,
)
from typing import Sequence as TypingSequence
from typing import TypeVar
from urllib.parse import urlparse
Expand All @@ -45,7 +54,7 @@
from opentelemetry.exporter.otlp.proto.grpc import (
_OTLP_GRPC_HEADERS,
)
from opentelemetry.proto.common.v1.common_pb2 import (
from opentelemetry.proto.common.v1.common_pb2 import ( # noqa: F401
AnyValue,
ArrayValue,
KeyValue,
Expand Down Expand Up @@ -97,44 +106,6 @@ def environ_to_compression(environ_key: str) -> Optional[Compression]:
return _ENVIRON_TO_COMPRESSION[environ_value]


def _translate_value(value: Any) -> KeyValue:
if isinstance(value, bool):
any_value = AnyValue(bool_value=value)

elif isinstance(value, str):
any_value = AnyValue(string_value=value)

elif isinstance(value, int):
any_value = AnyValue(int_value=value)

elif isinstance(value, float):
any_value = AnyValue(double_value=value)

elif isinstance(value, Sequence):
any_value = AnyValue(
array_value=ArrayValue(values=[_translate_value(v) for v in value])
)

# Tracing specs currently does not support Mapping type attributes
# elif isinstance(value, Mapping):
# any_value = AnyValue(
# kvlist_value=KeyValueList(
# values=[
# _translate_key_values(str(k), v) for k, v in value.items()
# ]
# )
# )

else:
raise Exception(f"Invalid type {type(value)} of value {value}")

return any_value


def _translate_key_values(key: str, value: Any) -> KeyValue:
return KeyValue(key=key, value=_translate_value(value))


@deprecated(
version="1.18.0",
reason="Use one of the encoders from opentelemetry-exporter-otlp-proto-common instead",
Expand Down Expand Up @@ -271,17 +242,6 @@ def _translate_data(
) -> ExportServiceRequestT:
pass

def _translate_attributes(self, attributes) -> TypingSequence[KeyValue]:
output = []
if attributes:

for key, value in attributes.items():
try:
output.append(_translate_key_values(key, value))
except Exception as error: # pylint: disable=broad-except
logger.exception(error)
return output

def _export(
self, data: Union[TypingSequence[ReadableSpan], MetricsData]
) -> ExportResultT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ def _exporting(self) -> str:
return "metrics"

def force_flush(self, timeout_millis: float = 10_000) -> bool:
"""Nothing is buffered in this exporter, so this method does nothing."""
return True
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def shutdown(self) -> None:
OTLPExporterMixin.shutdown(self)

def force_flush(self, timeout_millis: int = 30000) -> bool:
"""Nothing is buffered in this exporter, so this method does nothing."""
return True

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from grpc import ChannelCredentials, Compression, StatusCode, server

from opentelemetry._logs import SeverityNumber
from opentelemetry.exporter.otlp.proto.common._internal import _encode_value
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.exporter import _translate_value
from opentelemetry.exporter.otlp.proto.grpc.version import __version__
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import (
ExportLogsServiceRequest,
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_translate_log_data(self):
16,
"big",
),
body=_translate_value(
body=_encode_value(
"Zhengzhou, We have a heaviest rains in 1000 years"
),
attributes=[
Expand Down Expand Up @@ -426,7 +426,7 @@ def test_translate_multiple_logs(self):
16,
"big",
),
body=_translate_value(
body=_encode_value(
"Zhengzhou, We have a heaviest rains in 1000 years"
),
attributes=[
Expand Down Expand Up @@ -463,13 +463,13 @@ def test_translate_multiple_logs(self):
16,
"big",
),
body=_translate_value(
body=_encode_value(
"Sydney, Opera House is closed"
),
attributes=[
KeyValue(
key="custom_attr",
value=_translate_value([1, 2, 3]),
value=_encode_value([1, 2, 3]),
),
],
flags=int(
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_translate_multiple_logs(self):
16,
"big",
),
body=_translate_value(
body=_encode_value(
"Mumbai, Boil water before drinking"
),
attributes=[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ def test_shutdown(self):
warning.records[0].message,
"Exporter already shutdown, ignoring batch",
)
self.exporter = OTLPMetricExporter()

def test_shutdown_wait_last_export(self):
add_MetricsServiceServicer_to_server(
Expand Down
Loading

0 comments on commit abedc9b

Please sign in to comment.