Skip to content

Commit

Permalink
[receiver/prometheus] syncTargetAllocator now detects regex changes i…
Browse files Browse the repository at this point in the history
…n relabel config (#32127)

**Description:** <Describe what has changed.>
Fixing a bug - With the Targetallocator component in prometheus
receiver, when prometheus scrape config is updated for metric relabel
config with just regex change, the prometheus metrics receiver doesn't
update the hash and hence doesn't pick up the metrics relabel config
with the new regex.
This is because the **regex** struct has unexported fields. This fix
similar to the fix made in
[opentelemetry-operator](open-telemetry/opentelemetry-operator#2171)
fixes this issue by taking into account the unexported fields as well.


**Link to tracking Issue:** - #29313 

**Testing:** Tested to make sure that just the regex changes in metric
relabeling gets picked up with TargetAllocator enabled.

Reopening this PR since the old
one(#30258)
got closed due to inactivity.

---------

Co-authored-by: David Ashpole <[email protected]>
  • Loading branch information
rashmichandrashekar and dashpole authored Jul 11, 2024
1 parent e7be077 commit 42c03a2
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .chloggen/fix_promTAHashComputation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
change_type: 'bug_fix'
component: 'prometheusreceiver'
note: Fix hash computation to include non exported fields like regex in scrape configuration for TargetAllocator
issues: [29313]
subtext:
change_logs: []
1 change: 0 additions & 1 deletion cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cmd/oteltestbedcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ require (
github.com/miekg/dns v1.1.59 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions cmd/oteltestbedcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions connector/datadogconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ require (
github.com/miekg/dns v1.1.59 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions exporter/datadogexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions exporter/datadogexporter/integrationtest/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion exporter/prometheusexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ require (
github.com/miekg/dns v1.1.59 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions exporter/prometheusexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion receiver/prometheusreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/go-kit/log v0.2.1
github.com/gogo/protobuf v1.3.2
github.com/golang/snappy v0.0.4
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.104.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.104.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.104.0
Expand Down
2 changes: 0 additions & 2 deletions receiver/prometheusreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions receiver/prometheusreceiver/metrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"context"
"errors"
"fmt"
"hash/fnv"
"io"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"sort"
"sync"
"time"
"unsafe"

"github.com/go-kit/log"
"github.com/mitchellh/hashstructure/v2"
"github.com/prometheus/client_golang/prometheus"
commonconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -145,6 +146,33 @@ func (r *pReceiver) startTargetAllocator(allocConf *TargetAllocator, baseCfg *Pr
return nil
}

// Calculate a hash for a scrape config map.
// This is done by marshaling to YAML because it's the most straightforward and doesn't run into problems with unexported fields.
func getScrapeConfigHash(jobToScrapeConfig map[string]*config.ScrapeConfig) (uint64, error) {
var err error
hash := fnv.New64()
yamlEncoder := yaml.NewEncoder(hash)

jobKeys := make([]string, 0, len(jobToScrapeConfig))
for jobName := range jobToScrapeConfig {
jobKeys = append(jobKeys, jobName)
}
sort.Strings(jobKeys)

for _, jobName := range jobKeys {
_, err = hash.Write([]byte(jobName))
if err != nil {
return 0, err
}
err = yamlEncoder.Encode(jobToScrapeConfig[jobName])
if err != nil {
return 0, err
}
}
yamlEncoder.Close()
return hash.Sum64(), err
}

// syncTargetAllocator request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
// baseDiscoveryCfg can be used to provide additional ScrapeConfigs which will be added to the retrieved jobs.
func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAllocator, baseCfg *PromConfig) (uint64, error) {
Expand All @@ -155,7 +183,7 @@ func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAll
return 0, err
}

hash, err := hashstructure.Hash(scrapeConfigsResponse, hashstructure.FormatV2, nil)
hash, err := getScrapeConfigHash(scrapeConfigsResponse)
if err != nil {
r.settings.Logger.Error("Failed to hash job list", zap.Error(err))
return 0, err
Expand Down
Loading

0 comments on commit 42c03a2

Please sign in to comment.