2019-05-31 18:39:40 -07:00
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package remote
import (
2023-07-28 03:35:28 -07:00
"bytes"
"net/http"
"net/http/httptest"
2019-12-12 12:47:23 -08:00
"net/url"
2019-05-31 18:39:40 -07:00
"testing"
2019-09-04 10:21:53 -07:00
"time"
2019-05-31 18:39:40 -07:00
2020-06-25 23:33:52 -07:00
"github.com/prometheus/client_golang/prometheus"
2019-12-12 12:47:23 -08:00
common_config "github.com/prometheus/common/config"
2019-09-04 10:21:53 -07:00
"github.com/prometheus/common/model"
2020-10-29 02:43:23 -07:00
"github.com/stretchr/testify/require"
2023-07-28 03:35:28 -07:00
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
2020-10-22 02:00:08 -07:00
2019-05-31 18:39:40 -07:00
"github.com/prometheus/prometheus/config"
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
2019-05-31 18:39:40 -07:00
)
2021-10-25 00:24:53 -07:00
func testRemoteWriteConfig ( ) * config . RemoteWriteConfig {
return & config . RemoteWriteConfig {
Name : "dev" ,
URL : & common_config . URL {
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
2019-12-12 12:47:23 -08:00
} ,
2021-10-25 00:24:53 -07:00
QueueConfig : config . DefaultQueueConfig ,
}
2019-12-12 12:47:23 -08:00
}
func TestNoDuplicateWriteConfigs ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-12-12 12:47:23 -08:00
cfg1 := config . RemoteWriteConfig {
Name : "write-1" ,
2020-04-07 14:05:39 -07:00
URL : & common_config . URL {
2019-12-12 12:47:23 -08:00
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
} ,
QueueConfig : config . DefaultQueueConfig ,
}
cfg2 := config . RemoteWriteConfig {
Name : "write-2" ,
2020-04-07 14:05:39 -07:00
URL : & common_config . URL {
2019-12-12 12:47:23 -08:00
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
} ,
QueueConfig : config . DefaultQueueConfig ,
}
cfg3 := config . RemoteWriteConfig {
2020-04-07 14:05:39 -07:00
URL : & common_config . URL {
2019-12-12 12:47:23 -08:00
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
} ,
QueueConfig : config . DefaultQueueConfig ,
}
type testcase struct {
cfgs [ ] * config . RemoteWriteConfig
err bool
}
cases := [ ] testcase {
{ // Two duplicates, we should get an error.
cfgs : [ ] * config . RemoteWriteConfig {
& cfg1 ,
& cfg1 ,
} ,
err : true ,
} ,
{ // Duplicates but with different names, we should not get an error.
cfgs : [ ] * config . RemoteWriteConfig {
& cfg1 ,
& cfg2 ,
} ,
err : false ,
} ,
{ // Duplicates but one with no name, we should not get an error.
cfgs : [ ] * config . RemoteWriteConfig {
& cfg1 ,
& cfg3 ,
} ,
err : false ,
} ,
{ // Duplicates both with no name, we should get an error.
cfgs : [ ] * config . RemoteWriteConfig {
& cfg3 ,
& cfg3 ,
} ,
err : true ,
} ,
}
for _ , tc := range cases {
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , nil , dir , time . Millisecond , nil , false , false )
2019-12-12 12:47:23 -08:00
conf := & config . Config {
GlobalConfig : config . DefaultGlobalConfig ,
RemoteWriteConfigs : tc . cfgs ,
}
err := s . ApplyConfig ( conf )
gotError := err != nil
2020-10-29 02:43:23 -07:00
require . Equal ( t , tc . err , gotError )
2019-12-12 12:47:23 -08:00
err = s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-12-12 12:47:23 -08:00
}
}
func TestRestartOnNameChange ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-12-12 12:47:23 -08:00
2021-10-25 00:24:53 -07:00
cfg := testRemoteWriteConfig ( )
2019-12-12 12:47:23 -08:00
hash , err := toHash ( cfg )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-12-12 12:47:23 -08:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , nil , dir , time . Millisecond , nil , false , false )
2021-10-25 00:24:53 -07:00
2019-12-12 12:47:23 -08:00
conf := & config . Config {
GlobalConfig : config . DefaultGlobalConfig ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig {
2021-10-25 00:24:53 -07:00
cfg ,
2019-12-12 12:47:23 -08:00
} ,
}
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
require . Equal ( t , s . queues [ hash ] . client ( ) . Name ( ) , cfg . Name )
2019-12-12 12:47:23 -08:00
// Change the queues name, ensure the queue has been restarted.
conf . RemoteWriteConfigs [ 0 ] . Name = "dev-2"
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2019-12-12 12:47:23 -08:00
hash , err = toHash ( cfg )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
require . Equal ( t , s . queues [ hash ] . client ( ) . Name ( ) , conf . RemoteWriteConfigs [ 0 ] . Name )
2019-12-12 12:47:23 -08:00
err = s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-12-12 12:47:23 -08:00
}
2020-06-25 23:33:52 -07:00
func TestUpdateWithRegisterer ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2020-06-25 23:33:52 -07:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , prometheus . NewRegistry ( ) , dir , time . Millisecond , nil , false , false )
2020-06-25 23:33:52 -07:00
c1 := & config . RemoteWriteConfig {
Name : "named" ,
URL : & common_config . URL {
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
} ,
QueueConfig : config . DefaultQueueConfig ,
}
c2 := & config . RemoteWriteConfig {
URL : & common_config . URL {
URL : & url . URL {
Scheme : "http" ,
Host : "localhost" ,
} ,
} ,
QueueConfig : config . DefaultQueueConfig ,
}
conf := & config . Config {
GlobalConfig : config . DefaultGlobalConfig ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig { c1 , c2 } ,
}
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2020-06-25 23:33:52 -07:00
c1 . QueueConfig . MaxShards = 10
c2 . QueueConfig . MaxShards = 10
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2020-06-25 23:33:52 -07:00
for _ , queue := range s . queues {
2020-10-29 02:43:23 -07:00
require . Equal ( t , 10 , queue . cfg . MaxShards )
2020-06-25 23:33:52 -07:00
}
2021-11-19 12:21:45 -08:00
err := s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2020-06-25 23:33:52 -07:00
}
2019-05-31 18:39:40 -07:00
func TestWriteStorageLifecycle ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-05-31 18:39:40 -07:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , nil , dir , defaultFlushDeadline , nil , false , false )
2019-05-31 18:39:40 -07:00
conf := & config . Config {
GlobalConfig : config . DefaultGlobalConfig ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig {
2022-06-30 09:20:16 -07:00
baseRemoteWriteConfig ( "http://test-storage.com" ) ,
2019-05-31 18:39:40 -07:00
} ,
}
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 1 )
2019-05-31 18:39:40 -07:00
2021-11-19 12:21:45 -08:00
err := s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-05-31 18:39:40 -07:00
}
func TestUpdateExternalLabels ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-05-31 18:39:40 -07:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , prometheus . NewRegistry ( ) , dir , time . Second , nil , false , false )
2019-05-31 18:39:40 -07:00
externalLabels := labels . FromStrings ( "external" , "true" )
conf := & config . Config {
GlobalConfig : config . GlobalConfig { } ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig {
2021-10-25 00:24:53 -07:00
testRemoteWriteConfig ( ) ,
2019-05-31 18:39:40 -07:00
} ,
}
2019-12-12 12:47:23 -08:00
hash , err := toHash ( conf . RemoteWriteConfigs [ 0 ] )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 1 )
require . Empty ( t , s . queues [ hash ] . externalLabels )
2019-05-31 18:39:40 -07:00
conf . GlobalConfig . ExternalLabels = externalLabels
2019-12-12 12:47:23 -08:00
hash , err = toHash ( conf . RemoteWriteConfigs [ 0 ] )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 1 )
2022-03-09 14:35:14 -08:00
require . Equal ( t , [ ] labels . Label { { Name : "external" , Value : "true" } } , s . queues [ hash ] . externalLabels )
2019-05-31 18:39:40 -07:00
err = s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-05-31 18:39:40 -07:00
}
func TestWriteStorageApplyConfigsIdempotent ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-05-31 18:39:40 -07:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , nil , dir , defaultFlushDeadline , nil , false , false )
2019-05-31 18:39:40 -07:00
conf := & config . Config {
GlobalConfig : config . GlobalConfig { } ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig {
2022-06-30 09:20:16 -07:00
baseRemoteWriteConfig ( "http://test-storage.com" ) ,
2019-12-12 12:47:23 -08:00
} ,
}
hash , err := toHash ( conf . RemoteWriteConfigs [ 0 ] )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-12-12 12:47:23 -08:00
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 1 )
2019-05-31 18:39:40 -07:00
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 1 )
2019-12-12 12:47:23 -08:00
_ , hashExists := s . queues [ hash ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Queue pointer should have remained the same" )
2019-05-31 18:39:40 -07:00
err = s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-05-31 18:39:40 -07:00
}
2019-09-04 10:21:53 -07:00
func TestWriteStorageApplyConfigsPartialUpdate ( t * testing . T ) {
2021-11-19 12:21:45 -08:00
dir := t . TempDir ( )
2019-09-04 10:21:53 -07:00
2023-11-06 16:49:49 -08:00
s := NewWriteStorage ( nil , nil , dir , defaultFlushDeadline , nil , false , false )
2019-09-04 10:21:53 -07:00
c0 := & config . RemoteWriteConfig {
RemoteTimeout : model . Duration ( 10 * time . Second ) ,
QueueConfig : config . DefaultQueueConfig ,
2020-01-15 08:49:18 -08:00
WriteRelabelConfigs : [ ] * relabel . Config {
2020-08-12 18:55:42 -07:00
{
2020-01-15 08:49:18 -08:00
Regex : relabel . MustNewRegexp ( ".+" ) ,
} ,
} ,
2019-09-04 10:21:53 -07:00
}
c1 := & config . RemoteWriteConfig {
RemoteTimeout : model . Duration ( 20 * time . Second ) ,
QueueConfig : config . DefaultQueueConfig ,
2020-03-30 20:39:29 -07:00
HTTPClientConfig : common_config . HTTPClientConfig {
2020-01-15 08:49:18 -08:00
BearerToken : "foo" ,
} ,
2019-09-04 10:21:53 -07:00
}
c2 := & config . RemoteWriteConfig {
RemoteTimeout : model . Duration ( 30 * time . Second ) ,
QueueConfig : config . DefaultQueueConfig ,
}
conf := & config . Config {
GlobalConfig : config . GlobalConfig { } ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig { c0 , c1 , c2 } ,
}
2019-12-12 12:47:23 -08:00
// We need to set URL's so that metric creation doesn't panic.
2020-01-15 08:49:18 -08:00
for i := range conf . RemoteWriteConfigs {
conf . RemoteWriteConfigs [ i ] . URL = & common_config . URL {
URL : & url . URL {
Host : "http://test-storage.com" ,
} ,
}
2019-12-12 12:47:23 -08:00
}
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 3 )
2019-12-12 12:47:23 -08:00
2020-01-15 08:49:18 -08:00
hashes := make ( [ ] string , len ( conf . RemoteWriteConfigs ) )
2020-03-30 20:39:29 -07:00
queues := make ( [ ] * QueueManager , len ( conf . RemoteWriteConfigs ) )
2020-01-15 08:49:18 -08:00
storeHashes := func ( ) {
for i := range conf . RemoteWriteConfigs {
hash , err := toHash ( conf . RemoteWriteConfigs [ i ] )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2020-01-15 08:49:18 -08:00
hashes [ i ] = hash
2020-03-30 20:39:29 -07:00
queues [ i ] = s . queues [ hash ]
2020-01-15 08:49:18 -08:00
}
}
2019-09-04 10:21:53 -07:00
2020-01-15 08:49:18 -08:00
storeHashes ( )
2019-09-04 10:21:53 -07:00
// Update c0 and c2.
2020-01-15 08:49:18 -08:00
c0 . WriteRelabelConfigs [ 0 ] = & relabel . Config { Regex : relabel . MustNewRegexp ( "foo" ) }
2019-09-04 10:21:53 -07:00
c2 . RemoteTimeout = model . Duration ( 50 * time . Second )
conf = & config . Config {
GlobalConfig : config . GlobalConfig { } ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig { c0 , c1 , c2 } ,
}
2020-10-29 02:43:23 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 3 )
2019-09-04 10:21:53 -07:00
2020-01-15 08:49:18 -08:00
_ , hashExists := s . queues [ hashes [ 0 ] ]
2020-10-29 02:43:23 -07:00
require . False ( t , hashExists , "The queue for the first remote write configuration should have been restarted because the relabel configuration has changed." )
2020-03-30 20:39:29 -07:00
q , hashExists := s . queues [ hashes [ 1 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Hash of unchanged queue should have remained the same" )
require . Equal ( t , q , queues [ 1 ] , "Pointer of unchanged queue should have remained the same" )
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 2 ] ]
2020-10-29 02:43:23 -07:00
require . False ( t , hashExists , "The queue for the third remote write configuration should have been restarted because the timeout has changed." )
2019-09-04 10:21:53 -07:00
2020-01-15 08:49:18 -08:00
storeHashes ( )
2020-03-30 20:39:29 -07:00
secondClient := s . queues [ hashes [ 1 ] ] . client ( )
2020-01-15 08:49:18 -08:00
// Update c1.
c1 . HTTPClientConfig . BearerToken = "bar"
2021-11-19 12:21:45 -08:00
err := s . ApplyConfig ( conf )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 3 )
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 0 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Pointer of unchanged queue should have remained the same" )
2020-03-30 20:39:29 -07:00
q , hashExists = s . queues [ hashes [ 1 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Hash of queue with secret change should have remained the same" )
require . NotEqual ( t , secondClient , q . client ( ) , "Pointer of a client with a secret change should not be the same" )
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 2 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Pointer of unchanged queue should have remained the same" )
2020-01-15 08:49:18 -08:00
storeHashes ( )
2019-09-04 10:21:53 -07:00
// Delete c0.
conf = & config . Config {
GlobalConfig : config . GlobalConfig { } ,
RemoteWriteConfigs : [ ] * config . RemoteWriteConfig { c1 , c2 } ,
}
2021-10-25 00:24:33 -07:00
require . NoError ( t , s . ApplyConfig ( conf ) )
2023-12-07 03:35:01 -08:00
require . Len ( t , s . queues , 2 )
2019-09-04 10:21:53 -07:00
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 0 ] ]
2020-10-29 02:43:23 -07:00
require . False ( t , hashExists , "If a config is removed, the queue should be stopped and recreated." )
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 1 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Pointer of unchanged queue should have remained the same" )
2020-01-15 08:49:18 -08:00
_ , hashExists = s . queues [ hashes [ 2 ] ]
2020-10-29 02:43:23 -07:00
require . True ( t , hashExists , "Pointer of unchanged queue should have remained the same" )
2019-09-04 10:21:53 -07:00
err = s . Close ( )
2020-10-29 02:43:23 -07:00
require . NoError ( t , err )
2019-09-04 10:21:53 -07:00
}
2023-07-28 03:35:28 -07:00
func TestOTLPWriteHandler ( t * testing . T ) {
exportRequest := generateOTLPWriteRequest ( t )
buf , err := exportRequest . MarshalProto ( )
require . NoError ( t , err )
req , err := http . NewRequest ( "" , "" , bytes . NewReader ( buf ) )
require . NoError ( t , err )
req . Header . Set ( "Content-Type" , "application/x-protobuf" )
appendable := & mockAppendable { }
handler := NewOTLPWriteHandler ( nil , appendable )
recorder := httptest . NewRecorder ( )
handler . ServeHTTP ( recorder , req )
resp := recorder . Result ( )
require . Equal ( t , http . StatusOK , resp . StatusCode )
2023-12-07 03:35:01 -08:00
require . Len ( t , appendable . samples , 12 ) // 1 (counter) + 1 (gauge) + 1 (target_info) + 7 (hist_bucket) + 2 (hist_sum, hist_count)
require . Len ( t , appendable . histograms , 1 ) // 1 (exponential histogram)
require . Len ( t , appendable . exemplars , 1 ) // 1 (exemplar)
2023-07-28 03:35:28 -07:00
}
func generateOTLPWriteRequest ( t * testing . T ) pmetricotlp . ExportRequest {
d := pmetric . NewMetrics ( )
// Generate One Counter, One Gauge, One Histogram, One Exponential-Histogram
// with resource attributes: service.name="test-service", service.instance.id="test-instance", host.name="test-host"
// with metric attibute: foo.bar="baz"
timestamp := time . Now ( )
resourceMetric := d . ResourceMetrics ( ) . AppendEmpty ( )
resourceMetric . Resource ( ) . Attributes ( ) . PutStr ( "service.name" , "test-service" )
resourceMetric . Resource ( ) . Attributes ( ) . PutStr ( "service.instance.id" , "test-instance" )
resourceMetric . Resource ( ) . Attributes ( ) . PutStr ( "host.name" , "test-host" )
scopeMetric := resourceMetric . ScopeMetrics ( ) . AppendEmpty ( )
// Generate One Counter
counterMetric := scopeMetric . Metrics ( ) . AppendEmpty ( )
counterMetric . SetName ( "test-counter" )
counterMetric . SetDescription ( "test-counter-description" )
counterMetric . SetEmptySum ( )
counterMetric . Sum ( ) . SetAggregationTemporality ( pmetric . AggregationTemporalityCumulative )
counterMetric . Sum ( ) . SetIsMonotonic ( true )
counterDataPoint := counterMetric . Sum ( ) . DataPoints ( ) . AppendEmpty ( )
counterDataPoint . SetTimestamp ( pcommon . NewTimestampFromTime ( timestamp ) )
counterDataPoint . SetDoubleValue ( 10.0 )
counterDataPoint . Attributes ( ) . PutStr ( "foo.bar" , "baz" )
counterExemplar := counterDataPoint . Exemplars ( ) . AppendEmpty ( )
counterExemplar . SetTimestamp ( pcommon . NewTimestampFromTime ( timestamp ) )
counterExemplar . SetDoubleValue ( 10.0 )
counterExemplar . SetSpanID ( pcommon . SpanID { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 } )
counterExemplar . SetTraceID ( pcommon . TraceID { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 } )
// Generate One Gauge
gaugeMetric := scopeMetric . Metrics ( ) . AppendEmpty ( )
gaugeMetric . SetName ( "test-gauge" )
gaugeMetric . SetDescription ( "test-gauge-description" )
gaugeMetric . SetEmptyGauge ( )
gaugeDataPoint := gaugeMetric . Gauge ( ) . DataPoints ( ) . AppendEmpty ( )
gaugeDataPoint . SetTimestamp ( pcommon . NewTimestampFromTime ( timestamp ) )
gaugeDataPoint . SetDoubleValue ( 10.0 )
gaugeDataPoint . Attributes ( ) . PutStr ( "foo.bar" , "baz" )
// Generate One Histogram
histogramMetric := scopeMetric . Metrics ( ) . AppendEmpty ( )
histogramMetric . SetName ( "test-histogram" )
histogramMetric . SetDescription ( "test-histogram-description" )
histogramMetric . SetEmptyHistogram ( )
histogramMetric . Histogram ( ) . SetAggregationTemporality ( pmetric . AggregationTemporalityCumulative )
histogramDataPoint := histogramMetric . Histogram ( ) . DataPoints ( ) . AppendEmpty ( )
histogramDataPoint . SetTimestamp ( pcommon . NewTimestampFromTime ( timestamp ) )
histogramDataPoint . ExplicitBounds ( ) . FromRaw ( [ ] float64 { 0.0 , 1.0 , 2.0 , 3.0 , 4.0 , 5.0 } )
histogramDataPoint . BucketCounts ( ) . FromRaw ( [ ] uint64 { 2 , 2 , 2 , 2 , 2 , 2 } )
histogramDataPoint . SetCount ( 10 )
histogramDataPoint . SetSum ( 30.0 )
histogramDataPoint . Attributes ( ) . PutStr ( "foo.bar" , "baz" )
// Generate One Exponential-Histogram
exponentialHistogramMetric := scopeMetric . Metrics ( ) . AppendEmpty ( )
exponentialHistogramMetric . SetName ( "test-exponential-histogram" )
exponentialHistogramMetric . SetDescription ( "test-exponential-histogram-description" )
exponentialHistogramMetric . SetEmptyExponentialHistogram ( )
exponentialHistogramMetric . ExponentialHistogram ( ) . SetAggregationTemporality ( pmetric . AggregationTemporalityCumulative )
exponentialHistogramDataPoint := exponentialHistogramMetric . ExponentialHistogram ( ) . DataPoints ( ) . AppendEmpty ( )
exponentialHistogramDataPoint . SetTimestamp ( pcommon . NewTimestampFromTime ( timestamp ) )
exponentialHistogramDataPoint . SetScale ( 2.0 )
exponentialHistogramDataPoint . Positive ( ) . BucketCounts ( ) . FromRaw ( [ ] uint64 { 2 , 2 , 2 , 2 , 2 } )
exponentialHistogramDataPoint . SetZeroCount ( 2 )
exponentialHistogramDataPoint . SetCount ( 10 )
exponentialHistogramDataPoint . SetSum ( 30.0 )
exponentialHistogramDataPoint . Attributes ( ) . PutStr ( "foo.bar" , "baz" )
return pmetricotlp . NewExportRequestFromMetrics ( d )
}