-
Notifications
You must be signed in to change notification settings - Fork 486
/
factory.go
75 lines (63 loc) · 2.24 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package promsdprocessor
import (
"context"
"fmt"
prom_config "github.com/prometheus/prometheus/config"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"gopkg.in/yaml.v2"
)
// TypeStr is the unique identifier for the Prometheus SD processor.
const TypeStr = "prom_sd_processor"
const (
// OperationTypeInsert inserts a new k/v if it isn't already present
OperationTypeInsert = "insert"
// OperationTypeUpdate only modifies an existing k/v
OperationTypeUpdate = "update"
// OperationTypeUpsert does both of above
OperationTypeUpsert = "upsert"
podAssociationIPLabel = "ip"
podAssociationOTelIPLabel = "net.host.ip"
podAssociationk8sIPLabel = "k8s.pod.ip"
podAssociationHostnameLabel = "hostname"
podAssociationConnectionIP = "connection"
)
// Config holds the configuration for the Prometheus SD processor.
type Config struct {
config.ProcessorSettings `mapstructure:",squash"`
ScrapeConfigs []interface{} `mapstructure:"scrape_configs"`
OperationType string `mapstructure:"operation_type"`
PodAssociations []string `mapstructure:"pod_associations"`
}
// NewFactory returns a new factory for the Attributes processor.
func NewFactory() component.ProcessorFactory {
return component.NewProcessorFactory(
TypeStr,
createDefaultConfig,
component.WithTracesProcessor(createTraceProcessor),
)
}
func createDefaultConfig() config.Processor {
return &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(TypeStr, TypeStr)),
}
}
func createTraceProcessor(
_ context.Context,
cp component.ProcessorCreateSettings,
cfg config.Processor,
nextConsumer consumer.Traces,
) (component.TracesProcessor, error) {
oCfg := cfg.(*Config)
out, err := yaml.Marshal(oCfg.ScrapeConfigs)
if err != nil {
return nil, fmt.Errorf("unable to marshal scrapeConfigs interface{} to yaml: %w", err)
}
scrapeConfigs := make([]*prom_config.ScrapeConfig, 0)
err = yaml.Unmarshal(out, &scrapeConfigs)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal bytes to []*config.ScrapeConfig: %w", err)
}
return newTraceProcessor(nextConsumer, oCfg.OperationType, oCfg.PodAssociations, scrapeConfigs)
}