Skip to content

[prometheuswritereceiver] target info #38812

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

Merged
merged 32 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
131a3b8
test cases
perebaj Mar 19, 2025
1ca4ec8
improve testcase
perebaj Mar 19, 2025
b9dfc9f
mock consumer metrics
perebaj Mar 21, 2025
288c6bd
clean interRequestCache for each run
perebaj Mar 21, 2025
f2c0671
add target info cache
perebaj Mar 21, 2025
bd80734
Update receiver/prometheusremotewritereceiver/receiver.go
perebaj Mar 21, 2025
07fe080
Update receiver/prometheusremotewritereceiver/receiver.go
perebaj Mar 21, 2025
82d52a7
Update receiver/prometheusremotewritereceiver/receiver.go
perebaj Mar 21, 2025
2ddf810
Update receiver/prometheusremotewritereceiver/receiver.go
perebaj Mar 21, 2025
9f19aed
metric name target_info
perebaj Apr 21, 2025
499f0de
Merge branch 'target-info' of github.com:perebaj/opentelemetry-collec…
perebaj Apr 21, 2025
ddce741
remove t.Run
perebaj Apr 21, 2025
14f989d
fix conflict on go.modules and go.sum
perebaj Apr 21, 2025
5a08906
validate expected metric in the end of request
perebaj Apr 21, 2025
2451c27
add receiverhelper.ObsReport
perebaj Apr 21, 2025
f262c7c
Merge branch 'main' into target-info
perebaj Apr 21, 2025
42390ff
Update .chloggen/target-info.yaml
perebaj Apr 22, 2025
3026ee4
Update .chloggen/target-info.yaml
perebaj Apr 22, 2025
7739e72
remove obsreport
perebaj Apr 22, 2025
5afb0bc
Merge branch 'target-info' of github.com:perebaj/opentelemetry-collec…
perebaj Apr 22, 2025
05a8845
remove useless validation ls.Has(labels.MetricName)
perebaj Apr 22, 2025
d2ae2f4
remove useless ReplaceAll _ .
perebaj Apr 22, 2025
bc70d0c
remove old comments
perebaj Apr 22, 2025
3ce627e
replace METRIC_TYPE_INFO by GAUGE
perebaj Apr 22, 2025
14fbe4c
assert stats
perebaj Apr 22, 2025
c4e8771
tests sending target info in different orders
perebaj Apr 22, 2025
2922d19
Merge branch 'main' into target-info
perebaj Apr 22, 2025
da08b4c
conflict go.sum/mod
perebaj Apr 22, 2025
130c2b8
remove replace library & add unit test for taget info multiple requests
perebaj Apr 23, 2025
d13f346
improve comments
perebaj Apr 23, 2025
eceb950
Merge branch 'main' into target-info
perebaj Apr 23, 2025
892426a
rename cache variable
perebaj Apr 23, 2025
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
27 changes: 27 additions & 0 deletions .chloggen/target-info.yaml
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: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: prometheusremotewritereceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Create a cache approach to deal with desyncronization between requests that will be processed.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [37277]

# (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: The target info can be send through this service in a desynchronized way, so we need to create a cache to deal with this edge case.

# 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: [user]
7 changes: 7 additions & 0 deletions receiver/prometheusremotewritereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.opentelemetry.io/collector/consumer/consumertest v0.124.0
go.opentelemetry.io/collector/pdata v1.30.0
go.opentelemetry.io/collector/receiver v1.30.0
go.opentelemetry.io/collector/receiver/receiverhelper v0.124.0
go.opentelemetry.io/collector/receiver/receivertest v0.124.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
Expand Down Expand Up @@ -125,3 +126,9 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics => ../../internal/exp/metrics

// Temporary solution to unblock the development of target_info. Recently we found a bug in the decompression/compression of snappy requests.
// The solution is being addressed in https://github.com/open-telemetry/opentelemetry-collector/pull/12825. For this reason we are using the forked version of the collector.
replace go.opentelemetry.io/collector/config/confighttp => github.com/cardinalhq/opentelemetry-collector/config/confighttp v0.0.0-20250411101659-db48f79d3530

replace go.opentelemetry.io/collector/config/configcompression => github.com/cardinalhq/opentelemetry-collector/config/configcompression v0.0.0-20250411101659-db48f79d3530
10 changes: 6 additions & 4 deletions receiver/prometheusremotewritereceiver/go.sum

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

62 changes: 53 additions & 9 deletions receiver/prometheusremotewritereceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.uber.org/zap/zapcore"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity"
Expand All @@ -37,16 +38,19 @@ func newRemoteWriteReceiver(settings receiver.Settings, cfg *Config, nextConsume
server: &http.Server{
ReadTimeout: 60 * time.Second,
},
interRequestCache: make(map[uint64]pmetric.ResourceMetrics),
}, nil
}

