-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config.go
67 lines (55 loc) · 1.9 KB
/
config.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package headerssetterextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/headerssetterextension"
import (
"fmt"
)
var (
errMissingHeader = fmt.Errorf("missing header name")
errMissingHeadersConfig = fmt.Errorf("missing headers configuration")
errMissingSource = fmt.Errorf("missing header source, must be 'from_context' or 'value'")
errConflictingSources = fmt.Errorf("invalid header source, must either 'from_context' or 'value'")
)
type Config struct {
HeadersConfig []HeaderConfig `mapstructure:"headers"`
}
type HeaderConfig struct {
Action ActionValue `mapstructure:"action"`
Key *string `mapstructure:"key"`
Value *string `mapstructure:"value"`
FromContext *string `mapstructure:"from_context"`
DefaultValue *string `mapstructure:"default_value"`
}
// ActionValue is the enum to capture the four types of actions to perform on a header
type ActionValue string
const (
// INSERT inserts the new header if it does not exist
INSERT ActionValue = "insert"
// UPDATE updates the header value if it exists
UPDATE ActionValue = "update"
// UPSERT inserts a header if it does not exist and updates the header
// if it exists
UPSERT ActionValue = "upsert"
// DELETE deletes the header
DELETE ActionValue = "delete"
)
// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
if len(cfg.HeadersConfig) == 0 {
return errMissingHeadersConfig
}
for _, header := range cfg.HeadersConfig {
if header.Key == nil || *header.Key == "" {
return errMissingHeader
}
if header.Action != DELETE {
if header.FromContext == nil && header.Value == nil {
return errMissingSource
}
if header.FromContext != nil && header.Value != nil {
return errConflictingSources
}
}
}
return nil
}