Skip to content

Commit

Permalink
Handle HTTP 2XX responses as successful in OTLP exporters (#3623)
Browse files Browse the repository at this point in the history
* Handle HTTP 2XX responses as successful in OTLP exporters

* Add test cases for 2XX HTTP responses

* Add CHANGELOG entry

* Fix lint
  • Loading branch information
gshiva committed Jan 26, 2024
1 parent 4bac02e commit c4d17e9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +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
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
)

0 comments on commit c4d17e9

Please sign in to comment.