Skip to content

Commit 8a321ac

Browse files
committed
Apply @dmitryax feedback. Keep Sampling config in general logger
1 parent 868041f commit 8a321ac

File tree

4 files changed

+23
-34
lines changed

4 files changed

+23
-34
lines changed

.chloggen/SampledLoggerTelemetry.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ issues: [8134]
1515
# (Optional) One or more lines of additional information to render under the primary note.
1616
# These lines will be padded with 2 spaces and then inserted directly into the document.
1717
# Use pipe (|) for multiline entries.
18-
subtext: The sampled logger is built from the logger. It is configured by `LogsSamplingConfig` (`service.telemetry.logs.sampling`).
18+
subtext: The sampled logger is built from the logger. It is built using default config (sample 100 logs in a second after 10 initial logs)
19+
1920

2021
# Optional: The change log or logs in which this entry should be included.
2122
# e.g. '[user]' or '[user, api]'

service/telemetry/config.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package telemetry // import "go.opentelemetry.io/collector/service/telemetry"
55

66
import (
77
"fmt"
8-
"time"
98

109
"go.uber.org/zap/zapcore"
1110

@@ -55,11 +54,7 @@ type LogsConfig struct {
5554
// (default = false)
5655
DisableStacktrace bool `mapstructure:"disable_stacktrace"`
5756

58-
// Sampling sets a sampling policy for the more efficient sampled logger.
59-
// Default:
60-
// initial: 1
61-
// thereafter: 100
62-
// tick: 10s
57+
// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
6358
Sampling *LogsSamplingConfig `mapstructure:"sampling"`
6459

6560
// OutputPaths is a list of URLs or file paths to write logging output to.
@@ -92,13 +87,12 @@ type LogsConfig struct {
9287
InitialFields map[string]any `mapstructure:"initial_fields"`
9388
}
9489

95-
// LogsSamplingConfig sets a sampling strategy for the more efficient sampled logger. Sampling caps the
90+
// LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the
9691
// global CPU and I/O load that logging puts on your process while attempting
9792
// to preserve a representative subset of your logs.
9893
type LogsSamplingConfig struct {
99-
Initial int `mapstructure:"initial"`
100-
Thereafter int `mapstructure:"thereafter"`
101-
Tick time.Duration `mapstructure:"tick"`
94+
Initial int `mapstructure:"initial"`
95+
Thereafter int `mapstructure:"thereafter"`
10296
}
10397

10498
// MetricsConfig exposes the common Telemetry configuration for one component.

service/telemetry/telemetry.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type Telemetry struct {
2121

2222
createSampledLogger sync.Once
2323
sampledLogger *zap.Logger
24-
samplingCfg *LogsSamplingConfig
2524
}
2625

2726
func (t *Telemetry) TracerProvider() trace.TracerProvider {
@@ -35,7 +34,7 @@ func (t *Telemetry) Logger() *zap.Logger {
3534
func (t *Telemetry) SampledLogger() func() *zap.Logger {
3635
return func() *zap.Logger {
3736
t.createSampledLogger.Do(func() {
38-
t.sampledLogger = newSampledLogger(t.samplingCfg, t.logger)
37+
t.sampledLogger = newSampledLogger(t.logger)
3938
})
4039
return t.sampledLogger
4140
}
@@ -67,7 +66,6 @@ func New(_ context.Context, set Settings, cfg Config) (*Telemetry, error) {
6766
return &Telemetry{
6867
logger: logger,
6968
tracerProvider: tp,
70-
samplingCfg: cfg.Logs.Sampling,
7169
}, nil
7270
}
7371

@@ -76,6 +74,7 @@ func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) {
7674
zapCfg := &zap.Config{
7775
Level: zap.NewAtomicLevelAt(cfg.Level),
7876
Development: cfg.Development,
77+
Sampling: toSamplingConfig(cfg.Sampling),
7978
Encoding: cfg.Encoding,
8079
EncoderConfig: zap.NewProductionEncoderConfig(),
8180
OutputPaths: cfg.OutputPaths,
@@ -98,29 +97,26 @@ func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) {
9897
return logger, nil
9998
}
10099

101-
func newSampledLogger(cfg *LogsSamplingConfig, logger *zap.Logger) *zap.Logger {
102-
if cfg == nil {
103-
cfg = newDefaultLogsSamplingConfig()
100+
func toSamplingConfig(sc *LogsSamplingConfig) *zap.SamplingConfig {
101+
if sc == nil {
102+
return nil
104103
}
104+
return &zap.SamplingConfig{
105+
Initial: sc.Initial,
106+
Thereafter: sc.Thereafter,
107+
}
108+
}
105109

106-
// Create a logger that samples all messages to "initial" per "tick" initially,
107-
// and cfg.Initial/cfg.Thereafter of messages after that.
110+
func newSampledLogger(logger *zap.Logger) *zap.Logger {
111+
// Create a logger that samples all messages to 10 per second initially,
112+
// and 10/100 of messages after that.
108113
opts := zap.WrapCore(func(core zapcore.Core) zapcore.Core {
109114
return zapcore.NewSamplerWithOptions(
110115
core,
111-
cfg.Tick,
112-
cfg.Initial,
113-
cfg.Thereafter,
116+
1*time.Second,
117+
10,
118+
100,
114119
)
115120
})
116121
return logger.WithOptions(opts)
117122
}
118-
119-
// newDefaultLogsSamplingConfig returns a default LogsSamplingConfig.
120-
func newDefaultLogsSamplingConfig() *LogsSamplingConfig {
121-
return &LogsSamplingConfig{
122-
Initial: 1,
123-
Thereafter: 100,
124-
Tick: 10 * time.Second,
125-
}
126-
}

service/telemetry/telemetry_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package telemetry
66
import (
77
"context"
88
"testing"
9-
"time"
109

1110
"github.com/stretchr/testify/assert"
1211
"go.uber.org/zap"
@@ -83,14 +82,13 @@ func TestSampledLoggerCreateFirstTime(t *testing.T) {
8382
},
8483
},
8584
{
86-
name: "Custom sampling",
85+
name: "Already using sampling",
8786
cfg: &Config{
8887
Logs: LogsConfig{
8988
Level: zapcore.DebugLevel,
9089
Encoding: "console",
9190
Sampling: &LogsSamplingConfig{
9291
Initial: 50,
93-
Tick: 2 * time.Second,
9492
Thereafter: 40,
9593
},
9694
},

0 commit comments

Comments
 (0)