-
Notifications
You must be signed in to change notification settings - Fork 85
/
tenant.go
114 lines (92 loc) · 3.07 KB
/
tenant.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package stages
import (
"errors"
"reflect"
"time"
"github.com/go-kit/log"
"github.com/grafana/alloy/internal/alloy/logging/level"
"github.com/prometheus/common/model"
)
// Configuration errors.
var (
ErrTenantStageEmptyLabelSourceOrValue = errors.New("label, source or value config are required")
ErrTenantStageConflictingLabelSourceAndValue = errors.New("label, source and value are mutually exclusive: you should set source, value or label but not all")
)
// ReservedLabelTenantID is a shared value used to refer to the tenant ID.
const ReservedLabelTenantID = "__tenant_id__"
type tenantStage struct {
cfg TenantConfig
logger log.Logger
}
// TenantConfig configures a tenant stage.
type TenantConfig struct {
Label string `alloy:"label,attr,optional"`
Source string `alloy:"source,attr,optional"`
Value string `alloy:"value,attr,optional"`
}
// validateTenantConfig validates the tenant stage configuration
func validateTenantConfig(c TenantConfig) error {
if c.Source == "" && c.Value == "" && c.Label == "" {
return ErrTenantStageEmptyLabelSourceOrValue
}
if c.Source != "" && c.Value != "" || c.Label != "" && c.Value != "" || c.Source != "" && c.Label != "" {
return ErrTenantStageConflictingLabelSourceAndValue
}
return nil
}
// newTenantStage creates a new tenant stage to override the tenant ID from extracted data
func newTenantStage(logger log.Logger, cfg TenantConfig) (Stage, error) {
err := validateTenantConfig(cfg)
if err != nil {
return nil, err
}
return toStage(&tenantStage{
cfg: cfg,
logger: logger,
}), nil
}
// Process implements Stage
func (s *tenantStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) {
var tenantID string
// Get tenant ID from source or configured value
if s.cfg.Source != "" {
tenantID = s.getTenantFromSourceField(extracted)
} else if s.cfg.Label != "" {
tenantID = s.getTenantFromLabel(labels)
} else {
tenantID = s.cfg.Value
}
// Skip an empty tenant ID (i.e. failed to get the tenant from the source)
if tenantID == "" {
return
}
labels[ReservedLabelTenantID] = model.LabelValue(tenantID)
}
// Name implements Stage
func (s *tenantStage) Name() string {
return StageTypeTenant
}
func (s *tenantStage) getTenantFromSourceField(extracted map[string]interface{}) string {
// Get the tenant ID from the source data
value, ok := extracted[s.cfg.Source]
if !ok {
level.Debug(s.logger).Log("msg", "the tenant source does not exist in the extracted data", "source", s.cfg.Source)
return ""
}
// Convert the value to string
tenantID, err := getString(value)
if err != nil {
level.Debug(s.logger).Log("msg", "failed to convert value to string", "err", err, "type", reflect.TypeOf(value))
return ""
}
return tenantID
}
func (s *tenantStage) getTenantFromLabel(labels model.LabelSet) string {
// Get the tenant ID from the label map
tenantID, ok := labels[model.LabelName(s.cfg.Label)]
if !ok {
level.Debug(s.logger).Log("msg", "the tenant source does not exist in the labels", "source", s.cfg.Source)
return ""
}
return string(tenantID)
}