Skip to content

Commit

Permalink
Merge branch 'main' into bugfix-event-attribute-sequence-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 31, 2024
2 parents 13a67e6 + c4d17e9 commit 5659ccf
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 86 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Handle HTTP 2XX responses as successful in OTLP exporters
([#3623](https://github.com/open-telemetry/opentelemetry-python/pull/3623))
- Improve Resource Detector timeout messaging
([#3645](https://github.com/open-telemetry/opentelemetry-python/pull/3645))

## Version 1.22.0/0.43b0 (2023-12-15)

- Prometheus exporter sanitize info metric ([#3572](https://github.com/open-telemetry/opentelemetry-python/pull/3572))
- Prometheus exporter sanitize info metric
([#3572](https://github.com/open-telemetry/opentelemetry-python/pull/3572))
- Remove Jaeger exporters
([#3554](https://github.com/open-telemetry/opentelemetry-python/pull/3554))
- Log stacktrace on `UNKNOWN` status OTLP export error
Expand Down
2 changes: 0 additions & 2 deletions docs/examples/opencensus-exporter-tracer/collector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
#
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [

[project.optional-dependencies]
test = [
"responses == 0.22.0",
"responses >= 0.22.0, < 0.25",
]

[project.entry-points.opentelemetry_traces_exporter]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:

resp = self._export(serialized_data)
# pylint: disable=no-else-return
if resp.status_code in (200, 202):
if resp.ok:
return LogExportResult.SUCCESS
elif self._retryable(resp):
_logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def export(

resp = self._export(serialized_data.SerializeToString())
# pylint: disable=no-else-return
if resp.status_code in (200, 202):
if resp.ok:
return MetricExportResult.SUCCESS
elif self._retryable(resp):
_logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def export(self, spans) -> SpanExportResult:

resp = self._export(serialized_data)
# pylint: disable=no-else-return
if resp.status_code in (200, 202):
if resp.ok:
return SpanExportResult.SUCCESS
elif self._retryable(resp):
_logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging import WARNING
from os import environ
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import MagicMock, Mock, patch

from requests import Session
from requests.models import Response
Expand Down Expand Up @@ -476,3 +476,14 @@ def test_exponential_explicit_bucket_histogram(self):
OTLPMetricExporter()._preferred_aggregation[Histogram],
ExplicitBucketHistogramAggregation,
)

@patch.object(OTLPMetricExporter, "_export", return_value=Mock(ok=True))
def test_2xx_status_code(self, mock_otlp_metric_exporter):
"""
Test that any HTTP 2XX code returns a successful result
"""

self.assertEqual(
OTLPMetricExporter().export(MagicMock()),
MetricExportResult.SUCCESS,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import unittest
from typing import List
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, Mock, patch

import requests
import responses
Expand All @@ -34,6 +34,7 @@
from opentelemetry.exporter.otlp.proto.http.version import __version__
from opentelemetry.sdk._logs import LogData
from opentelemetry.sdk._logs import LogRecord as SDKLogRecord
from opentelemetry.sdk._logs.export import LogExportResult
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION,
Expand Down Expand Up @@ -262,3 +263,13 @@ def _get_sdk_log_data() -> List[LogData]:
)

return [log1, log2, log3, log4]

@patch.object(OTLPLogExporter, "_export", return_value=Mock(ok=True))
def test_2xx_status_code(self, mock_otlp_metric_exporter):
"""
Test that any HTTP 2XX code returns a successful result
"""

self.assertEqual(
OTLPLogExporter().export(MagicMock()), LogExportResult.SUCCESS
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import unittest
from collections import OrderedDict
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import requests
import responses
Expand Down Expand Up @@ -42,6 +42,7 @@
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
)
from opentelemetry.sdk.trace import _Span
from opentelemetry.sdk.trace.export import SpanExportResult

OS_ENV_ENDPOINT = "os.env.base"
OS_ENV_CERTIFICATE = "os/env/base.crt"
Expand Down Expand Up @@ -239,3 +240,13 @@ def generate_delays(*args, **kwargs):

exporter.export([span])
mock_sleep.assert_called_once_with(1)

@patch.object(OTLPSpanExporter, "_export", return_value=Mock(ok=True))
def test_2xx_status_code(self, mock_otlp_metric_exporter):
"""
Test that any HTTP 2XX code returns a successful result
"""

self.assertEqual(
OTLPSpanExporter().export(MagicMock()), SpanExportResult.SUCCESS
)
10 changes: 3 additions & 7 deletions exporter/opentelemetry-exporter-otlp/tests/test_otlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
Expand All @@ -26,19 +25,16 @@
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter as HTTPSpanExporter,
)
from opentelemetry.test import TestCase


class TestOTLPExporters(unittest.TestCase):
class TestOTLPExporters(TestCase):
def test_constructors(self):
for exporter in [
OTLPSpanExporter,
HTTPSpanExporter,
OTLPLogExporter,
OTLPMetricExporter,
]:
try:
with self.assertNotRaises(Exception):
exporter()
except Exception: # pylint: disable=broad-except
self.fail(
f"Unexpected exception raised when instantiating {exporter.__name__}"
)
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ def get_aggregated_resources(
detected_resource: Resource = _EMPTY_RESOURCE
try:
detected_resource = future.result(timeout=timeout)
except concurrent.futures.TimeoutError as ex:
if detector.raise_on_error:
raise ex
logger.warning(
"Detector %s took longer than %s seconds, skipping",
detector,
timeout,
)
# pylint: disable=broad-except
except Exception as ex:
if detector.raise_on_error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from math import inf
from sys import float_info, version_info
from unittest import TestCase
from unittest.mock import patch

from pytest import mark
Expand All @@ -31,6 +30,7 @@
MIN_NORMAL_EXPONENT,
MIN_NORMAL_VALUE,
)
from opentelemetry.test import TestCase

if version_info >= (3, 9):
from math import nextafter
Expand Down Expand Up @@ -69,12 +69,9 @@ def test_init_called_once(self, mock_init):

def test_exponent_mapping_0(self):

try:
with self.assertNotRaises(Exception):
ExponentMapping(0)

except Exception as error:
self.fail(f"Unexpected exception raised: {error}")

def test_exponent_mapping_zero(self):

exponent_mapping = ExponentMapping(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from math import ldexp
from sys import float_info
from types import MethodType
from unittest import TestCase
from unittest.mock import Mock, patch

from opentelemetry.sdk.metrics._internal.aggregation import (
Expand All @@ -41,6 +40,7 @@
from opentelemetry.sdk.metrics.view import (
ExponentialBucketHistogramAggregation,
)
from opentelemetry.test import TestCase


def get_counts(buckets: Buckets) -> int:
Expand Down Expand Up @@ -793,11 +793,8 @@ def test_boundary_statistics(self):

index = mapping.map_to_index(value)

try:
with self.assertNotRaises(Exception):
boundary = mapping.get_lower_boundary(index + 1)
except Exception as error:
raise error
self.fail(f"Unexpected exception {error} raised")

if boundary < value:
above += 1
Expand Down Expand Up @@ -836,30 +833,6 @@ def test_aggregate_collect(self):
"""
Tests a repeated cycle of aggregation and collection.
"""
"""
try:
exponential_histogram_aggregation = (
_ExponentialBucketHistogramAggregation(
Mock(),
Mock(),
)
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
exponential_histogram_aggregation.aggregate(Measurement(2, Mock()))
exponential_histogram_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
except Exception as error:
self.fail(f"Unexpected exception raised: {error}")
"""
exponential_histogram_aggregation = (
_ExponentialBucketHistogramAggregation(
Mock(),
Expand Down
14 changes: 4 additions & 10 deletions opentelemetry-sdk/tests/metrics/test_backward_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"""

from typing import Iterable, Sequence
from unittest import TestCase

from opentelemetry.metrics import CallbackOptions, Observation
from opentelemetry.sdk.metrics import MeterProvider
Expand All @@ -38,6 +37,7 @@
MetricReader,
PeriodicExportingMetricReader,
)
from opentelemetry.test import TestCase


# Do not change these classes until after major version 1
Expand Down Expand Up @@ -82,30 +82,24 @@ def test_metric_exporter(self):
)
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
meter_provider.shutdown()
except Exception:
self.fail()

def test_metric_reader(self):
reader = OrigMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
meter_provider.shutdown()
except Exception:
self.fail()

def test_observable_callback(self):
reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
# produce some data
meter_provider.get_meter("foo").create_counter("mycounter").add(12)
try:
with self.assertNotRaises(Exception):
metrics_data = reader.get_metrics_data()
except Exception:
self.fail()

self.assertEqual(len(metrics_data.resource_metrics), 1)
self.assertEqual(
Expand Down
14 changes: 4 additions & 10 deletions opentelemetry-sdk/tests/metrics/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# pylint: disable=unused-import

from unittest import TestCase
from opentelemetry.test import TestCase


class TestImport(TestCase):
Expand All @@ -23,7 +23,7 @@ def test_import_init(self):
Test that the metrics root module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics import ( # noqa: F401
Counter,
Histogram,
Expand All @@ -34,15 +34,13 @@ def test_import_init(self):
ObservableUpDownCounter,
UpDownCounter,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")

def test_import_export(self):
"""
Test that the metrics export module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics.export import ( # noqa: F401
AggregationTemporality,
ConsoleMetricExporter,
Expand All @@ -63,15 +61,13 @@ def test_import_export(self):
ScopeMetrics,
Sum,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")

def test_import_view(self):
"""
Test that the metrics view module has the right symbols
"""

try:
with self.assertNotRaises(Exception):
from opentelemetry.sdk.metrics.view import ( # noqa: F401
Aggregation,
DefaultAggregation,
Expand All @@ -81,5 +77,3 @@ def test_import_view(self):
SumAggregation,
View,
)
except Exception as error:
self.fail(f"Unexpected error {error} was raised")

0 comments on commit 5659ccf

Please sign in to comment.