Skip to content

Commit e1de6cc

Browse files
[receiver/prometheusremotewritereceiver] Add MetricIdentity struct with xxhash-based hash method (#38795)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Creates MetricIdentity struct for uniquely identifying metrics and adds a deterministic Hash method using xxhash with proper field separation. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue partly fixes #37277. <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.-->
1 parent ac351e7 commit e1de6cc

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

receiver/prometheusremotewritereceiver/receiver.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ type prometheusRemoteWriteReceiver struct {
4949
wg sync.WaitGroup
5050
}
5151

52+
// MetricIdentity contains all the components that uniquely identify a metric
53+
// according to the OpenTelemetry Protocol data model.
54+
// The definition of the metric uniqueness is based on the following document. Ref: https://opentelemetry.io/docs/specs/otel/metrics/data-model/#opentelemetry-protocol-data-model
55+
type MetricIdentity struct {
56+
ResourceID string
57+
ScopeName string
58+
ScopeVersion string
59+
MetricName string
60+
Unit string
61+
Type writev2.Metadata_MetricType
62+
}
63+
64+
// createMetricIdentity creates a MetricIdentity struct from the required components
65+
func createMetricIdentity(resourceID, scopeName, scopeVersion, metricName, unit string, metricType writev2.Metadata_MetricType) MetricIdentity {
66+
return MetricIdentity{
67+
ResourceID: resourceID,
68+
ScopeName: scopeName,
69+
ScopeVersion: scopeVersion,
70+
MetricName: metricName,
71+
Unit: unit,
72+
Type: metricType,
73+
}
74+
}
75+
76+
// Hash generates a unique hash for the metric identity
77+
func (mi MetricIdentity) Hash() uint64 {
78+
const separator = "\xff"
79+
80+
combined := strings.Join([]string{
81+
mi.ResourceID,
82+
mi.ScopeName,
83+
mi.ScopeVersion,
84+
mi.MetricName,
85+
mi.Unit,
86+
fmt.Sprintf("%d", mi.Type),
87+
}, separator)
88+
89+
return xxhash.Sum64String(combined)
90+
}
91+
5292
func (prw *prometheusRemoteWriteReceiver) Start(ctx context.Context, host component.Host) error {
5393
mux := http.NewServeMux()
5494
mux.HandleFunc("/api/v1/write", prw.handlePRW)
@@ -179,8 +219,7 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr
179219
// between requests based on the metric "target_info".
180220
intraRequestCache = make(map[uint64]pmetric.ResourceMetrics)
181221
// The key is composed by: resource_hash:scope_name:scope_version:metric_name:unit:type
182-
// TODO: use the appropriate hash function.
183-
metricCache = make(map[string]pmetric.Metric)
222+
metricCache = make(map[uint64]pmetric.Metric)
184223
)
185224

186225
for _, ts := range req.Timeseries {
@@ -221,16 +260,17 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr
221260
description := req.Symbols[ts.Metadata.HelpRef]
222261

223262
resourceID := identity.OfResource(rm.Resource())
224-
// Temporary approach to generate the metric key.
225-
// TODO: Replace this with a proper hashing function.
226-
// The definition of the metric uniqueness is based on the following document. Ref: https://opentelemetry.io/docs/specs/otel/metrics/data-model/#opentelemetry-protocol-data-model
227-
metricKey := fmt.Sprintf("%s:%s:%s:%s:%s:%d",
263+
264+
metricIdentity := createMetricIdentity(
228265
resourceID.String(), // Resource identity
229266
scopeName, // Scope name
230267
scopeVersion, // Scope version
231268
metricName, // Metric name
232269
unit, // Unit
233-
ts.Metadata.Type) // Metric type
270+
ts.Metadata.Type, // Metric type
271+
)
272+
273+
metricKey := metricIdentity.Hash()
234274

235275
var scope pmetric.ScopeMetrics
236276
var foundScope bool

0 commit comments

Comments
 (0)