github.com/thanos-io/thanos@v0.32.5/pkg/exemplars/exemplars_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package exemplars
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/efficientgo/core/testutil"
    10  	"github.com/thanos-io/thanos/pkg/exemplars/exemplarspb"
    11  	"github.com/thanos-io/thanos/pkg/store/labelpb"
    12  	"github.com/thanos-io/thanos/pkg/testutil/custom"
    13  )
    14  
    15  func TestMain(m *testing.M) {
    16  	custom.TolerantVerifyLeakMain(m)
    17  }
    18  
    19  func TestDedupExemplarsResponse(t *testing.T) {
    20  	for _, tc := range []struct {
    21  		name            string
    22  		exemplars, want []*exemplarspb.ExemplarData
    23  		replicaLabels   []string
    24  	}{
    25  		{
    26  			name:      "nil slice",
    27  			exemplars: nil,
    28  			want:      nil,
    29  		},
    30  		{
    31  			name:      "empty exemplars data slice",
    32  			exemplars: []*exemplarspb.ExemplarData{},
    33  			want:      []*exemplarspb.ExemplarData{},
    34  		},
    35  		{
    36  			name: "empty exemplars data",
    37  			exemplars: []*exemplarspb.ExemplarData{
    38  				{
    39  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    40  						{Name: "__name__", Value: "test_exemplar_metric_total"},
    41  						{Name: "instance", Value: "localhost:8090"},
    42  						{Name: "job", Value: "prometheus"},
    43  						{Name: "service", Value: "bar"},
    44  					}},
    45  				},
    46  			},
    47  			want: []*exemplarspb.ExemplarData{},
    48  		},
    49  		{
    50  			name:          "multiple series",
    51  			replicaLabels: []string{"replica"},
    52  			exemplars: []*exemplarspb.ExemplarData{
    53  				{
    54  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    55  						{Name: "__name__", Value: "test_exemplar_metric_total"},
    56  						{Name: "instance", Value: "localhost:8090"},
    57  						{Name: "job", Value: "prometheus"},
    58  						{Name: "service", Value: "bar"},
    59  						{Name: "replica", Value: "0"},
    60  					}},
    61  					Exemplars: []*exemplarspb.Exemplar{
    62  						{
    63  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    64  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
    65  							}},
    66  							Value: 19,
    67  							Ts:    1600096955479,
    68  						},
    69  						{
    70  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    71  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
    72  							}},
    73  							Value: 19,
    74  							Ts:    1600096955479,
    75  						},
    76  					},
    77  				},
    78  				{
    79  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    80  						{Name: "__name__", Value: "test_exemplar_metric_total"},
    81  						{Name: "instance", Value: "localhost:8090"},
    82  						{Name: "job", Value: "prometheus"},
    83  						{Name: "service", Value: "bar"},
    84  						{Name: "replica", Value: "1"},
    85  					}},
    86  					Exemplars: []*exemplarspb.Exemplar{
    87  						{
    88  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
    89  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
    90  							}},
    91  							Value: 19,
    92  							Ts:    1600096955479,
    93  						},
    94  					},
    95  				},
    96  			},
    97  			want: []*exemplarspb.ExemplarData{
    98  				{
    99  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   100  						{Name: "__name__", Value: "test_exemplar_metric_total"},
   101  						{Name: "instance", Value: "localhost:8090"},
   102  						{Name: "job", Value: "prometheus"},
   103  						{Name: "service", Value: "bar"},
   104  					}},
   105  					Exemplars: []*exemplarspb.Exemplar{
   106  						{
   107  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   108  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
   109  							}},
   110  							Value: 19,
   111  							Ts:    1600096955479,
   112  						},
   113  					},
   114  				},
   115  			},
   116  		},
   117  		{
   118  			name:          "multiple series with multiple exemplars data",
   119  			replicaLabels: []string{"replica"},
   120  			exemplars: []*exemplarspb.ExemplarData{
   121  				{
   122  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   123  						{Name: "__name__", Value: "test_exemplar_metric_total"},
   124  						{Name: "instance", Value: "localhost:8090"},
   125  						{Name: "job", Value: "prometheus"},
   126  						{Name: "service", Value: "bar"},
   127  						{Name: "replica", Value: "0"},
   128  					}},
   129  					Exemplars: []*exemplarspb.Exemplar{
   130  						{
   131  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   132  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
   133  							}},
   134  							Value: 19,
   135  							Ts:    1600096955479,
   136  						},
   137  						{
   138  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   139  								{Name: "traceID", Value: "foo"},
   140  							}},
   141  							Value: 19,
   142  							Ts:    1600096955470,
   143  						},
   144  					},
   145  				},
   146  				{
   147  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   148  						{Name: "__name__", Value: "test_exemplar_metric_total"},
   149  						{Name: "instance", Value: "localhost:8090"},
   150  						{Name: "job", Value: "prometheus"},
   151  						{Name: "service", Value: "bar"},
   152  						{Name: "replica", Value: "1"},
   153  					}},
   154  					Exemplars: []*exemplarspb.Exemplar{
   155  						{
   156  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   157  								{Name: "traceID", Value: "bar"},
   158  							}},
   159  							Value: 19,
   160  							Ts:    1600096955579,
   161  						},
   162  						{
   163  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   164  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
   165  							}},
   166  							Value: 19,
   167  							Ts:    1600096955479,
   168  						},
   169  						// Same ts but different labels, cannot dedup.
   170  						{
   171  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   172  								{Name: "traceID", Value: "test"},
   173  							}},
   174  							Value: 19,
   175  							Ts:    1600096955479,
   176  						},
   177  					},
   178  				},
   179  			},
   180  			want: []*exemplarspb.ExemplarData{
   181  				{
   182  					SeriesLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   183  						{Name: "__name__", Value: "test_exemplar_metric_total"},
   184  						{Name: "instance", Value: "localhost:8090"},
   185  						{Name: "job", Value: "prometheus"},
   186  						{Name: "service", Value: "bar"},
   187  					}},
   188  					Exemplars: []*exemplarspb.Exemplar{
   189  						{
   190  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   191  								{Name: "traceID", Value: "foo"},
   192  							}},
   193  							Value: 19,
   194  							Ts:    1600096955470,
   195  						},
   196  						{
   197  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   198  								{Name: "traceID", Value: "EpTxMJ40fUus7aGY"},
   199  							}},
   200  							Value: 19,
   201  							Ts:    1600096955479,
   202  						},
   203  						{
   204  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   205  								{Name: "traceID", Value: "test"},
   206  							}},
   207  							Value: 19,
   208  							Ts:    1600096955479,
   209  						},
   210  						{
   211  							Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{
   212  								{Name: "traceID", Value: "bar"},
   213  							}},
   214  							Value: 19,
   215  							Ts:    1600096955579,
   216  						},
   217  					},
   218  				},
   219  			},
   220  		},
   221  	} {
   222  		t.Run(tc.name, func(t *testing.T) {
   223  			replicaLabels := make(map[string]struct{})
   224  			for _, lbl := range tc.replicaLabels {
   225  				replicaLabels[lbl] = struct{}{}
   226  			}
   227  			testutil.Equals(t, tc.want, dedupExemplarsResponse(tc.exemplars, replicaLabels))
   228  		})
   229  	}
   230  }