github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/marshal/query.go (about)

     1  package marshal
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/prometheus/common/model"
     8  	"github.com/prometheus/prometheus/model/labels"
     9  	"github.com/prometheus/prometheus/promql"
    10  	"github.com/prometheus/prometheus/promql/parser"
    11  
    12  	"github.com/grafana/loki/pkg/loghttp"
    13  	"github.com/grafana/loki/pkg/logproto"
    14  	"github.com/grafana/loki/pkg/logqlmodel"
    15  )
    16  
    17  // NewResultValue constructs a ResultValue from a promql.Value
    18  func NewResultValue(v parser.Value) (loghttp.ResultValue, error) {
    19  	var err error
    20  	var value loghttp.ResultValue
    21  
    22  	switch v.Type() {
    23  	case loghttp.ResultTypeStream:
    24  		s, ok := v.(logqlmodel.Streams)
    25  
    26  		if !ok {
    27  			return nil, fmt.Errorf("unexpected type %T for streams", s)
    28  		}
    29  
    30  		value, err = NewStreams(s)
    31  
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  
    36  	case loghttp.ResultTypeScalar:
    37  		scalar, ok := v.(promql.Scalar)
    38  
    39  		if !ok {
    40  			return nil, fmt.Errorf("unexpected type %T for scalar", scalar)
    41  		}
    42  
    43  		value = NewScalar(scalar)
    44  
    45  	case loghttp.ResultTypeVector:
    46  		vector, ok := v.(promql.Vector)
    47  
    48  		if !ok {
    49  			return nil, fmt.Errorf("unexpected type %T for vector", vector)
    50  		}
    51  
    52  		value = NewVector(vector)
    53  	case loghttp.ResultTypeMatrix:
    54  		m, ok := v.(promql.Matrix)
    55  
    56  		if !ok {
    57  			return nil, fmt.Errorf("unexpected type %T for matrix", m)
    58  		}
    59  
    60  		value = NewMatrix(m)
    61  	default:
    62  		return nil, fmt.Errorf("v1 endpoints do not support type %s", v.Type())
    63  	}
    64  
    65  	return value, nil
    66  }
    67  
    68  // NewStreams constructs a Streams from a logql.Streams
    69  func NewStreams(s logqlmodel.Streams) (loghttp.Streams, error) {
    70  	var err error
    71  	ret := make([]loghttp.Stream, len(s))
    72  
    73  	for i, stream := range s {
    74  		ret[i], err = NewStream(stream)
    75  
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  	}
    80  
    81  	return ret, nil
    82  }
    83  
    84  // NewStream constructs a Stream from a logproto.Stream
    85  func NewStream(s logproto.Stream) (loghttp.Stream, error) {
    86  	labels, err := NewLabelSet(s.Labels)
    87  	if err != nil {
    88  		return loghttp.Stream{}, errors.Wrapf(err, "err while creating labelset for %s", s.Labels)
    89  	}
    90  
    91  	ret := loghttp.Stream{
    92  		Labels:  labels,
    93  		Entries: make([]loghttp.Entry, len(s.Entries)),
    94  	}
    95  
    96  	for i, e := range s.Entries {
    97  		ret.Entries[i] = NewEntry(e)
    98  	}
    99  
   100  	return ret, nil
   101  }
   102  
   103  // NewEntry constructs an Entry from a logproto.Entry
   104  func NewEntry(e logproto.Entry) loghttp.Entry {
   105  	return loghttp.Entry{
   106  		Timestamp: e.Timestamp,
   107  		Line:      e.Line,
   108  	}
   109  }
   110  
   111  func NewScalar(s promql.Scalar) loghttp.Scalar {
   112  	return loghttp.Scalar{
   113  		Timestamp: model.Time(s.T),
   114  		Value:     model.SampleValue(s.V),
   115  	}
   116  
   117  }
   118  
   119  // NewVector constructs a Vector from a promql.Vector
   120  func NewVector(v promql.Vector) loghttp.Vector {
   121  	ret := make([]model.Sample, len(v))
   122  
   123  	for i, s := range v {
   124  		ret[i] = NewSample(s)
   125  	}
   126  
   127  	return ret
   128  }
   129  
   130  // NewSample constructs a model.Sample from a promql.Sample
   131  func NewSample(s promql.Sample) model.Sample {
   132  
   133  	ret := model.Sample{
   134  		Value:     model.SampleValue(s.V),
   135  		Timestamp: model.Time(s.T),
   136  		Metric:    NewMetric(s.Metric),
   137  	}
   138  
   139  	return ret
   140  }
   141  
   142  // NewMatrix constructs a Matrix from a promql.Matrix
   143  func NewMatrix(m promql.Matrix) loghttp.Matrix {
   144  	ret := make([]model.SampleStream, len(m))
   145  
   146  	for i, s := range m {
   147  		ret[i] = NewSampleStream(s)
   148  	}
   149  
   150  	return ret
   151  }
   152  
   153  // NewSampleStream constructs a model.SampleStream from a promql.Series
   154  func NewSampleStream(s promql.Series) model.SampleStream {
   155  	ret := model.SampleStream{
   156  		Metric: NewMetric(s.Metric),
   157  		Values: make([]model.SamplePair, len(s.Points)),
   158  	}
   159  
   160  	for i, p := range s.Points {
   161  		ret.Values[i].Timestamp = model.Time(p.T)
   162  		ret.Values[i].Value = model.SampleValue(p.V)
   163  	}
   164  
   165  	return ret
   166  }
   167  
   168  // NewMetric constructs a labels.Labels from a model.Metric
   169  func NewMetric(l labels.Labels) model.Metric {
   170  	ret := make(map[model.LabelName]model.LabelValue)
   171  
   172  	for _, label := range l {
   173  		ret[model.LabelName(label.Name)] = model.LabelValue(label.Value)
   174  	}
   175  
   176  	return ret
   177  }