-
Notifications
You must be signed in to change notification settings - Fork 814
feat(logs): implement log creation metric #4935
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| from opentelemetry.attributes import _VALID_ANY_VALUE_TYPES, BoundedAttributes | ||
| from opentelemetry.context import get_current | ||
| from opentelemetry.context.context import Context | ||
| from opentelemetry.metrics import MeterProvider, get_meter_provider | ||
| from opentelemetry.sdk.environment_variables import ( | ||
| OTEL_ATTRIBUTE_COUNT_LIMIT, | ||
| OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, | ||
|
|
@@ -58,6 +59,8 @@ | |
| ) | ||
| from opentelemetry.util.types import AnyValue, _ExtendedAttributes | ||
|
|
||
| from ._logger_metrics import LoggerMetrics | ||
|
|
||
| _DEFAULT_OTEL_ATTRIBUTE_COUNT_LIMIT = 128 | ||
| _ENV_VALUE_UNSET = "" | ||
|
|
||
|
|
@@ -600,6 +603,8 @@ def __init__( | |
| ConcurrentMultiLogRecordProcessor, | ||
| ], | ||
| instrumentation_scope: InstrumentationScope, | ||
| *, | ||
| logger_metrics: LoggerMetrics, | ||
| ): | ||
| super().__init__( | ||
| instrumentation_scope.name, | ||
|
|
@@ -610,6 +615,7 @@ def __init__( | |
| self._resource = resource | ||
| self._multi_log_record_processor = multi_log_record_processor | ||
| self._instrumentation_scope = instrumentation_scope | ||
| self._logger_metrics = logger_metrics | ||
|
|
||
| @property | ||
| def resource(self): | ||
|
|
@@ -662,6 +668,7 @@ def emit( | |
| instrumentation_scope=self._instrumentation_scope, | ||
| ) | ||
|
|
||
| self._logger_metrics.emit_log() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe call this |
||
| self._multi_log_record_processor.on_emit(writable_record) | ||
|
|
||
|
|
||
|
|
@@ -673,6 +680,8 @@ def __init__( | |
| multi_log_record_processor: SynchronousMultiLogRecordProcessor | ||
| | ConcurrentMultiLogRecordProcessor | ||
| | None = None, | ||
| *, | ||
| meter_provider: MeterProvider | None = None, | ||
| ): | ||
| if resource is None: | ||
| self._resource = Resource.create({}) | ||
|
|
@@ -681,6 +690,9 @@ def __init__( | |
| self._multi_log_record_processor = ( | ||
| multi_log_record_processor or SynchronousMultiLogRecordProcessor() | ||
| ) | ||
| self._logger_metrics = LoggerMetrics( | ||
| meter_provider or get_meter_provider() | ||
| ) | ||
| disabled = environ.get(OTEL_SDK_DISABLED, "") | ||
| self._disabled = disabled.lower().strip() == "true" | ||
| self._at_exit_handler = None | ||
|
|
@@ -709,6 +721,7 @@ def _get_logger_no_cache( | |
| schema_url, | ||
| attributes, | ||
| ), | ||
| logger_metrics=self._logger_metrics, | ||
| ) | ||
|
|
||
| def _get_logger_cached( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| # Copyright The OpenTelemetry Authors | ||||||
| # | ||||||
| # 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. | ||||||
|
|
||||||
| from opentelemetry import metrics as metrics_api | ||||||
| from opentelemetry.semconv._incubating.metrics.otel_metrics import ( | ||||||
| create_otel_sdk_log_created, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class LoggerMetrics: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could make this a bit more descriptive, something like Let me know what you think |
||||||
| def __init__(self, meter_provider: metrics_api.MeterProvider) -> None: | ||||||
| meter = meter_provider.get_meter("opentelemetry-sdk") | ||||||
| self._created_logs = create_otel_sdk_log_created(meter) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
|
|
||||||
| def emit_log(self) -> None: | ||||||
| self._created_logs.add(1) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,9 +116,9 @@ class ConsoleLogRecordExporter(LogRecordExporter): | |
| def __init__( | ||
| self, | ||
| out: IO = sys.stdout, | ||
| formatter: Callable[ | ||
| [ReadableLogRecord], str | ||
| ] = lambda record: record.to_json() + linesep, | ||
| formatter: Callable[[ReadableLogRecord], str] = lambda record: ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a newer ruff installed? |
||
| record.to_json() + linesep | ||
| ), | ||
| ): | ||
| self.out = out | ||
| self.formatter = formatter | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # 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. | ||
|
|
||
| from unittest import TestCase | ||
|
|
||
| from opentelemetry.sdk._logs import LoggerProvider | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.metrics.export import InMemoryMetricReader | ||
|
|
||
|
|
||
| class TestLoggerProviderMetrics(TestCase): | ||
| def setUp(self): | ||
| self.metric_reader = InMemoryMetricReader() | ||
| self.meter_provider = MeterProvider( | ||
| metric_readers=[self.metric_reader] | ||
| ) | ||
|
|
||
| def tearDown(self): | ||
| self.meter_provider.shutdown() | ||
|
|
||
| def assert_created_logs(self, metric_data, value, attrs): | ||
| metrics = metric_data.resource_metrics[0].scope_metrics[0].metrics | ||
| created_logs_metric = next( | ||
| (m for m in metrics if m.name == "otel.sdk.log.created"), None | ||
| ) | ||
| self.assertIsNotNone(created_logs_metric) | ||
| self.assertEqual(created_logs_metric.data.data_points[0].value, value) | ||
| self.assertDictEqual( | ||
| created_logs_metric.data.data_points[0].attributes, attrs | ||
| ) | ||
|
|
||
| def test_create_logs(self): | ||
| logger_provider = LoggerProvider(meter_provider=self.meter_provider) | ||
| logger = logger_provider.get_logger("test") | ||
| logger.emit(body="log1") | ||
| metric_data = self.metric_reader.get_metrics_data() | ||
| self.assert_created_logs( | ||
| metric_data, | ||
| 1, | ||
| {}, | ||
| ) | ||
| logger.emit(body="log2") | ||
| metric_data = self.metric_reader.get_metrics_data() | ||
| self.assert_created_logs( | ||
| metric_data, | ||
| 2, | ||
| {}, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit, but not sure if we want to stick to the convention of using absolute paths.