Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/traces/automaticloggingprocessor/factory.go
4095 views
1
package automaticloggingprocessor
2
3
import (
4
"context"
5
"fmt"
6
"time"
7
8
"github.com/grafana/agent/pkg/logs"
9
"go.opentelemetry.io/collector/component"
10
"go.opentelemetry.io/collector/config"
11
"go.opentelemetry.io/collector/consumer"
12
)
13
14
// TypeStr is the unique identifier for the Automatic Logging processor.
15
const TypeStr = "automatic_logging"
16
17
// Config holds the configuration for the Automatic Logging processor.
18
type Config struct {
19
config.ProcessorSettings `mapstructure:",squash"`
20
21
LoggingConfig *AutomaticLoggingConfig `mapstructure:"automatic_logging"`
22
}
23
24
// AutomaticLoggingConfig holds config information for automatic logging
25
type AutomaticLoggingConfig struct {
26
Backend string `mapstructure:"backend" yaml:"backend,omitempty"`
27
LogsName string `mapstructure:"logs_instance_name" yaml:"logs_instance_name,omitempty"`
28
Spans bool `mapstructure:"spans" yaml:"spans,omitempty"`
29
Roots bool `mapstructure:"roots" yaml:"roots,omitempty"`
30
Processes bool `mapstructure:"processes" yaml:"processes,omitempty"`
31
SpanAttributes []string `mapstructure:"span_attributes" yaml:"span_attributes,omitempty"`
32
ProcessAttributes []string `mapstructure:"process_attributes" yaml:"process_attributes,omitempty"`
33
Overrides OverrideConfig `mapstructure:"overrides" yaml:"overrides,omitempty"`
34
Timeout time.Duration `mapstructure:"timeout" yaml:"timeout,omitempty"`
35
Labels []string `mapstructure:"labels" yaml:"labels,omitempty"`
36
37
// Deprecated fields:
38
LokiName string `mapstructure:"loki_name" yaml:"loki_name,omitempty"` // Superseded by LogsName
39
}
40
41
// Validate ensures that the AutomaticLoggingConfig is valid.
42
func (c *AutomaticLoggingConfig) Validate(logsConfig *logs.Config) error {
43
if c.Backend == BackendLoki {
44
c.Backend = BackendLogs
45
}
46
47
if c.LogsName != "" && c.LokiName != "" {
48
return fmt.Errorf("must configure at most one of logs_instance_name and loki_name. loki_name is deprecated in favor of logs_instance_name")
49
}
50
51
if c.LogsName != "" && logsConfig == nil {
52
return fmt.Errorf("logs instance %s is set but no logs config is provided", c.LogsName)
53
}
54
55
// Migrate deprecated config to new one
56
if c.LogsName == "" && c.LokiName != "" {
57
c.LogsName, c.LokiName = c.LokiName, ""
58
}
59
60
if c.Overrides.LogsTag != "" && c.Overrides.LokiTag != "" {
61
return fmt.Errorf("must configure at most one of overrides.logs_instance_tag and overrides.loki_tag. loki_tag is deprecated in favor of logs_instance_tag")
62
}
63
64
// Migrate deprecated config to new one
65
if c.Overrides.LogsTag == "" && c.Overrides.LokiTag != "" {
66
c.Overrides.LogsTag, c.Overrides.LokiTag = c.Overrides.LokiTag, ""
67
}
68
69
// Ensure the logging instance exists when using it as a backend.
70
if c.Backend == BackendLogs {
71
var found bool
72
for _, inst := range logsConfig.Configs {
73
if inst.Name == c.LogsName {
74
found = true
75
break
76
}
77
}
78
if !found {
79
return fmt.Errorf("specified logs config %s not found in agent config", c.LogsName)
80
}
81
}
82
83
return nil
84
}
85
86
// OverrideConfig contains overrides for various strings
87
type OverrideConfig struct {
88
LogsTag string `mapstructure:"logs_instance_tag" yaml:"logs_instance_tag,omitempty"`
89
ServiceKey string `mapstructure:"service_key" yaml:"service_key,omitempty"`
90
SpanNameKey string `mapstructure:"span_name_key" yaml:"span_name_key,omitempty"`
91
StatusKey string `mapstructure:"status_key" yaml:"status_key,omitempty"`
92
DurationKey string `mapstructure:"duration_key" yaml:"duration_key,omitempty"`
93
TraceIDKey string `mapstructure:"trace_id_key" yaml:"trace_id_key,omitempty"`
94
95
// Deprecated fields:
96
LokiTag string `mapstructure:"loki_tag" yaml:"loki_tag,omitempty"` // Superseded by LogsTag
97
}
98
99
const (
100
// BackendLogs is the backend config for sending logs to a Loki pipeline
101
BackendLogs = "logs_instance"
102
// BackendLoki is an alias to BackendLogs. DEPRECATED.
103
BackendLoki = "loki"
104
// BackendStdout is the backend config value for sending logs to stdout
105
BackendStdout = "stdout"
106
)
107
108
// NewFactory returns a new factory for the Attributes processor.
109
func NewFactory() component.ProcessorFactory {
110
return component.NewProcessorFactory(
111
TypeStr,
112
createDefaultConfig,
113
component.WithTracesProcessor(createTraceProcessor, component.StabilityLevelUndefined),
114
)
115
}
116
117
func createDefaultConfig() config.Processor {
118
return &Config{
119
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(TypeStr, TypeStr)),
120
}
121
}
122
123
func createTraceProcessor(
124
_ context.Context,
125
cp component.ProcessorCreateSettings,
126
cfg config.Processor,
127
nextConsumer consumer.Traces,
128
) (component.TracesProcessor, error) {
129
130
oCfg := cfg.(*Config)
131
return newTraceProcessor(nextConsumer, oCfg.LoggingConfig)
132
}
133
134