type prometheusRemoteWriteReceiver struct {
settings receiver.Settings
nextConsumer consumer.Metrics

config *Config
server *http.Server
wg sync.WaitGroup
config *Config
server *http.Server
wg sync.WaitGroup
interRequestCache map[uint64]pmetric.ResourceMetrics
obsrecv *receiverhelper.ObsReport
}

// MetricIdentity contains all the components that uniquely identify a metric
Expand Down Expand Up @@ -94,6 +98,15 @@ func (prw *prometheusRemoteWriteReceiver) Start(ctx context.Context, host compon
mux.HandleFunc("/api/v1/write", prw.handlePRW)
var err error

prw.obsrecv, err = receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: prw.settings.ID,
Transport: "http",
ReceiverCreateSettings: prw.settings,
})
if err != nil {
return fmt.Errorf("failed to create obsreport: %w", err)
}

prw.server, err = prw.config.ToServer(ctx, host, prw.settings.TelemetrySettings, mux)
if err != nil {
return fmt.Errorf("failed to create server definition: %w", err)
Expand Down Expand Up @@ -163,14 +176,20 @@ func (prw *prometheusRemoteWriteReceiver) handlePRW(w http.ResponseWriter, req *
return
}

_, stats, err := prw.translateV2(req.Context(), &prw2Req)
m, stats, err := prw.translateV2(req.Context(), &prw2Req)
stats.SetHeaders(w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) // Following instructions at https://prometheus.io/docs/specs/remote_write_spec_2_0/#invalid-samples
return
}

w.WriteHeader(http.StatusNoContent)
obsCtx := prw.obsrecv.StartMetricsOp(req.Context())
err = prw.nextConsumer.ConsumeMetrics(req.Context(), m)
if err != nil {
prw.settings.Logger.Error("Error consuming metrics", zapcore.Field{Key: "error", Type: zapcore.ErrorType, Interface: err})
}
prw.obsrecv.EndMetricsOp(obsCtx, "prometheusremotewritereceiver", m.ResourceMetrics().Len(), err)
}

// parseProto parses the content-type header and returns the version of the remote-write protocol.
Expand Down Expand Up @@ -217,7 +236,6 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr
// Instead of creating a whole new OTLP metric, we just append the new sample to the existing OTLP metric.
// This cache is called "intra" because in the future we'll have a "interRequestCache" to cache resourceAttributes
// between requests based on the metric "target_info".
intraRequestCache = make(map[uint64]pmetric.ResourceMetrics)
// The key is composed by: resource_hash:scope_name:scope_version:metric_name:unit:type
metricCache = make(map[uint64]pmetric.Metric)
)
Expand All @@ -232,16 +250,42 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr
continue
}

// If the metric name is equal to target_info, we use its labels as attributes of the resource
// Ref: https://opentelemetry.io/docs/specs/otel/compatibility/prometheus_and_openmetrics/#resource-attributes-1
if ls.Has(labels.MetricName) && ls.Get(labels.MetricName) == "target_info" {
var rm pmetric.ResourceMetrics
hashedLabels := xxhash.Sum64String(ls.Get("job") + string([]byte{'\xff'}) + ls.Get("instance"))

if existingRM, ok := prw.interRequestCache[hashedLabels]; ok {
rm = existingRM
} else {
rm = otelMetrics.ResourceMetrics().AppendEmpty()
}

attrs := rm.Resource().Attributes()
parseJobAndInstance(attrs, ls.Get("job"), ls.Get("instance"))

// Add the remaining labels as resource attributes
for _, l := range ls {
if l.Name != "job" && l.Name != "instance" && l.Name != labels.MetricName {
attrKey := strings.ReplaceAll(l.Name, "_", ".")
attrs.PutStr(attrKey, l.Value)
}
}
prw.interRequestCache[hashedLabels] = rm
continue
}

// For metrics other than target_info, we need to follow the standard process of creating a metric.
var rm pmetric.ResourceMetrics
hashedLabels := xxhash.Sum64String(ls.Get("job") + string([]byte{'\xff'}) + ls.Get("instance"))
intraCacheEntry, ok := intraRequestCache[hashedLabels]
existingRM, ok := prw.interRequestCache[hashedLabels]
if ok {
// We found the same time series in the same request, so we should append to the same OTLP metric.
rm = intraCacheEntry
rm = existingRM
} else {
rm = otelMetrics.ResourceMetrics().AppendEmpty()
parseJobAndInstance(rm.Resource().Attributes(), ls.Get("job"), ls.Get("instance"))
intraRequestCache[hashedLabels] = rm
prw.interRequestCache[hashedLabels] = rm
}

scopeName, scopeVersion := prw.extractScopeInfo(ls)
Expand Down
Loading
Loading