@@ -231,6 +231,73 @@ func convertTimeseriesToMetrics(timeseries []prompb.TimeSeries) pmetric.Metrics
231
231
return metrics
232
232
}
233
233
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
+
234
301
// Push series to OTLP endpoint
235
302
func (c * Client ) OTLP (timeseries []prompb.TimeSeries ) (* http.Response , error ) {
236
303
@@ -267,6 +334,13 @@ func (c *Client) Query(query string, ts time.Time) (model.Value, error) {
267
334
return value , err
268
335
}
269
336
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
+
270
344
// QueryRange runs a query range.
271
345
func (c * Client ) QueryRange (query string , start , end time.Time , step time.Duration ) (model.Value , error ) {
272
346
value , _ , err := c .querierClient .QueryRange (context .Background (), query , promv1.Range {
0 commit comments