Skip to content

Commit 97b31a6

Browse files
cbandybenjaminjbdsessler7Tony Landreth
committed
Initial configuration for an OpenTelemetry Collector
Co-authored-by: Benjamin Blattberg <[email protected]> Co-authored-by: Drew Sessler <[email protected]> Co-authored-by: Tony Landreth <[email protected]>
1 parent 740400d commit 97b31a6

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

internal/collector/config.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

internal/collector/config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Crunchy Data Solutions, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package collector
6+
7+
import (
8+
"testing"
9+
10+
"gotest.tools/v3/assert"
11+
)
12+
13+
func TestConfigToYAML(t *testing.T) {
14+
result, err := NewConfig().ToYAML()
15+
assert.NilError(t, err)
16+
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
17+
# Your changes will not be saved.
18+
exporters:
19+
debug:
20+
verbosity: detailed
21+
extensions: {}
22+
processors:
23+
batch/1s:
24+
timeout: 1s
25+
batch/200ms:
26+
timeout: 200ms
27+
groupbyattrs/compact: {}
28+
receivers: {}
29+
service:
30+
extensions: []
31+
pipelines: {}
32+
`)
33+
}

internal/collector/naming.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2024 Crunchy Data Solutions, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package collector
6+
7+
const CompactingProcessor = "groupbyattrs/compact"
8+
const DebugExporter = "debug"
9+
const OneSecondBatchProcessor = "batch/1s"
10+
const SubSecondBatchProcessor = "batch/200ms"

internal/naming/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
)
1919

2020
const (
21+
ContainerCollector = "collector"
22+
2123
// ContainerDatabase is the name of the container running PostgreSQL and
2224
// supporting tools: Patroni, pgBackRest, etc.
2325
ContainerDatabase = "database"

0 commit comments

Comments
 (0)