Skip to content

Commit

Permalink
env variable substitution for
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Nov 8, 2022
1 parent e833482 commit 5577956
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 41 deletions.
26 changes: 26 additions & 0 deletions cmd/otel-allocator/prehook/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package prehook

import (
"bytes"
"os"

"github.com/go-logr/logr"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
yaml2 "gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
)
Expand Down Expand Up @@ -82,6 +87,19 @@ func (tf *RelabelConfigTargetFilter) SetConfig(cfgs map[string][]*relabel.Config
for key, val := range cfgs {
relabelCfgCopy[key] = val
}

out, err := yaml2.Marshal(relabelCfgCopy)
if err != nil {
tf.log.V(2).Info("Error Marshalling", "error", err)
return
}

byteArr := replaceShard([]byte(out))
err = yaml2.Unmarshal(byteArr, &relabelCfgCopy)
if err != nil {
tf.log.Info("Error Unmarshalling", "error", err)
return
}
tf.relabelCfg = relabelCfgCopy
}

Expand All @@ -93,6 +111,14 @@ func (tf *RelabelConfigTargetFilter) GetConfig() map[string][]*relabel.Config {
return relabelCfgCopy
}

func replaceShard(body []byte) []byte {
shard, ok := os.LookupEnv("SHARD")
if !ok {
shard = "0"
}
return bytes.ReplaceAll(body, []byte("$(SHARD)"), []byte(shard))
}

func init() {
err := Register(relabelConfigTargetFilterName, NewRelabelConfigTargetFilter)
if err != nil {
Expand Down
147 changes: 106 additions & 41 deletions cmd/otel-allocator/prehook/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,72 +35,127 @@ var (

relabelConfigs = []relabelConfigObj{
{
cfg: relabel.Config{
Action: "replace",
Separator: ";",
Regex: relabel.MustNewRegexp("(.*)"),
Replacement: "$1",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Action: "replace",
Separator: ";",
Regex: relabel.MustNewRegexp("(.*)"),
Replacement: "$1",
TargetLabel: "foo",
},
},
isDrop: false,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Action: "keep",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Separator: ";",
Action: "keep",
Replacement: "$1",
},
},
isDrop: false,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("bad.*match"),
Action: "drop",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("bad.*match"),
Action: "drop",
Separator: ";",
Replacement: "$1",
},
},
isDrop: false,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"label_not_present"},
Regex: relabel.MustNewRegexp("(.*)"),
Action: "keep",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"label_not_present"},
Regex: relabel.MustNewRegexp("(.*)"),
Separator: ";",
Action: "keep",
Replacement: "$1",
},
},
isDrop: false,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Action: "drop",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Separator: ";",
Action: "drop",
Replacement: "$1",
},
},
isDrop: true,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"collector"},
Regex: relabel.MustNewRegexp("(collector.*)"),
Action: "drop",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"collector"},
Regex: relabel.MustNewRegexp("(collector.*)"),
Separator: ";",
Action: "drop",
Replacement: "$1",
},
},
isDrop: true,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("bad.*match"),
Action: "keep",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("bad.*match"),
Separator: ";",
Action: "keep",
Replacement: "$1",
},
},
isDrop: true,
},
{
cfg: relabel.Config{
SourceLabels: model.LabelNames{"collector"},
Regex: relabel.MustNewRegexp("collectors-n"),
Action: "keep",
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"collector"},
Regex: relabel.MustNewRegexp("collectors-n"),
Separator: ";",
Action: "keep",
Replacement: "$1",
},
},
isDrop: true,
},
}

HashmodConfig = relabelConfigObj{
cfg: []*relabel.Config{
{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Separator: ";",
Modulus: 1,
TargetLabel: "tmp",
Action: "hashmod",
Replacement: "$1",
},

{
SourceLabels: model.LabelNames{"tmp"},
Regex: relabel.MustNewRegexp("$(SHARD)"),
Separator: ";",
Action: "keep",
Replacement: "$1",
},
},
isDrop: false,
}

