|
| 1 | +// Copyright 2024 Crunchy Data Solutions, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package collector |
| 6 | + |
| 7 | +import ( |
| 8 | + "k8s.io/apimachinery/pkg/util/sets" |
| 9 | + "sigs.k8s.io/yaml" |
| 10 | +) |
| 11 | + |
| 12 | +// ComponentID represents a component identifier within an OpenTelemetry |
| 13 | +// Collector YAML configuration. Each value is a "type" followed by an optional |
| 14 | +// slash-then-name: `type[/name]` |
| 15 | +type ComponentID string |
| 16 | + |
| 17 | +// Config represents an OpenTelemetry Collector YAML configuration. |
| 18 | +// See: https://opentelemetry.io/docs/collector/configuration |
| 19 | +type Config struct { |
| 20 | + Exporters map[ComponentID]any |
| 21 | + Extensions map[ComponentID]any |
| 22 | + Processors map[ComponentID]any |
| 23 | + Receivers map[ComponentID]any |
| 24 | + |
| 25 | + Pipelines map[PipelineID]Pipeline |
| 26 | +} |
| 27 | + |
| 28 | +// Pipeline represents the YAML configuration of a flow of telemetry data |
| 29 | +// through an OpenTelemetry Collector. |
| 30 | +// See: https://opentelemetry.io/docs/collector/configuration#pipelines |
| 31 | +type Pipeline struct { |
| 32 | + Extensions []ComponentID |
| 33 | + Exporters []ComponentID |
| 34 | + Processors []ComponentID |
| 35 | + Receivers []ComponentID |
| 36 | +} |
| 37 | + |
| 38 | +// PipelineID represents a pipeline identifier within an OpenTelemetry Collector |
| 39 | +// YAML configuration. Each value is a signal followed by an optional |
| 40 | +// slash-then-name: `signal[/name]` |
| 41 | +type PipelineID string |
| 42 | + |
| 43 | +func (c *Config) ToYAML() (string, error) { |
| 44 | + const yamlGeneratedWarning = "" + |
| 45 | + "# Generated by postgres-operator. DO NOT EDIT.\n" + |
| 46 | + "# Your changes will not be saved.\n" |
| 47 | + |
| 48 | + extensions := sets.New[ComponentID]() |
| 49 | + pipelines := make(map[PipelineID]any, len(c.Pipelines)) |
| 50 | + |
| 51 | + for id, p := range c.Pipelines { |
| 52 | + extensions.Insert(p.Extensions...) |
| 53 | + pipelines[id] = map[string]any{ |
| 54 | + "exporters": p.Exporters, |
| 55 | + "processors": p.Processors, |
| 56 | + "receivers": p.Receivers, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + b, err := yaml.Marshal(map[string]any{ |
| 61 | + "exporters": c.Exporters, |
| 62 | + "extensions": c.Extensions, |
| 63 | + "processors": c.Processors, |
| 64 | + "receivers": c.Receivers, |
| 65 | + "service": map[string]any{ |
| 66 | + "extensions": sets.List(extensions), // Sorted |
| 67 | + "pipelines": pipelines, |
| 68 | + }, |
| 69 | + }) |
| 70 | + return string(append([]byte(yamlGeneratedWarning), b...)), err |
| 71 | +} |
| 72 | + |
| 73 | +// NewConfig creates a base config for an OTel collector container |
| 74 | +func NewConfig() *Config { |
| 75 | + return &Config{ |
| 76 | + Exporters: map[ComponentID]any{ |
| 77 | + // TODO: Do we want a DebugExporter outside of development? |
| 78 | + // https://pkg.go.dev/go.opentelemetry.io/collector/exporter/debugexporter#section-readme |
| 79 | + DebugExporter: map[string]any{"verbosity": "detailed"}, |
| 80 | + }, |
| 81 | + Extensions: map[ComponentID]any{}, |
| 82 | + Processors: map[ComponentID]any{ |
| 83 | + // https://pkg.go.dev/go.opentelemetry.io/collector/processor/batchprocessor#section-readme |
| 84 | + OneSecondBatchProcessor: map[string]any{"timeout": "1s"}, |
| 85 | + SubSecondBatchProcessor: map[string]any{"timeout": "200ms"}, |
| 86 | + |
| 87 | + // https://pkg.go.dev/github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor#readme-compaction |
| 88 | + CompactingProcessor: map[string]any{}, |
| 89 | + }, |
| 90 | + Receivers: map[ComponentID]any{}, |
| 91 | + Pipelines: map[PipelineID]Pipeline{}, |
| 92 | + } |
| 93 | +} |
0 commit comments