github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/chunk/fixtures.go (about)

     1  package chunk
     2  
     3  // Chunk functions used only in tests
     4  
     5  import (
     6  	"context"
     7  	"time"
     8  
     9  	"github.com/prometheus/common/model"
    10  	"github.com/prometheus/prometheus/pkg/labels"
    11  
    12  	"github.com/cortexproject/cortex/pkg/util"
    13  )
    14  
    15  // BenchmarkLabels is a real example from Kubernetes' embedded cAdvisor metrics, lightly obfuscated
    16  var BenchmarkLabels = labels.Labels{
    17  	{Name: model.MetricNameLabel, Value: "container_cpu_usage_seconds_total"},
    18  	{Name: "beta_kubernetes_io_arch", Value: "amd64"},
    19  	{Name: "beta_kubernetes_io_instance_type", Value: "c3.somesize"},
    20  	{Name: "beta_kubernetes_io_os", Value: "linux"},
    21  	{Name: "container_name", Value: "some-name"},
    22  	{Name: "cpu", Value: "cpu01"},
    23  	{Name: "failure_domain_beta_kubernetes_io_region", Value: "somewhere-1"},
    24  	{Name: "failure_domain_beta_kubernetes_io_zone", Value: "somewhere-1b"},
    25  	{Name: "id", Value: "/kubepods/burstable/pod6e91c467-e4c5-11e7-ace3-0a97ed59c75e/a3c8498918bd6866349fed5a6f8c643b77c91836427fb6327913276ebc6bde28"},
    26  	{Name: "image", Value: "registry/organisation/name@sha256:dca3d877a80008b45d71d7edc4fd2e44c0c8c8e7102ba5cbabec63a374d1d506"},
    27  	{Name: "instance", Value: "ip-111-11-1-11.ec2.internal"},
    28  	{Name: "job", Value: "kubernetes-cadvisor"},
    29  	{Name: "kubernetes_io_hostname", Value: "ip-111-11-1-11"},
    30  	{Name: "monitor", Value: "prod"},
    31  	{Name: "name", Value: "k8s_some-name_some-other-name-5j8s8_kube-system_6e91c467-e4c5-11e7-ace3-0a97ed59c75e_0"},
    32  	{Name: "namespace", Value: "kube-system"},
    33  	{Name: "pod_name", Value: "some-other-name-5j8s8"},
    34  }
    35  
    36  // DefaultSchemaConfig creates a simple schema config for testing
    37  func DefaultSchemaConfig(store, schema string, from model.Time) SchemaConfig {
    38  	s := SchemaConfig{
    39  		Configs: []PeriodConfig{{
    40  			IndexType: store,
    41  			Schema:    schema,
    42  			From:      DayTime{from},
    43  			ChunkTables: PeriodicTableConfig{
    44  				Prefix: "cortex",
    45  				Period: 7 * 24 * time.Hour,
    46  			},
    47  			IndexTables: PeriodicTableConfig{
    48  				Prefix: "cortex_chunks",
    49  				Period: 7 * 24 * time.Hour,
    50  			},
    51  		}},
    52  	}
    53  	if err := s.Validate(); err != nil {
    54  		panic(err)
    55  	}
    56  	return s
    57  }
    58  
    59  // ChunksToMatrix converts a set of chunks to a model.Matrix.
    60  func ChunksToMatrix(ctx context.Context, chunks []Chunk, from, through model.Time) (model.Matrix, error) {
    61  	// Group chunks by series, sort and dedupe samples.
    62  	metrics := map[model.Fingerprint]model.Metric{}
    63  	samplesBySeries := map[model.Fingerprint][][]model.SamplePair{}
    64  	for _, c := range chunks {
    65  		ss, err := c.Samples(from, through)
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		metrics[c.Fingerprint] = util.LabelsToMetric(c.Metric)
    71  		samplesBySeries[c.Fingerprint] = append(samplesBySeries[c.Fingerprint], ss)
    72  	}
    73  
    74  	matrix := make(model.Matrix, 0, len(samplesBySeries))
    75  	for fp, ss := range samplesBySeries {
    76  		matrix = append(matrix, &model.SampleStream{
    77  			Metric: metrics[fp],
    78  			Values: util.MergeNSampleSets(ss...),
    79  		})
    80  	}
    81  
    82  	return matrix, nil
    83  }