forked from grafana/loki
/
tenant.go
107 lines (87 loc) · 2.54 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
package stages
import (
"reflect"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/grafana/loki/clients/pkg/promtail/client"
)
const (
ErrTenantStageEmptySourceOrValue = "source or value config are required"
ErrTenantStageConflictingSourceAndValue = "source and value are mutually exclusive: you should set source or value but not both"
)
type tenantStage struct {
cfg TenantConfig
logger log.Logger
}
type TenantConfig struct {
Source string `mapstructure:"source"`
Value string `mapstructure:"value"`
}
// validateTenantConfig validates the tenant stage configuration
func validateTenantConfig(c TenantConfig) error {
if c.Source == "" && c.Value == "" {
return errors.New(ErrTenantStageEmptySourceOrValue)
}
if c.Source != "" && c.Value != "" {
return errors.New(ErrTenantStageConflictingSourceAndValue)
}
return nil
}
// newTenantStage creates a new tenant stage to override the tenant ID from extracted data
func newTenantStage(logger log.Logger, configs interface{}) (Stage, error) {
cfg := TenantConfig{}
err := mapstructure.Decode(configs, &cfg)
if err != nil {
return nil, err
}
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 {
tenantID = s.cfg.Value
}
// Skip an empty tenant ID (ie. failed to get the tenant from the source)
if tenantID == "" {
return
}
labels[client.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 {
if Debug {
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 {
if Debug {
level.Debug(s.logger).Log("msg", "failed to convert value to string", "err", err, "type", reflect.TypeOf(value))
}
return ""
}
return tenantID
}