Skip to content

Commit

Permalink
Allow to configure sampling config for logs
Browse files Browse the repository at this point in the history
Fixes open-telemetry#4554

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu committed Nov 7, 2022
1 parent 6b27644 commit 276c1c9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
11 changes: 11 additions & 0 deletions .chloggen/allowsampling.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: service/telemetry

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow to configure sampling config for logs.

# One or more tracking issues or pull requests related to the change
issues: [4554]
10 changes: 7 additions & 3 deletions service/config_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ var configNop = &Config{
},
Telemetry: telemetry.Config{
Logs: telemetry.LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Sampling: &telemetry.LogsSamplingConfig{
Initial: 100,
Thereafter: 100,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
Expand Down
11 changes: 11 additions & 0 deletions service/telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type LogsConfig struct {
// (default = false)
DisableStacktrace bool `mapstructure:"disable_stacktrace"`

// Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
Sampling *LogsSamplingConfig `mapstructure:"sampling"`

// OutputPaths is a list of URLs or file paths to write logging output to.
// The URLs could only be with "file" schema or without schema.
// The URLs with "file" schema must be an absolute path.
Expand Down Expand Up @@ -91,6 +94,14 @@ type LogsConfig struct {
InitialFields map[string]interface{} `mapstructure:"initial_fields"`
}

// LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the
// global CPU and I/O load that logging puts on your process while attempting
// to preserve a representative subset of your logs.
type LogsSamplingConfig struct {
Initial int `mapstructure:"initial"`
Thereafter int `mapstructure:"thereafter"`
}

// MetricsConfig exposes the common Telemetry configuration for one component.
// Experimental: *NOTE* this structure is subject to change or removal in the future.
type MetricsConfig struct {
Expand Down
20 changes: 13 additions & 7 deletions service/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,9 @@ func New(_ context.Context, set Settings, cfg Config) (*Telemetry, error) {
func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) {
// Copied from NewProductionConfig.
zapCfg := &zap.Config{
Level: zap.NewAtomicLevelAt(cfg.Level),
Development: cfg.Development,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Level: zap.NewAtomicLevelAt(cfg.Level),
Development: cfg.Development,
Sampling: toSamplingConfig(cfg.Sampling),
Encoding: cfg.Encoding,
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: cfg.OutputPaths,
Expand All @@ -101,9 +98,18 @@ func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) {
return logger, nil
}

type nopSpanProcessor struct {
func toSamplingConfig(sc *LogsSamplingConfig) *zap.SamplingConfig {
if sc == nil {
return nil
}
return &zap.SamplingConfig{
Initial: sc.Initial,
Thereafter: sc.Thereafter,
}
}

type nopSpanProcessor struct{}

func (n nopSpanProcessor) OnStart(context.Context, sdktrace.ReadWriteSpan) {}

func (n nopSpanProcessor) OnEnd(sdktrace.ReadOnlySpan) {}
Expand Down
10 changes: 7 additions & 3 deletions service/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ func unmarshal(v *confmap.Conf, factories component.Factories) (*configSettings,
Service: ConfigService{
Telemetry: telemetry.Config{
Logs: telemetry.LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
Sampling: &telemetry.LogsSamplingConfig{
Initial: 100,
Thereafter: 100,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
Expand Down
10 changes: 7 additions & 3 deletions service/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ func TestUnmarshalEmptyAllSections(t *testing.T) {

zapProdCfg := zap.NewProductionConfig()
assert.Equal(t, telemetry.LogsConfig{
Level: zapProdCfg.Level.Level(),
Development: zapProdCfg.Development,
Encoding: "console",
Level: zapProdCfg.Level.Level(),
Development: zapProdCfg.Development,
Encoding: "console",
Sampling: &telemetry.LogsSamplingConfig{
Initial: 100,
Thereafter: 100,
},
DisableCaller: zapProdCfg.DisableCaller,
DisableStacktrace: zapProdCfg.DisableStacktrace,
OutputPaths: zapProdCfg.OutputPaths,
Expand Down

0 comments on commit 276c1c9

Please sign in to comment.