forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/prometheus] include scrape configs provided via `scrape_con…
…fig_files` (open-telemetry#34897) **Description:** This PR fixes a bug in the prometheus receiver where the scrape configs provided via `scrape_config_files` were not applied. Using `scrape_config_files` instead of providing the scrape configs directly does come with some limitations regarding the use of env vars, as also mentioned in open-telemetry#34786 (comment). **Link to tracking Issue:** open-telemetry#34786 **Testing:** Added unit tests --------- Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com> Co-authored-by: Antoine Toulme <antoine@toulme.name> Co-authored-by: David Ashpole <dashpole@google.com>
- Loading branch information
Showing
7 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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. filelogreceiver) | ||
component: prometheusreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Fix the retrieval of scrape configurations by also considering scrape config files | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34786] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
receiver/prometheusreceiver/metrics_receiver_scrape_config_files_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package prometheusreceiver | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.27.0" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var scrapeFileTargetPage = ` | ||
# A simple counter | ||
# TYPE foo counter | ||
foo1 1 | ||
` | ||
|
||
// TestScrapeConfigFiles validates that scrape configs provided via scrape_config_files are | ||
// also considered and added to the applied configuration | ||
|
||
func TestScrapeConfigFiles(t *testing.T) { | ||
setMetricsTimestamp() | ||
targets := []*testData{ | ||
{ | ||
name: "target1", | ||
pages: []mockPrometheusResponse{ | ||
{code: 200, data: scrapeFileTargetPage}, | ||
}, | ||
validateFunc: verifyScrapeConfigFiles, | ||
}, | ||
} | ||
|
||
testComponent(t, targets, func(cfg *Config) { | ||
// take the generated scrape config and move it into a file instead | ||
marshalledScrapeConfigs, err := yaml.Marshal(cfg.PrometheusConfig.ScrapeConfigs) | ||
require.NoError(t, err) | ||
tmpDir := t.TempDir() | ||
cfgFileName := fmt.Sprintf("%s/test-scrape-config.yaml", tmpDir) | ||
scrapeConfigFileContent := fmt.Sprintf("scrape_configs:\n%s", string(marshalledScrapeConfigs)) | ||
err = os.WriteFile(cfgFileName, []byte(scrapeConfigFileContent), 0400) | ||
require.NoError(t, err) | ||
cfg.PrometheusConfig.ScrapeConfigs = []*config.ScrapeConfig{} | ||
cfg.PrometheusConfig.ScrapeConfigFiles = []string{cfgFileName} | ||
}) | ||
} | ||
|
||
func verifyScrapeConfigFiles(t *testing.T, _ *testData, result []pmetric.ResourceMetrics) { | ||
require.Len(t, result, 1) | ||
serviceName, ok := result[0].Resource().Attributes().Get(semconv.AttributeServiceName) | ||
assert.True(t, ok) | ||
assert.Equal(t, "target1", serviceName.Str()) | ||
assert.Equal(t, 6, result[0].ScopeMetrics().At(0).Metrics().Len()) | ||
metricFound := false | ||
|
||
for i := 0; i < result[0].ScopeMetrics().At(0).Metrics().Len(); i++ { | ||
if result[0].ScopeMetrics().At(0).Metrics().At(i).Name() == "foo1" { | ||
metricFound = true | ||
break | ||
} | ||
} | ||
assert.True(t, metricFound) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
receiver/prometheusreceiver/testdata/config_scrape_config_files.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
prometheus: | ||
trim_metric_suffixes: true | ||
use_start_time_metric: true | ||
start_time_metric_regex: '^(.+_)*process_start_time_seconds$' | ||
report_extra_scrape_metrics: true | ||
config: | ||
scrape_config_files: | ||
- ./testdata/scrape-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scrape_configs: | ||
- job_name: 'demo' | ||
scrape_interval: 5s |