|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/prometheus/common/model" |
| 8 | + "github.com/prometheus/prometheus/model/relabel" |
| 9 | +) |
| 10 | + |
| 11 | +// copy and modification of github.com/prometheus/prometheus/model/relabel/relabel.go |
| 12 | +// reason: the custom types in github.com/prometheus/prometheus/model/relabel/relabel.go are difficult to unmarshal |
| 13 | +type RelabelConfig struct { |
| 14 | + // A list of labels from which values are taken and concatenated |
| 15 | + // with the configured separator in order. |
| 16 | + SourceLabels []string `json:"source_labels,omitempty"` |
| 17 | + // Separator is the string between concatenated values from the source labels. |
| 18 | + Separator string `json:"separator,omitempty"` |
| 19 | + // Regex against which the concatenation is matched. |
| 20 | + Regex string `json:"regex,omitempty"` |
| 21 | + // Modulus to take of the hash of concatenated values from the source labels. |
| 22 | + Modulus uint64 `json:"modulus,omitempty"` |
| 23 | + // TargetLabel is the label to which the resulting string is written in a replacement. |
| 24 | + // Regexp interpolation is allowed for the replace action. |
| 25 | + TargetLabel string `json:"target_label,omitempty"` |
| 26 | + // Replacement is the regex replacement pattern to be used. |
| 27 | + Replacement string `json:"replacement,omitempty"` |
| 28 | + // Action is the action to be performed for the relabeling. |
| 29 | + Action string `json:"action,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// UnmarshalJSON implements the json.Unmarshaler interface. |
| 33 | +func (rc *RelabelConfig) UnmarshalJSON(data []byte) error { |
| 34 | + *rc = RelabelConfig{ |
| 35 | + Action: string(relabel.Replace), |
| 36 | + Separator: ";", |
| 37 | + Regex: "(.*)", |
| 38 | + Replacement: "$1", |
| 39 | + } |
| 40 | + type plain RelabelConfig |
| 41 | + if err := json.Unmarshal(data, (*plain)(rc)); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +// ToPrometheusConfig converts our JSON-friendly RelabelConfig to the Prometheus RelabelConfig |
| 48 | +func (rc *RelabelConfig) ToPrometheusConfig() (*relabel.Config, error) { |
| 49 | + var regex relabel.Regexp |
| 50 | + if rc.Regex != "" { |
| 51 | + var err error |
| 52 | + regex, err = relabel.NewRegexp(rc.Regex) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("invalid regex %q: %w", rc.Regex, err) |
| 55 | + } |
| 56 | + } else { |
| 57 | + regex = relabel.DefaultRelabelConfig.Regex |
| 58 | + } |
| 59 | + |
| 60 | + action := relabel.Action(rc.Action) |
| 61 | + if rc.Action == "" { |
| 62 | + action = relabel.DefaultRelabelConfig.Action |
| 63 | + } |
| 64 | + |
| 65 | + separator := rc.Separator |
| 66 | + if separator == "" { |
| 67 | + separator = relabel.DefaultRelabelConfig.Separator |
| 68 | + } |
| 69 | + |
| 70 | + replacement := rc.Replacement |
| 71 | + if replacement == "" { |
| 72 | + replacement = relabel.DefaultRelabelConfig.Replacement |
| 73 | + } |
| 74 | + |
| 75 | + sourceLabels := make(model.LabelNames, 0, len(rc.SourceLabels)) |
| 76 | + for _, l := range rc.SourceLabels { |
| 77 | + sourceLabels = append(sourceLabels, model.LabelName(l)) |
| 78 | + } |
| 79 | + |
| 80 | + cfg := &relabel.Config{ |
| 81 | + SourceLabels: sourceLabels, |
| 82 | + Separator: separator, |
| 83 | + Regex: regex, |
| 84 | + Modulus: rc.Modulus, |
| 85 | + TargetLabel: rc.TargetLabel, |
| 86 | + Replacement: replacement, |
| 87 | + Action: action, |
| 88 | + } |
| 89 | + |
| 90 | + if err := cfg.Validate(); err != nil { |
| 91 | + return nil, fmt.Errorf("invalid relabel config: %w", err) |
| 92 | + } |
| 93 | + return cfg, nil |
| 94 | +} |
| 95 | + |
| 96 | +func ToPrometheusConfigs(cfgs []*RelabelConfig) ([]*relabel.Config, error) { |
| 97 | + promConfigs := make([]*relabel.Config, 0, len(cfgs)) |
| 98 | + for _, cfg := range cfgs { |
| 99 | + promCfg, err := cfg.ToPrometheusConfig() |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("invalid relabel config: %w", err) |
| 102 | + } |
| 103 | + promConfigs = append(promConfigs, promCfg) |
| 104 | + } |
| 105 | + return promConfigs, nil |
| 106 | +} |
| 107 | + |
| 108 | +func parseRelabelConfigs(relabelConfigsRaw string) ([]*relabel.Config, error) { |
| 109 | + if relabelConfigsRaw == "" { |
| 110 | + return nil, nil |
| 111 | + } |
| 112 | + |
| 113 | + var relabelConfigs []*RelabelConfig |
| 114 | + |
| 115 | + if err := json.Unmarshal([]byte(relabelConfigsRaw), &relabelConfigs); err != nil { |
| 116 | + return nil, fmt.Errorf("failed to parse RELABEL_CONFIGS: %v", err) |
| 117 | + } |
| 118 | + promConfigs, err := ToPrometheusConfigs(relabelConfigs) |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("failed to parse RELABEL_CONFIGS: %v", err) |
| 121 | + } |
| 122 | + return promConfigs, nil |
| 123 | +} |
0 commit comments