Path: blob/main/pkg/traces/automaticloggingprocessor/factory.go
4095 views
package automaticloggingprocessor12import (3"context"4"fmt"5"time"67"github.com/grafana/agent/pkg/logs"8"go.opentelemetry.io/collector/component"9"go.opentelemetry.io/collector/config"10"go.opentelemetry.io/collector/consumer"11)1213// TypeStr is the unique identifier for the Automatic Logging processor.14const TypeStr = "automatic_logging"1516// Config holds the configuration for the Automatic Logging processor.17type Config struct {18config.ProcessorSettings `mapstructure:",squash"`1920LoggingConfig *AutomaticLoggingConfig `mapstructure:"automatic_logging"`21}2223// AutomaticLoggingConfig holds config information for automatic logging24type AutomaticLoggingConfig struct {25Backend string `mapstructure:"backend" yaml:"backend,omitempty"`26LogsName string `mapstructure:"logs_instance_name" yaml:"logs_instance_name,omitempty"`27Spans bool `mapstructure:"spans" yaml:"spans,omitempty"`28Roots bool `mapstructure:"roots" yaml:"roots,omitempty"`29Processes bool `mapstructure:"processes" yaml:"processes,omitempty"`30SpanAttributes []string `mapstructure:"span_attributes" yaml:"span_attributes,omitempty"`31ProcessAttributes []string `mapstructure:"process_attributes" yaml:"process_attributes,omitempty"`32Overrides OverrideConfig `mapstructure:"overrides" yaml:"overrides,omitempty"`33Timeout time.Duration `mapstructure:"timeout" yaml:"timeout,omitempty"`34Labels []string `mapstructure:"labels" yaml:"labels,omitempty"`3536// Deprecated fields:37LokiName string `mapstructure:"loki_name" yaml:"loki_name,omitempty"` // Superseded by LogsName38}3940// Validate ensures that the AutomaticLoggingConfig is valid.41func (c *AutomaticLoggingConfig) Validate(logsConfig *logs.Config) error {42if c.Backend == BackendLoki {43c.Backend = BackendLogs44}4546if c.LogsName != "" && c.LokiName != "" {47return fmt.Errorf("must configure at most one of logs_instance_name and loki_name. loki_name is deprecated in favor of logs_instance_name")48}4950if c.LogsName != "" && logsConfig == nil {51return fmt.Errorf("logs instance %s is set but no logs config is provided", c.LogsName)52}5354// Migrate deprecated config to new one55if c.LogsName == "" && c.LokiName != "" {56c.LogsName, c.LokiName = c.LokiName, ""57}5859if c.Overrides.LogsTag != "" && c.Overrides.LokiTag != "" {60return 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")61}6263// Migrate deprecated config to new one64if c.Overrides.LogsTag == "" && c.Overrides.LokiTag != "" {65c.Overrides.LogsTag, c.Overrides.LokiTag = c.Overrides.LokiTag, ""66}6768// Ensure the logging instance exists when using it as a backend.69if c.Backend == BackendLogs {70var found bool71for _, inst := range logsConfig.Configs {72if inst.Name == c.LogsName {73found = true74break75}76}77if !found {78return fmt.Errorf("specified logs config %s not found in agent config", c.LogsName)79}80}8182return nil83}8485// OverrideConfig contains overrides for various strings86type OverrideConfig struct {87LogsTag string `mapstructure:"logs_instance_tag" yaml:"logs_instance_tag,omitempty"`88ServiceKey string `mapstructure:"service_key" yaml:"service_key,omitempty"`89SpanNameKey string `mapstructure:"span_name_key" yaml:"span_name_key,omitempty"`90StatusKey string `mapstructure:"status_key" yaml:"status_key,omitempty"`91DurationKey string `mapstructure:"duration_key" yaml:"duration_key,omitempty"`92TraceIDKey string `mapstructure:"trace_id_key" yaml:"trace_id_key,omitempty"`9394// Deprecated fields:95LokiTag string `mapstructure:"loki_tag" yaml:"loki_tag,omitempty"` // Superseded by LogsTag96}9798const (99// BackendLogs is the backend config for sending logs to a Loki pipeline100BackendLogs = "logs_instance"101// BackendLoki is an alias to BackendLogs. DEPRECATED.102BackendLoki = "loki"103// BackendStdout is the backend config value for sending logs to stdout104BackendStdout = "stdout"105)106107// NewFactory returns a new factory for the Attributes processor.108func NewFactory() component.ProcessorFactory {109return component.NewProcessorFactory(110TypeStr,111createDefaultConfig,112component.WithTracesProcessor(createTraceProcessor, component.StabilityLevelUndefined),113)114}115116func createDefaultConfig() config.Processor {117return &Config{118ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(TypeStr, TypeStr)),119}120}121122func createTraceProcessor(123_ context.Context,124cp component.ProcessorCreateSettings,125cfg config.Processor,126nextConsumer consumer.Traces,127) (component.TracesProcessor, error) {128129oCfg := cfg.(*Config)130return newTraceProcessor(nextConsumer, oCfg.LoggingConfig)131}132133134