DefaultDropRelabelConfig = relabel.Config{
SourceLabels: model.LabelNames{"i"},
Regex: relabel.MustNewRegexp("(.*)"),
Expand All @@ -109,7 +164,7 @@ var (
)

type relabelConfigObj struct {
cfg relabel.Config
cfg []*relabel.Config
isDrop bool
}

Expand All @@ -120,7 +175,7 @@ func colIndex(index, numCols int) int {
return index % numCols
}

func makeNNewTargets(n int, numCollectors int, startingIndex int) (map[string]*target.Item, int, map[string]*target.Item, map[string][]*relabel.Config) {
func makeNNewTargets(rCfgs []relabelConfigObj, n int, numCollectors int, startingIndex int) (map[string]*target.Item, int, map[string]*target.Item, map[string][]*relabel.Config) {
toReturn := map[string]*target.Item{}
expectedMap := make(map[string]*target.Item)
numItemsRemaining := n
Expand All @@ -140,9 +195,7 @@ func makeNNewTargets(n int, numCollectors int, startingIndex int) (map[string]*t

index = int(ind.Int64())

relabelConfig[jobName] = []*relabel.Config{
&relabelConfigs[index].cfg,
}
relabelConfig[jobName] = rCfgs[index].cfg

targetKey := newTarget.Hash()
if relabelConfigs[index].isDrop {
Expand All @@ -159,7 +212,7 @@ func TestApply(t *testing.T) {
allocatorPrehook := New("relabel-config", logger)
assert.NotNil(t, allocatorPrehook)

targets, numRemaining, expectedTargetMap, relabelCfg := makeNNewTargets(numTargets, 3, 0)
targets, numRemaining, expectedTargetMap, relabelCfg := makeNNewTargets(relabelConfigs, numTargets, 3, 0)
allocatorPrehook.SetConfig(relabelCfg)
remainingItems := allocatorPrehook.Apply(targets)
assert.Len(t, remainingItems, numRemaining)
Expand All @@ -178,12 +231,24 @@ func TestApply(t *testing.T) {
assert.Equal(t, remainingItems, targets)
}

func TestApplyHashmodAction(t *testing.T) {
allocatorPrehook := New("relabel-config", logger)
assert.NotNil(t, allocatorPrehook)

hashRelabelConfigs := append(relabelConfigs, HashmodConfig)
targets, numRemaining, expectedTargetMap, relabelCfg := makeNNewTargets(hashRelabelConfigs, numTargets, 3, 0)
allocatorPrehook.SetConfig(relabelCfg)
remainingItems := allocatorPrehook.Apply(targets)
assert.Len(t, remainingItems, numRemaining)
assert.Equal(t, remainingItems, expectedTargetMap)
}

func TestApplyEmptyRelabelCfg(t *testing.T) {

allocatorPrehook := New("relabel-config", logger)
assert.NotNil(t, allocatorPrehook)

targets, _, _, _ := makeNNewTargets(numTargets, 3, 0)
targets, _, _, _ := makeNNewTargets(relabelConfigs, numTargets, 3, 0)

relabelCfg := map[string][]*relabel.Config{}
allocatorPrehook.SetConfig(relabelCfg)
Expand All @@ -197,7 +262,7 @@ func TestSetConfig(t *testing.T) {
allocatorPrehook := New("relabel-config", logger)
assert.NotNil(t, allocatorPrehook)

_, _, _, relabelCfg := makeNNewTargets(numTargets, 3, 0)
_, _, _, relabelCfg := makeNNewTargets(relabelConfigs, numTargets, 3, 0)
allocatorPrehook.SetConfig(relabelCfg)
assert.Equal(t, relabelCfg, allocatorPrehook.GetConfig())
}

0 comments on commit 5577956

Please sign in to comment.