api support for scalars (#1704)

* adds conversions to loghttp scalar types

* logcli prints scalars, lothttp scalars use prometheus json marshaling
pull/1707/head
Owen Diehl 6 years ago committed by GitHub
parent 414f95fedc
commit db6fd7e779
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      pkg/logcli/query/query.go
  2. 13
      pkg/loghttp/query.go
  3. 18
      pkg/logql/marshal/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
}

@ -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

@ -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))

Loading…
Cancel
Save