-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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/prometheusremotewrite] Parse labels #35656
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: receiver/prometheusremotewrite | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Parse labels from Prometheus Remote Write requests into Resource and Scope Attributes | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35656] | ||
|
||
# (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: Warning - The HTTP Server still doesn't pass metrics to the next consumer. The component is unusable for now. | ||
|
||
# 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: [api, user] | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,24 @@ import ( | |
|
||
"github.com/gogo/protobuf/proto" | ||
promconfig "github.com/prometheus/prometheus/config" | ||
"github.com/prometheus/prometheus/model/labels" | ||
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2" | ||
promremote "github.com/prometheus/prometheus/storage/remote" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componentstatus" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/receiver" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func newRemoteWriteReceiver(settings receiver.Settings, cfg *Config, nextConsumer consumer.Metrics) (receiver.Metrics, error) { | ||
return &prometheusRemoteWriteReceiver{ | ||
settings: settings, | ||
nextConsumer: nextConsumer, | ||
config: cfg, | ||
settings: settings, | ||
nextConsumer: nextConsumer, | ||
config: cfg, | ||
jobInstanceCache: make(map[string]pmetric.ResourceMetrics), | ||
server: &http.Server{ | ||
ReadTimeout: 60 * time.Second, | ||
}, | ||
|
@@ -39,8 +42,9 @@ type prometheusRemoteWriteReceiver struct { | |
settings receiver.Settings | ||
nextConsumer consumer.Metrics | ||
|
||
config *Config | ||
server *http.Server | ||
jobInstanceCache map[string]pmetric.ResourceMetrics | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider using an int64 as the key for this cache, and constructing an identifier using xxhash, similar to what prometheus translation does: https://github.com/prometheus/prometheus/blob/0f0deb77a2f15383914ad60a3309281adb7e8b27/storage/remote/otlptranslator/prometheusremotewrite/helper.go#L94 You can just append the job, a separator, and then the instance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any mechanism to expire items in this cache? We don't want this to leak memory. Actually, i'm not sure we should be storing the job + instance cache on the receiver itself. We probably want this cache around for a single PRW request, right? Otherwise, we will keep appending new metrics to the same resourceMetrics forever. |
||
config *Config | ||
server *http.Server | ||
} | ||
|
||
func (prw *prometheusRemoteWriteReceiver) Start(ctx context.Context, host component.Host) error { | ||
|
@@ -150,8 +154,105 @@ func (prw *prometheusRemoteWriteReceiver) parseProto(contentType string) (promco | |
} | ||
|
||
// translateV2 translates a v2 remote-write request into OTLP metrics. | ||
// For now translateV2 is not implemented and returns an empty metrics. | ||
// translate is not feature complete. | ||
// nolint | ||
func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, _ *writev2.Request) (pmetric.Metrics, promremote.WriteResponseStats, error) { | ||
return pmetric.NewMetrics(), promremote.WriteResponseStats{}, nil | ||
func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *writev2.Request) (pmetric.Metrics, promremote.WriteResponseStats, error) { | ||
var ( | ||
badRequestErrors []error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we just use a single error, and use errors.Join to append new errors? e.g. var err error
// then, when we want to append an error:
err = errors.Join(err, fmt.Errorf("missing metric name in labels")) |
||
otelMetrics = pmetric.NewMetrics() | ||
b = labels.NewScratchBuilder(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we use a more descriptive name? Perhaps labelsBuilder? |
||
stats = promremote.WriteResponseStats{} | ||
) | ||
|
||
for _, ts := range req.Timeseries { | ||
ls := ts.ToLabels(&b, req.Symbols) | ||
|
||
if !ls.Has(labels.MetricName) { | ||
badRequestErrors = append(badRequestErrors, fmt.Errorf("missing metric name in labels")) | ||
continue | ||
} else if duplicateLabel, hasDuplicate := ls.HasDuplicateLabelNames(); hasDuplicate { | ||
badRequestErrors = append(badRequestErrors, fmt.Errorf("duplicate label %q in labels", duplicateLabel)) | ||
continue | ||
} | ||
|
||
var rm pmetric.ResourceMetrics | ||
// This cache should be populated by the metric 'target_info', but we're not handling it yet. | ||
cacheEntry, ok := prw.jobInstanceCache[ls.Get("instance")+ls.Get("job")] | ||
if ok { | ||
rm = pmetric.NewResourceMetrics() | ||
cacheEntry.CopyTo(rm) | ||
} else { | ||
// A remote-write request can have multiple timeseries with the same instance and job labels. | ||
// While they are different timeseries in Prometheus, we're handling it as the same OTLP metric | ||
// until we support 'target_info'. | ||
rm = otelMetrics.ResourceMetrics().AppendEmpty() | ||
parseJobAndInstance(rm.Resource().Attributes(), ls.Get("instance"), ls.Get("job")) | ||
prw.jobInstanceCache[ls.Get("instance")+ls.Get("job")] = rm | ||
} | ||
|
||
switch ts.Metadata.Type { | ||
case writev2.Metadata_METRIC_TYPE_COUNTER: | ||
addCounterDatapoints(rm, ls, ts) | ||
case writev2.Metadata_METRIC_TYPE_GAUGE: | ||
addGaugeDatapoints(rm, ls, ts) | ||
case writev2.Metadata_METRIC_TYPE_SUMMARY: | ||
addSummaryDatapoints(rm, ls, ts) | ||
case writev2.Metadata_METRIC_TYPE_HISTOGRAM: | ||
addHistogramDatapoints(rm, ls, ts) | ||
default: | ||
badRequestErrors = append(badRequestErrors, fmt.Errorf("unsupported metric type %q for metric %q", ts.Metadata.Type, ls.Get(labels.MetricName))) | ||
} | ||
} | ||
|
||
return otelMetrics, stats, errors.Join(badRequestErrors...) | ||
} | ||
|
||
// parseJobAndInstance turns the job and instance labels service resource attributes. | ||
// Following the specification at https://opentelemetry.io/docs/specs/otel/compatibility/prometheus_and_openmetrics/ | ||
func parseJobAndInstance(dest pcommon.Map, instance, job string) { | ||
if job != "" { | ||
dest.PutStr("service.namespace", job) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Job should be service.name + "/" + service.namespace. Instance is only service.instance.id. You need to move |
||
} | ||
if instance != "" { | ||
parts := strings.Split(instance, "/") | ||
if len(parts) == 2 { | ||
dest.PutStr("service.name", parts[0]) | ||
dest.PutStr("service.instance.id", parts[1]) | ||
return | ||
} | ||
dest.PutStr("service.name", instance) | ||
} | ||
} | ||
|
||
func addCounterDatapoints(_ pmetric.ResourceMetrics, _ labels.Labels, _ writev2.TimeSeries) { | ||
// TODO: Implement this function | ||
} | ||
|
||
func addGaugeDatapoints(rm pmetric.ResourceMetrics, ls labels.Labels, ts writev2.TimeSeries) { | ||
m := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge() | ||
addDatapoints(m.DataPoints(), ls, ts) | ||
} | ||
|
||
func addSummaryDatapoints(_ pmetric.ResourceMetrics, _ labels.Labels, _ writev2.TimeSeries) { | ||
// TODO: Implement this function | ||
} | ||
|
||
func addHistogramDatapoints(_ pmetric.ResourceMetrics, _ labels.Labels, _ writev2.TimeSeries) { | ||
// TODO: Implement this function | ||
} | ||
|
||
// addDatapoints adds the labels to the datapoints attributes. | ||
// TODO: We're still not handling several fields that make a datapoint complete, e.g. StartTimestamp, | ||
// Timestamp, Value, etc. | ||
func addDatapoints(datapoints pmetric.NumberDataPointSlice, ls labels.Labels, _ writev2.TimeSeries) { | ||
attributes := datapoints.AppendEmpty().Attributes() | ||
|
||
for _, l := range ls { | ||
if l.Name == "instance" || l.Name == "job" || // Become resource attributes "service.name", "service.instance.id" and "service.namespace" | ||
l.Name == labels.MetricName || // Becomes metric name | ||
l.Name == "otel_scope_name" || l.Name == "otel_scope_version" { // Becomes scope name and version | ||
continue | ||
} | ||
attributes.PutStr(l.Name, l.Value) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any changes to the public API of the package.