Skip to content

Commit 6e092c1

Browse files
perebajArthurSens
andauthored
[prometheusremotewritereceiver] add obsreport (#40399)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Adding the obs report component The discussion to add this component started [here](#38812 (comment)) The last commit the we reviewed before I screw up the whole branch https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/39828/files/13688834d2674c61ced371b5500abb4e901e8183 --------- Co-authored-by: Arthur Silva Sens <[email protected]>
1 parent 5a34fb7 commit 6e092c1

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

.chloggen/add-obsreport-prw.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: 'prometheusremotewritereceiver'
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Improve observability about data ingestion
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37277]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/prometheusremotewritereceiver/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
go.opentelemetry.io/collector/consumer/consumertest v0.127.1-0.20250602081514-8568c97b0d15
2121
go.opentelemetry.io/collector/pdata v1.33.1-0.20250602081514-8568c97b0d15
2222
go.opentelemetry.io/collector/receiver v1.33.1-0.20250602081514-8568c97b0d15
23+
go.opentelemetry.io/collector/receiver/receiverhelper v0.127.1-0.20250602081514-8568c97b0d15
2324
go.opentelemetry.io/collector/receiver/receivertest v0.127.1-0.20250602081514-8568c97b0d15
2425
go.uber.org/goleak v1.3.0
2526
go.uber.org/zap v1.27.0

receiver/prometheusremotewritereceiver/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/prometheusremotewritereceiver/receiver.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"go.opentelemetry.io/collector/pdata/pcommon"
2626
"go.opentelemetry.io/collector/pdata/pmetric"
2727
"go.opentelemetry.io/collector/receiver"
28+
"go.opentelemetry.io/collector/receiver/receiverhelper"
2829
"go.uber.org/zap/zapcore"
2930

3031
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity"
@@ -35,6 +36,7 @@ func newRemoteWriteReceiver(settings receiver.Settings, cfg *Config, nextConsume
3536
if err != nil {
3637
return nil, fmt.Errorf("failed to create LRU cache: %w", err)
3738
}
39+
3840
return &prometheusRemoteWriteReceiver{
3941
settings: settings,
4042
nextConsumer: nextConsumer,
@@ -55,6 +57,7 @@ type prometheusRemoteWriteReceiver struct {
5557
wg sync.WaitGroup
5658

5759
rmCache *lru.Cache[uint64, pmetric.ResourceMetrics]
60+
obsrecv *receiverhelper.ObsReport
5861
}
5962

6063
// MetricIdentity contains all the components that uniquely identify a metric
@@ -101,6 +104,14 @@ func (prw *prometheusRemoteWriteReceiver) Start(ctx context.Context, host compon
101104
mux := http.NewServeMux()
102105
mux.HandleFunc("/api/v1/write", prw.handlePRW)
103106
var err error
107+
prw.obsrecv, err = receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
108+
ReceiverID: prw.settings.ID,
109+
ReceiverCreateSettings: prw.settings,
110+
Transport: "http",
111+
})
112+
if err != nil {
113+
return fmt.Errorf("failed to create obsreport: %w", err)
114+
}
104115

105116
prw.server, err = prw.config.ToServer(ctx, host, prw.settings.TelemetrySettings, mux)
106117
if err != nil {
@@ -179,8 +190,13 @@ func (prw *prometheusRemoteWriteReceiver) handlePRW(w http.ResponseWriter, req *
179190
}
180191

181192
w.WriteHeader(http.StatusNoContent)
182-
// TODO(@perebaj): Evaluate if we should use the obsreport here. Ref: https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/38812#discussion_r2053094391
183-
_ = prw.nextConsumer.ConsumeMetrics(req.Context(), m)
193+
194+
obsrecvCtx := prw.obsrecv.StartMetricsOp(req.Context())
195+
err = prw.nextConsumer.ConsumeMetrics(req.Context(), m)
196+
if err != nil {
197+
prw.settings.Logger.Error("Error consuming metrics", zapcore.Field{Key: "error", Type: zapcore.ErrorType, Interface: err})
198+
}
199+
prw.obsrecv.EndMetricsOp(obsrecvCtx, "prometheusremotewritereceiver", m.ResourceMetrics().Len(), err)
184200
}
185201

186202
// parseProto parses the content-type header and returns the version of the remote-write protocol.

receiver/prometheusremotewritereceiver/receiver_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import (
2020
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
2121
"github.com/prometheus/prometheus/storage/remote"
2222
"github.com/stretchr/testify/assert"
23+
"go.opentelemetry.io/collector/component"
2324
"go.opentelemetry.io/collector/consumer"
2425
"go.opentelemetry.io/collector/consumer/consumertest"
2526
"go.opentelemetry.io/collector/pdata/pcommon"
2627
"go.opentelemetry.io/collector/pdata/pmetric"
28+
"go.opentelemetry.io/collector/receiver/receiverhelper"
2729
"go.opentelemetry.io/collector/receiver/receivertest"
2830

2931
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
@@ -63,6 +65,16 @@ func setupMetricsReceiver(t *testing.T) *prometheusRemoteWriteReceiver {
6365
prwReceiver, err := factory.CreateMetrics(context.Background(), receivertest.NewNopSettings(metadata.Type), cfg, consumertest.NewNop())
6466
assert.NoError(t, err)
6567
assert.NotNil(t, prwReceiver, "metrics receiver creation failed")
68+
69+
receiverID := component.MustNewID("test")
70+
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
71+
ReceiverID: receiverID,
72+
Transport: "http",
73+
ReceiverCreateSettings: receivertest.NewNopSettings(metadata.Type),
74+
})
75+
assert.NoError(t, err)
76+
77+
prwReceiver.(*prometheusRemoteWriteReceiver).obsrecv = obsrecv
6678
writeReceiver := prwReceiver.(*prometheusRemoteWriteReceiver)
6779

6880
// Add cleanup to ensure LRU cache is properly purged

0 commit comments

Comments
 (0)