Commit Graph

6 Commits

Author SHA1 Message Date
Andrew Wilkins 5b18870ccc
[chore] service: refactor logger wrapping (#13329)
#### Description

There are two main changes here:
- We move the componentattribute logger core wrapping and Logger to
LoggerProvider tee'ing to the service package. This will enable the
wrapping to be performed independently of the implementation of
telemetry providers.
- We move away from using zapcore.NewTeeCore, and introduce a new
zapcore.Core implementation that only copies log entries accepted by the
native Zap core to the OTel LoggerProvider.

The second change is necessary due to the first: because the
LoggerProvider will in the future be injectable, and because the
telemetry configuration will be opaque to the service package, we must
always tee the logs from the zap logger to the LoggerProvider. When
using zapcore.NewTeeCore, the resulting core will duplicate all entries
to all cores - which is not what we want.

There's also some light refactoring of tests, and the service package
tests are now ~30s faster by replacing a sleep with assert.Eventually.

#### Link to tracking issue

Preparation work for
https://github.com/open-telemetry/opentelemetry-collector/issues/4970

#### Testing

Unit tests pass

#### Documentation

N/A

---------

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
2025-07-18 10:13:33 +00:00
Jade Guiton 4ddf2cc7c8
[service] Share log sampler core allocations with reflect magic (#13107)
#### Context

PR #12617, which implemented the injection of component-identifying
attributes into the `zap.Logger` provided to components, introduced
significant additional memory use when the Collector's pipelines contain
many components (#13014). This was because we would call
`zapcore.NewSamplerWithOptions` to wrap the specialized logger core of
each Collector component, which allocates half a megabyte's worth of
sampling counters.

This problem was mitigated in #13015 by moving the sampling layer to a
different location in the logger core hierarchy. This meant that
Collector users that do not export their logs through OTLP and only use
stdout-based logs no longer saw the memory increase.

#### Description

This PR aims to provide a better solution to this issue, by using the
`reflect` library to clone zap's sampler core and set a new inner core,
while reusing the counter allocation.

(This may also be "more correct" from a sampling point of view, ie. we
only have one global instance of the counters instead of one for console
logs and one for each component's OTLP-exported logs, but I'm not sure
if anyone noticed the difference anyway).

#### Link to tracking issue
Fixes #13014

#### Testing
A new test was added which checks that the log counters are shared
between two sampler cores with different attributes.
2025-06-11 10:06:08 +00:00
Jade Guiton 7ff2465190
[componentattribute] Use otelzap.WithAttributes, remove LoggerProviderWithAttributes (#13108)
#### Description

Now that `otelzap` has been bumped up to 0.11.0 (#13103), this PR
follows up on a TODO comment to use
https://github.com/open-telemetry/opentelemetry-go-contrib/pull/6962 to
set instrumentation scope attributes on the Logger created by otelzap,
instead of a workaround (`LoggerProviderWithAttributes`).

This is an internal change, and the new code should be functionally
equivalent, so I don't think this needs a changelog.

#### Testing

I believe this is already covered by existing tests.
2025-05-30 19:13:14 +00:00
Bogdan Drutu 8991b48796
Avoid allocating too much memory, sampling logic is not re-applied (#13015)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Avoid re-creating sampler counters every time we wrap with attributes.

<!-- Issue number if applicable -->
#### Link to tracking issue
Updates #13014 

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->

---------

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
Co-authored-by: Jade Guiton <jade.guiton@datadoghq.com>
2025-05-12 16:31:17 +00:00
Matthieu MOREL cb5c3f4fb9
[chore]: fix staticcheck exclusions (#12792)
#### Description

Fixes staticcheck rules which where disabled with golangci-lint v2
upgrade

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-04-02 20:07:55 +00:00
Jade Guiton 54c13a9217
Inject component-identifying scope attributes (#12617)
#### Description

Fork of #12384 to showcase how component attributes can be injected into
scope attributes instead of log/metric/span attributes. See that PR for
more context.

To see the diff from the previous PR, filter changes starting from the
"Prototype using scope attributes" commit.

#### Link to tracking issue
Resolves #12217 
Also incidentally resolves #12213 and resolves #12117

#### Testing
I updated the existing tests to check for scope attributes, and did some
manual testing with a debug exporter to check that the scope attributes
are added/removed properly.

---------

Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
2025-03-28 12:15:25 +00:00