diff --git a/pkg/logcli/query/query.go b/pkg/logcli/query/query.go index 5acaa38b597f..cf5fb3c04c6f 100644 --- a/pkg/logcli/query/query.go +++ b/pkg/logcli/query/query.go @@ -65,6 +65,8 @@ func (q *Query) DoQuery(c *client.Client, out output.LogOutput, statistics bool) case logql.ValueTypeStreams: streams := resp.Data.Result.(loghttp.Streams) q.printStream(streams, out) + case promql.ValueTypeScalar: + q.printScalar(resp.Data.Result.(loghttp.Scalar)) case promql.ValueTypeMatrix: matrix := resp.Data.Result.(loghttp.Matrix) q.printMatrix(matrix) @@ -170,6 +172,16 @@ func (q *Query) printVector(vector loghttp.Vector) { fmt.Print(string(bytes)) } +func (q *Query) printScalar(scalar loghttp.Scalar) { + bytes, err := json.MarshalIndent(scalar, "", " ") + + if err != nil { + log.Fatalf("Error marshalling scalar: %v", err) + } + + fmt.Print(string(bytes)) +} + type kvLogger struct { *tabwriter.Writer } diff --git a/pkg/loghttp/query.go b/pkg/loghttp/query.go index c6180474ecd8..66bb77368841 100644 --- a/pkg/loghttp/query.go +++ b/pkg/loghttp/query.go @@ -182,6 +182,19 @@ func (e *Entry) UnmarshalJSON(data []byte) error { // Scalar is a single timestamp/float with no labels type Scalar model.Scalar +func (s Scalar) MarshalJSON() ([]byte, error) { + return model.Scalar(s).MarshalJSON() +} + +func (s *Scalar) UnmarshalJSON(b []byte) error { + var v model.Scalar + if err := v.UnmarshalJSON(b); err != nil { + return err + } + *s = Scalar(v) + return nil +} + // Vector is a slice of Samples type Vector []model.Sample diff --git a/pkg/logql/marshal/query.go b/pkg/logql/marshal/query.go index 3a9e19ff5d08..7ec1f70fdb15 100644 --- a/pkg/logql/marshal/query.go +++ b/pkg/logql/marshal/query.go @@ -29,6 +29,16 @@ func NewResultValue(v promql.Value) (loghttp.ResultValue, error) { if err != nil { return nil, err } + + case loghttp.ResultTypeScalar: + scalar, ok := v.(promql.Scalar) + + if !ok { + return nil, fmt.Errorf("unexpected type %T for scalar", scalar) + } + + value = NewScalar(scalar) + case loghttp.ResultTypeVector: vector, ok := v.(promql.Vector) @@ -95,6 +105,14 @@ func NewEntry(e logproto.Entry) loghttp.Entry { } } +func NewScalar(s promql.Scalar) loghttp.Scalar { + return loghttp.Scalar{ + Timestamp: model.Time(s.T), + Value: model.SampleValue(s.V), + } + +} + // NewVector constructs a Vector from a promql.Vector func NewVector(v promql.Vector) loghttp.Vector { ret := make([]model.Sample, len(v))