Skip to content

Filter duplicate logs out of some logger's logs that might otherwise endlessly log #4695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Overwrite logging.config.fileConfig and logging.config.dictConfig to ensure
the OTLP `LogHandler` remains attached to the root logger. Fix a bug that
can cause a deadlock to occur over `logging._lock` in some cases ([#4636](https://github.com/open-telemetry/opentelemetry-python/pull/4636)).
- Filter duplicate logs emitted from the OTLP exporters to avoid endlessly logging when the OTLP logger itself
is failing to export logs.

## Version 1.35.0/0.56b0 (2025-07-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import annotations

import logging
import time
from collections.abc import Sequence
from typing import (
Any,
Expand Down Expand Up @@ -51,6 +52,21 @@
_ResourceDataT = TypeVar("_ResourceDataT")


class DuplicateFilter(logging.Filter):
def filter(self, record):
current_log = (
record.module,
record.levelno,
record.msg,
time.time() // 60,
)
if current_log != getattr(self, "last_log", None):
self.last_log = current_log
return True
# False means python's `logging` module will no longer process this log.
return False


def _encode_instrumentation_scope(
instrumentation_scope: InstrumentationScope,
) -> PB2InstrumentationScope:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging
import unittest

from opentelemetry.exporter.otlp.proto.common._internal import (
DuplicateFilter,
)


class TestCommonFuncs(unittest.TestCase):
def test_duplicate_logs_filter_works(self):
test_logger = logging.getLogger("testLogger")
test_logger.addFilter(DuplicateFilter())
with self.assertLogs("testLogger") as cm:
test_logger.info("message")
test_logger.info("message")
self.assertEqual(len(cm.output), 1)
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
ssl_channel_credentials,
)
from opentelemetry.exporter.otlp.proto.common._internal import (
DuplicateFilter,
_get_resource_data,
)
from opentelemetry.exporter.otlp.proto.grpc import (
Expand Down Expand Up @@ -87,6 +88,8 @@
)
_MAX_RETRYS = 6
logger = getLogger(__name__)
# This prevents logs generated when a log fails to be written to generate another log which fails to be written etc. etc.
logger.addFilter(DuplicateFilter())
SDKDataT = TypeVar("SDKDataT")
ResourceDataT = TypeVar("ResourceDataT")
TypingResourceT = TypeVar("TypingResourceT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import requests
from requests.exceptions import ConnectionError

from opentelemetry.exporter.otlp.proto.common._internal import (
DuplicateFilter,
)
from opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs
from opentelemetry.exporter.otlp.proto.http import (
_OTLP_HTTP_HEADERS,
Expand Down Expand Up @@ -56,6 +59,8 @@
from opentelemetry.util.re import parse_env_headers

_logger = logging.getLogger(__name__)
# This prevents logs generated when a log fails to be written to generate another log which fails to be written etc. etc.
_logger.addFilter(DuplicateFilter())


DEFAULT_COMPRESSION = Compression.NoCompression
Expand Down
Loading