Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/prometheus] set user agent #21914

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/prometheus-useragent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: The prometheus receiver now sets a full, versioned user agent.

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

# (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:
8 changes: 7 additions & 1 deletion receiver/prometheusreceiver/metrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/go-kit/log"
"github.com/mitchellh/hashstructure/v2"
commonconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
Expand Down Expand Up @@ -281,7 +282,12 @@ func (r *pReceiver) initPrometheusComponents(ctx context.Context, host component
if err != nil {
return err
}
r.scrapeManager = scrape.NewManager(&scrape.Options{PassMetadataInContext: true}, logger, store)
r.scrapeManager = scrape.NewManager(&scrape.Options{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any means we can test this within the project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to avoid duplicating most of testComponent to test this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make sure that this doesn't regress again in future.

PassMetadataInContext: true,
HTTPClientOptions: []commonconfig.HTTPClientOption{
commonconfig.WithUserAgent(r.settings.BuildInfo.Command + "/" + r.settings.BuildInfo.Version),
},
}, logger, store)

go func() {
// The scrape manager needs to wait for the configuration to be loaded before beginning
Expand Down
48 changes: 47 additions & 1 deletion receiver/prometheusreceiver/metrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
package prometheusreceiver

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

gokitlog "github.com/go-kit/log"
"github.com/prometheus/common/model"
promConfig "github.com/prometheus/prometheus/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/receivertest"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -1428,7 +1438,6 @@ func TestUntypedMetrics(t *testing.T) {
}

testComponent(t, targets, false, "", featuregate.GlobalRegistry())

}

func verifyUntypedMetrics(t *testing.T, td *testData, resourceMetrics []pmetric.ResourceMetrics) {
Expand Down Expand Up @@ -1523,3 +1532,40 @@ func TestGCInterval(t *testing.T) {
})
}
}

func TestUserAgent(t *testing.T) {
uaCh := make(chan string, 1)
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case uaCh <- r.UserAgent():
default:
}
}))
defer svr.Close()

cfg, err := promConfig.Load(fmt.Sprintf(`
scrape_configs:
- job_name: foo
scrape_interval: 100ms
static_configs:
- targets:
- %s
`, strings.TrimPrefix(svr.URL, "http://")), false, gokitlog.NewNopLogger())
require.NoError(t, err)
set := receivertest.NewNopCreateSettings()
receiver := newPrometheusReceiver(set, &Config{
PrometheusConfig: cfg,
}, new(consumertest.MetricsSink), featuregate.GlobalRegistry())

ctx := context.Background()

require.NoError(t, receiver.Start(ctx, componenttest.NewNopHost()))
t.Cleanup(func() {
require.NoError(t, receiver.Shutdown(ctx))
})

gotUA := <-uaCh

require.Contains(t, gotUA, set.BuildInfo.Command)
require.Contains(t, gotUA, set.BuildInfo.Version)
}