Path: blob/main/pkg/traces/promsdprocessor/factory.go
4094 views
package promsdprocessor12import (3"context"4"fmt"56prom_config "github.com/prometheus/prometheus/config"7"go.opentelemetry.io/collector/component"8"go.opentelemetry.io/collector/config"9"go.opentelemetry.io/collector/consumer"10"gopkg.in/yaml.v2"11)1213// TypeStr is the unique identifier for the Prometheus SD processor.14const TypeStr = "prom_sd_processor"1516const (17// OperationTypeInsert inserts a new k/v if it isn't already present18OperationTypeInsert = "insert"19// OperationTypeUpdate only modifies an existing k/v20OperationTypeUpdate = "update"21// OperationTypeUpsert does both of above22OperationTypeUpsert = "upsert"2324podAssociationIPLabel = "ip"25podAssociationOTelIPLabel = "net.host.ip"26podAssociationk8sIPLabel = "k8s.pod.ip"27podAssociationHostnameLabel = "hostname"28podAssociationConnectionIP = "connection"29)3031// Config holds the configuration for the Prometheus SD processor.32type Config struct {33config.ProcessorSettings `mapstructure:",squash"`34ScrapeConfigs []interface{} `mapstructure:"scrape_configs"`35OperationType string `mapstructure:"operation_type"`36PodAssociations []string `mapstructure:"pod_associations"`37}3839// NewFactory returns a new factory for the Attributes processor.40func NewFactory() component.ProcessorFactory {41return component.NewProcessorFactory(42TypeStr,43createDefaultConfig,44component.WithTracesProcessor(createTraceProcessor, component.StabilityLevelUndefined),45)46}4748func createDefaultConfig() config.Processor {49return &Config{50ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(TypeStr, TypeStr)),51}52}5354func createTraceProcessor(55_ context.Context,56cp component.ProcessorCreateSettings,57cfg config.Processor,58nextConsumer consumer.Traces,59) (component.TracesProcessor, error) {6061oCfg := cfg.(*Config)62out, err := yaml.Marshal(oCfg.ScrapeConfigs)63if err != nil {64return nil, fmt.Errorf("unable to marshal scrapeConfigs interface{} to yaml: %w", err)65}6667scrapeConfigs := make([]*prom_config.ScrapeConfig, 0)68err = yaml.Unmarshal(out, &scrapeConfigs)69if err != nil {70return nil, fmt.Errorf("unable to unmarshal bytes to []*config.ScrapeConfig: %w", err)71}7273return newTraceProcessor(nextConsumer, oCfg.OperationType, oCfg.PodAssociations, scrapeConfigs)74}757677