Skip to content

Commit

Permalink
Merge pull request open-telemetry#101 from IshwarKanse/rebase-bot-main
Browse files Browse the repository at this point in the history
  • Loading branch information
openshift-merge-bot[bot] authored Nov 13, 2024
2 parents e14e1dd + 12fcb0d commit a0e8286
Show file tree
Hide file tree
Showing 23 changed files with 289 additions and 140 deletions.
19 changes: 19 additions & 0 deletions .chloggen/3446.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Operator pod crashed if the Service Monitor for the operator metrics was created before by another operator pod.

# One or more tracking issues related to the change
issues: [3446]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
Operator fails when the pod is restarted and the Service Monitor for operator metrics was already created by another operator pod.
To fix this, the operator now sets the owner reference on the Service Monitor to itself and checks if the Service Monitor already exists.
10 changes: 5 additions & 5 deletions cmd/otel-allocator/allocation/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package allocation
import (
"testing"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
Expand Down Expand Up @@ -176,11 +176,11 @@ func TestAllocationCollision(t *testing.T) {

cols := MakeNCollectors(3, 0)
allocator.SetCollectors(cols)
firstLabels := model.LabelSet{
"test": "test1",
firstLabels := labels.Labels{
{Name: "test", Value: "test1"},
}
secondLabels := model.LabelSet{
"test": "test2",
secondLabels := labels.Labels{
{Name: "test", Value: "test2"},
}
firstTarget := target.NewItem("sample-name", "0.0.0.0:8000", firstLabels, "")
secondTarget := target.NewItem("sample-name", "0.0.0.0:8000", secondLabels, "")
Expand Down
3 changes: 1 addition & 2 deletions cmd/otel-allocator/allocation/consistent_hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package allocation

import (
"fmt"
"strings"

"github.com/buraksezer/consistent"
"github.com/cespare/xxhash/v2"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (s *consistentHashingStrategy) GetName() string {
}

func (s *consistentHashingStrategy) GetCollectorForTarget(collectors map[string]*Collector, item *target.Item) (*Collector, error) {
hashKey := strings.Join(item.TargetURL, "")
hashKey := item.TargetURL
member := s.consistentHasher.LocateKey([]byte(hashKey))
collectorName := member.String()
collector, ok := collectors[collectorName]
Expand Down
26 changes: 13 additions & 13 deletions cmd/otel-allocator/allocation/per_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package allocation
import (
"testing"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -33,23 +33,23 @@ func TestAllocationPerNode(t *testing.T) {

cols := MakeNCollectors(4, 0)
s.SetCollectors(cols)
firstLabels := model.LabelSet{
"test": "test1",
"__meta_kubernetes_pod_node_name": "node-0",
firstLabels := labels.Labels{
{Name: "test", Value: "test1"},
{Name: "__meta_kubernetes_pod_node_name", Value: "node-0"},
}
secondLabels := model.LabelSet{
"test": "test2",
"__meta_kubernetes_node_name": "node-1",
secondLabels := labels.Labels{
{Name: "test", Value: "test2"},
{Name: "__meta_kubernetes_node_name", Value: "node-1"},
}
// no label, should be skipped
thirdLabels := model.LabelSet{
"test": "test3",
thirdLabels := labels.Labels{
{Name: "test", Value: "test3"},
}
// endpointslice target kind and name
fourthLabels := model.LabelSet{
"test": "test4",
"__meta_kubernetes_endpointslice_address_target_kind": "Node",
"__meta_kubernetes_endpointslice_address_target_name": "node-3",
fourthLabels := labels.Labels{
{Name: "test", Value: "test4"},
{Name: "__meta_kubernetes_endpointslice_address_target_kind", Value: "Node"},
{Name: "__meta_kubernetes_endpointslice_address_target_name", Value: "node-3"},
}

firstTarget := target.NewItem("sample-name", "0.0.0.0:8000", firstLabels, "")
Expand Down
16 changes: 8 additions & 8 deletions cmd/otel-allocator/allocation/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strconv"
"testing"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -39,9 +39,9 @@ func MakeNNewTargets(n int, numCollectors int, startingIndex int) map[string]*ta
toReturn := map[string]*target.Item{}
for i := startingIndex; i < n+startingIndex; i++ {
collector := fmt.Sprintf("collector-%d", colIndex(i, numCollectors))
label := model.LabelSet{
"i": model.LabelValue(strconv.Itoa(i)),
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
label := labels.Labels{
{Name: "i", Value: strconv.Itoa(i)},
{Name: "total", Value: strconv.Itoa(n + startingIndex)},
}
newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, collector)
toReturn[newTarget.Hash()] = newTarget
Expand All @@ -65,10 +65,10 @@ func MakeNCollectors(n int, startingIndex int) map[string]*Collector {
func MakeNNewTargetsWithEmptyCollectors(n int, startingIndex int) map[string]*target.Item {
toReturn := map[string]*target.Item{}
for i := startingIndex; i < n+startingIndex; i++ {
label := model.LabelSet{
"i": model.LabelValue(strconv.Itoa(i)),
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
"__meta_kubernetes_pod_node_name": model.LabelValue("node-0"),
label := labels.Labels{
{Name: "i", Value: strconv.Itoa(i)},
{Name: "total", Value: strconv.Itoa(n + startingIndex)},
{Name: "__meta_kubernetes_pod_node_name", Value: "node-0"},
}
newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, "")
toReturn[newTarget.Hash()] = newTarget
Expand Down
25 changes: 21 additions & 4 deletions cmd/otel-allocator/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"

gokitlog "github.com/go-kit/log"
Expand All @@ -26,7 +28,9 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/stretchr/testify/require"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -45,13 +49,14 @@ func BenchmarkProcessTargets(b *testing.B) {
targetsPerGroup := 5
groupsPerJob := 20
tsets := prepareBenchmarkData(numTargets, targetsPerGroup, groupsPerJob)
labelsBuilder := labels.NewBuilder(labels.EmptyLabels())

b.ResetTimer()
for _, strategy := range allocation.GetRegisteredAllocatorNames() {
b.Run(strategy, func(b *testing.B) {
targetDiscoverer, allocator := createTestDiscoverer(strategy, map[string][]*relabel.Config{})
for i := 0; i < b.N; i++ {
targetDiscoverer.ProcessTargets(tsets, allocator.SetTargets)
targetDiscoverer.ProcessTargets(labelsBuilder, tsets, allocator.SetTargets)
}
})
}
Expand All @@ -64,12 +69,24 @@ func BenchmarkProcessTargetsWithRelabelConfig(b *testing.B) {
targetsPerGroup := 5
groupsPerJob := 20
tsets := prepareBenchmarkData(numTargets, targetsPerGroup, groupsPerJob)
labelsBuilder := labels.NewBuilder(labels.EmptyLabels())
prehookConfig := make(map[string][]*relabel.Config, len(tsets))
for jobName := range tsets {
// keep all targets in half the jobs, drop the rest
jobNrStr := strings.Split(jobName, "-")[1]
jobNr, err := strconv.Atoi(jobNrStr)
require.NoError(b, err)
var action relabel.Action
if jobNr%2 == 0 {
action = "keep"
} else {
action = "drop"
}
prehookConfig[jobName] = []*relabel.Config{
{
Action: "keep",
Regex: relabel.MustNewRegexp(".*"),
Action: action,
Regex: relabel.MustNewRegexp(".*"),
SourceLabels: model.LabelNames{"__address__"},
},
}
}
Expand All @@ -79,7 +96,7 @@ func BenchmarkProcessTargetsWithRelabelConfig(b *testing.B) {
b.Run(strategy, func(b *testing.B) {
targetDiscoverer, allocator := createTestDiscoverer(strategy, prehookConfig)
for i := 0; i < b.N; i++ {
targetDiscoverer.ProcessTargets(tsets, allocator.SetTargets)
targetDiscoverer.ProcessTargets(labelsBuilder, tsets, allocator.SetTargets)
}
})
}
Expand Down
29 changes: 5 additions & 24 deletions cmd/otel-allocator/prehook/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package prehook

import (
"github.com/go-logr/logr"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
Expand All @@ -35,18 +33,6 @@ func NewRelabelConfigTargetFilter(log logr.Logger) Hook {
}
}

// helper function converts from model.LabelSet to []labels.Label.
func convertLabelToPromLabelSet(lbls model.LabelSet) []labels.Label {
newLabels := make([]labels.Label, len(lbls))
index := 0
for k, v := range lbls {
newLabels[index].Name = string(k)
newLabels[index].Value = string(v)
index++
}
return newLabels
}

func (tf *RelabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[string]*target.Item {
numTargets := len(targets)

Expand All @@ -57,20 +43,15 @@ func (tf *RelabelConfigTargetFilter) Apply(targets map[string]*target.Item) map[

// Note: jobNameKey != tItem.JobName (jobNameKey is hashed)
for jobNameKey, tItem := range targets {
keepTarget := true
lset := convertLabelToPromLabelSet(tItem.Labels)
var keepTarget bool
lset := tItem.Labels
for _, cfg := range tf.relabelCfg[tItem.JobName] {
if newLset, keep := relabel.Process(lset, cfg); !keep {
keepTarget = false
lset, keepTarget = relabel.Process(lset, cfg)
if !keepTarget {
delete(targets, jobNameKey)
break // inner loop
} else {
lset = newLset
}
}

if !keepTarget {
delete(targets, jobNameKey)
}
}

tf.log.V(2).Info("Filtering complete", "seen", numTargets, "kept", len(targets))
Expand Down
9 changes: 5 additions & 4 deletions cmd/otel-allocator/prehook/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/stretchr/testify/assert"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -184,10 +185,10 @@ func makeNNewTargets(rCfgs []relabelConfigObj, n int, numCollectors int, startin
relabelConfig := make(map[string][]*relabel.Config)
for i := startingIndex; i < n+startingIndex; i++ {
collector := fmt.Sprintf("collector-%d", colIndex(i, numCollectors))
label := model.LabelSet{
"collector": model.LabelValue(collector),
"i": model.LabelValue(strconv.Itoa(i)),
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
label := labels.Labels{
{Name: "collector", Value: collector},
{Name: "i", Value: strconv.Itoa(i)},
{Name: "total", Value: strconv.Itoa(n + startingIndex)},
}
jobName := fmt.Sprintf("test-job-%d", i)
newTarget := target.NewItem(jobName, "test-url", label, collector)
Expand Down
12 changes: 7 additions & 5 deletions cmd/otel-allocator/server/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation"
Expand Down Expand Up @@ -249,12 +250,13 @@ func makeNCollectorJSON(random rand.Rand, numCollectors, numItems int) map[strin
}

func makeNTargetItems(random rand.Rand, numItems, numLabels int) []*target.Item {
builder := labels.NewBuilder(labels.EmptyLabels())
items := make([]*target.Item, 0, numItems)
for i := 0; i < numItems; i++ {
items = append(items, target.NewItem(
randSeq(random, 80),
randSeq(random, 150),
makeNNewLabels(random, numLabels),
makeNNewLabels(builder, random, numLabels),
randSeq(random, 30),
))
}
Expand All @@ -270,10 +272,10 @@ func makeNTargetJSON(random rand.Rand, numItems, numLabels int) []*targetJSON {
return targets
}

func makeNNewLabels(random rand.Rand, n int) model.LabelSet {
labels := make(map[model.LabelName]model.LabelValue, n)
func makeNNewLabels(builder *labels.Builder, random rand.Rand, n int) labels.Labels {
builder.Reset(labels.EmptyLabels())
for i := 0; i < n; i++ {
labels[model.LabelName(randSeq(random, 20))] = model.LabelValue(randSeq(random, 20))
builder.Set(randSeq(random, 20), randSeq(random, 20))
}
return labels
return builder.Labels()
}
8 changes: 4 additions & 4 deletions cmd/otel-allocator/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
promcommconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/allocation"
Expand Down Expand Up @@ -67,8 +67,8 @@ type linkJSON struct {
}

type targetJSON struct {
TargetURL []string `json:"targets"`
Labels model.LabelSet `json:"labels"`
TargetURL []string `json:"targets"`
Labels labels.Labels `json:"labels"`
}

type Server struct {
Expand Down Expand Up @@ -374,7 +374,7 @@ func registerPprof(g *gin.RouterGroup) {

func targetJsonFromTargetItem(item *target.Item) *targetJSON {
return &targetJSON{
TargetURL: item.TargetURL,
TargetURL: []string{item.TargetURL},
Labels: item.Labels,
}
}
Loading

0 comments on commit a0e8286

Please sign in to comment.