Skip to content

Commit ff782ca

Browse files
authored
Add otlp exemplar ingestion test (#6244)
1 parent fbe118b commit ff782ca

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

integration/e2ecortex/client.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,73 @@ func convertTimeseriesToMetrics(timeseries []prompb.TimeSeries) pmetric.Metrics
231231
return metrics
232232
}
233233

234+
func otlpWriteRequest(name string) pmetricotlp.ExportRequest {
235+
d := pmetric.NewMetrics()
236+
237+
// Generate One Counter, One Gauge, One Histogram, One Exponential-Histogram
238+
// with resource attributes: service.name="test-service", service.instance.id="test-instance", host.name="test-host"
239+
// with metric attibute: foo.bar="baz"
240+
241+
timestamp := time.Now()
242+
243+
resourceMetric := d.ResourceMetrics().AppendEmpty()
244+
resourceMetric.Resource().Attributes().PutStr("service.name", "test-service")
245+
resourceMetric.Resource().Attributes().PutStr("service.instance.id", "test-instance")
246+
resourceMetric.Resource().Attributes().PutStr("host.name", "test-host")
247+
248+
scopeMetric := resourceMetric.ScopeMetrics().AppendEmpty()
249+
250+
// Generate One Counter
251+
counterMetric := scopeMetric.Metrics().AppendEmpty()
252+
counterMetric.SetName(name)
253+
counterMetric.SetDescription("test-counter-description")
254+
255+
counterMetric.SetEmptySum()
256+
counterMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
257+
258+
counterDataPoint := counterMetric.Sum().DataPoints().AppendEmpty()
259+
counterDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
260+
counterDataPoint.SetDoubleValue(10.0)
261+
counterDataPoint.Attributes().PutStr("foo.bar", "baz")
262+
263+
counterExemplar := counterDataPoint.Exemplars().AppendEmpty()
264+
counterExemplar.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
265+
counterExemplar.SetDoubleValue(10.0)
266+
counterExemplar.SetSpanID(pcommon.SpanID{0, 1, 2, 3, 4, 5, 6, 7})
267+
counterExemplar.SetTraceID(pcommon.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
268+
269+
return pmetricotlp.NewExportRequestFromMetrics(d)
270+
}
271+
272+
func (c *Client) OTLPPushExemplar(name string) (*http.Response, error) {
273+
data, err := otlpWriteRequest(name).MarshalProto()
274+
if err != nil {
275+
return nil, err
276+
}
277+
278+
// Create HTTP request
279+
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/v1/otlp/v1/metrics", c.distributorAddress), bytes.NewReader(data))
280+
if err != nil {
281+
return nil, err
282+
}
283+
284+
req.Header.Set("X-Scope-OrgID", c.orgID)
285+
req.Header.Set("Content-Type", "application/x-protobuf")
286+
287+
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
288+
defer cancel()
289+
290+
// Execute HTTP request
291+
res, err := c.httpClient.Do(req.WithContext(ctx))
292+
if err != nil {
293+
return nil, err
294+
}
295+
296+
defer res.Body.Close()
297+
298+
return res, nil
299+
}
300+
234301
// Push series to OTLP endpoint
235302
func (c *Client) OTLP(timeseries []prompb.TimeSeries) (*http.Response, error) {
236303

@@ -267,6 +334,13 @@ func (c *Client) Query(query string, ts time.Time) (model.Value, error) {
267334
return value, err
268335
}
269336

337+
// QueryExemplars runs an exemplars query
338+
func (c *Client) QueryExemplars(query string, start, end time.Time) ([]promv1.ExemplarQueryResult, error) {
339+
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
340+
defer cancel()
341+
return c.querierClient.QueryExemplars(ctx, query, start, end)
342+
}
343+
270344
// QueryRange runs a query range.
271345
func (c *Client) QueryRange(query string, start, end time.Time, step time.Duration) (model.Value, error) {
272346
value, _, err := c.querierClient.QueryRange(context.Background(), query, promv1.Range{

integration/otlp_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,49 @@ func TestOTLP(t *testing.T) {
9898
require.Equal(t, float64(expectedHistogram.Count), float64(v[0].Histogram.Count))
9999
require.Equal(t, expectedHistogram.Sum, float64(v[0].Histogram.Sum))
100100
}
101+
102+
func TestOTLPIngestExemplar(t *testing.T) {
103+
s, err := e2e.NewScenario(networkName)
104+
require.NoError(t, err)
105+
defer s.Close()
106+
107+
// Start dependencies.
108+
minio := e2edb.NewMinio(9000, bucketName)
109+
require.NoError(t, s.StartAndWaitReady(minio))
110+
111+
// Start Cortex components.
112+
require.NoError(t, copyFileToSharedDir(s, "docs/configuration/single-process-config-blocks.yaml", cortexConfigFile))
113+
114+
// Start Cortex in single binary mode, reading the config from file and overwriting
115+
// the backend config to make it work with Minio.
116+
flags := map[string]string{
117+
"-blocks-storage.s3.access-key-id": e2edb.MinioAccessKey,
118+
"-blocks-storage.s3.secret-access-key": e2edb.MinioSecretKey,
119+
"-blocks-storage.s3.bucket-name": bucketName,
120+
"-blocks-storage.s3.endpoint": fmt.Sprintf("%s-minio-9000:9000", networkName),
121+
"-blocks-storage.s3.insecure": "true",
122+
"-blocks-storage.tsdb.enable-native-histograms": "true",
123+
"-ingester.max-exemplars": "100",
124+
// alert manager
125+
"-alertmanager.web.external-url": "http://localhost/alertmanager",
126+
"-alertmanager-storage.backend": "local",
127+
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
128+
}
129+
// make alert manager config dir
130+
require.NoError(t, writeFileToSharedDir(s, "alertmanager_configs", []byte{}))
131+
132+
cortex := e2ecortex.NewSingleBinaryWithConfigFile("cortex-1", cortexConfigFile, flags, "", 9009, 9095)
133+
require.NoError(t, s.StartAndWaitReady(cortex))
134+
135+
c, err := e2ecortex.NewClient(cortex.HTTPEndpoint(), cortex.HTTPEndpoint(), "", "", "user-1")
136+
require.NoError(t, err)
137+
138+
res, err := c.OTLPPushExemplar("exemplar_1")
139+
require.NoError(t, err)
140+
require.Equal(t, 200, res.StatusCode)
141+
142+
now := time.Now()
143+
exemplars, err := c.QueryExemplars("exemplar_1", now.Add(-time.Minute), now.Add(time.Minute))
144+
require.NoError(t, err)
145+
require.Equal(t, 1, len(exemplars))
146+
}

0 commit comments

Comments
 (0)