2023-02-01 00:33:50 -08:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/util/jsonutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
jsoniter.RegisterTypeEncoderFunc("promql.Series", marshalSeriesJSON, marshalSeriesJSONIsEmpty)
|
|
|
|
jsoniter.RegisterTypeEncoderFunc("promql.Sample", marshalSampleJSON, marshalSampleJSONIsEmpty)
|
2023-04-14 00:56:11 -07:00
|
|
|
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", marshalFPointJSON, marshalPointJSONIsEmpty)
|
|
|
|
jsoniter.RegisterTypeEncoderFunc("promql.HPoint", marshalHPointJSON, marshalPointJSONIsEmpty)
|
2023-02-01 00:33:50 -08:00
|
|
|
jsoniter.RegisterTypeEncoderFunc("exemplar.Exemplar", marshalExemplarJSON, marshalExemplarJSONEmpty)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONCodec is a Codec that encodes API responses as JSON.
|
|
|
|
type JSONCodec struct{}
|
|
|
|
|
2023-02-23 19:03:00 -08:00
|
|
|
func (j JSONCodec) ContentType() MIMEType {
|
|
|
|
return MIMEType{Type: "application", SubType: "json"}
|
2023-02-01 00:33:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j JSONCodec) CanEncode(_ *Response) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JSONCodec) Encode(resp *Response) ([]byte, error) {
|
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
return json.Marshal(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshalSeriesJSON writes something like the following:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "metric" : {
|
|
|
|
// "__name__" : "up",
|
|
|
|
// "job" : "prometheus",
|
|
|
|
// "instance" : "localhost:9090"
|
|
|
|
// },
|
|
|
|
// "values": [
|
|
|
|
// [ 1435781451.781, "1" ],
|
|
|
|
// < more values>
|
|
|
|
// ],
|
|
|
|
// "histograms": [
|
2023-04-14 00:56:11 -07:00
|
|
|
// [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ],
|
2023-02-01 00:33:50 -08:00
|
|
|
// < more histograms >
|
|
|
|
// ],
|
|
|
|
// },
|
|
|
|
func marshalSeriesJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
s := *((*promql.Series)(ptr))
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
stream.WriteObjectField(`metric`)
|
|
|
|
m, err := s.Metric.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
stream.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stream.SetBuffer(append(stream.Buffer(), m...))
|
|
|
|
|
2023-04-14 00:56:11 -07:00
|
|
|
for i, p := range s.Floats {
|
|
|
|
stream.WriteMore()
|
|
|
|
if i == 0 {
|
|
|
|
stream.WriteObjectField(`values`)
|
|
|
|
stream.WriteArrayStart()
|
2023-02-01 00:33:50 -08:00
|
|
|
}
|
2023-04-14 00:56:11 -07:00
|
|
|
marshalFPointJSON(unsafe.Pointer(&p), stream)
|
2023-02-01 00:33:50 -08:00
|
|
|
}
|
2023-04-14 00:56:11 -07:00
|
|
|
if len(s.Floats) > 0 {
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteArrayEnd()
|
|
|
|
}
|
2023-04-14 00:56:11 -07:00
|
|
|
for i, p := range s.Histograms {
|
|
|
|
stream.WriteMore()
|
|
|
|
if i == 0 {
|
|
|
|
stream.WriteObjectField(`histograms`)
|
|
|
|
stream.WriteArrayStart()
|
2023-02-01 00:33:50 -08:00
|
|
|
}
|
2023-04-14 00:56:11 -07:00
|
|
|
marshalHPointJSON(unsafe.Pointer(&p), stream)
|
|
|
|
}
|
|
|
|
if len(s.Histograms) > 0 {
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteArrayEnd()
|
|
|
|
}
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
2023-04-26 06:25:21 -07:00
|
|
|
func marshalSeriesJSONIsEmpty(unsafe.Pointer) bool {
|
2023-02-01 00:33:50 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshalSampleJSON writes something like the following for normal value samples:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "metric" : {
|
|
|
|
// "__name__" : "up",
|
|
|
|
// "job" : "prometheus",
|
|
|
|
// "instance" : "localhost:9090"
|
|
|
|
// },
|
2023-04-14 00:56:11 -07:00
|
|
|
// "value": [ 1435781451.781, "1.234" ]
|
2023-02-01 00:33:50 -08:00
|
|
|
// },
|
|
|
|
//
|
|
|
|
// For histogram samples, it writes something like this:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "metric" : {
|
|
|
|
// "__name__" : "up",
|
|
|
|
// "job" : "prometheus",
|
|
|
|
// "instance" : "localhost:9090"
|
|
|
|
// },
|
2023-04-14 00:56:11 -07:00
|
|
|
// "histogram": [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ]
|
2023-02-01 00:33:50 -08:00
|
|
|
// },
|
|
|
|
func marshalSampleJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
s := *((*promql.Sample)(ptr))
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
stream.WriteObjectField(`metric`)
|
|
|
|
m, err := s.Metric.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
stream.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stream.SetBuffer(append(stream.Buffer(), m...))
|
|
|
|
stream.WriteMore()
|
2023-04-14 00:56:11 -07:00
|
|
|
if s.H == nil {
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteObjectField(`value`)
|
|
|
|
} else {
|
|
|
|
stream.WriteObjectField(`histogram`)
|
|
|
|
}
|
2023-04-14 00:56:11 -07:00
|
|
|
stream.WriteArrayStart()
|
|
|
|
jsonutil.MarshalTimestamp(s.T, stream)
|
|
|
|
stream.WriteMore()
|
|
|
|
if s.H == nil {
|
|
|
|
jsonutil.MarshalFloat(s.F, stream)
|
|
|
|
} else {
|
|
|
|
jsonutil.MarshalHistogram(s.H, stream)
|
|
|
|
}
|
|
|
|
stream.WriteArrayEnd()
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
2023-04-26 06:25:21 -07:00
|
|
|
func marshalSampleJSONIsEmpty(unsafe.Pointer) bool {
|
2023-02-01 00:33:50 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-14 00:56:11 -07:00
|
|
|
// marshalFPointJSON writes `[ts, "1.234"]`.
|
|
|
|
func marshalFPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
p := *((*promql.FPoint)(ptr))
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteArrayStart()
|
|
|
|
jsonutil.MarshalTimestamp(p.T, stream)
|
|
|
|
stream.WriteMore()
|
2023-04-14 00:56:11 -07:00
|
|
|
jsonutil.MarshalFloat(p.F, stream)
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteArrayEnd()
|
|
|
|
}
|
|
|
|
|
2023-04-14 00:56:11 -07:00
|
|
|
// marshalHPointJSON writes `[ts, { < histogram, see jsonutil.MarshalHistogram > } ]`.
|
|
|
|
func marshalHPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
p := *((*promql.HPoint)(ptr))
|
|
|
|
stream.WriteArrayStart()
|
|
|
|
jsonutil.MarshalTimestamp(p.T, stream)
|
2023-02-01 00:33:50 -08:00
|
|
|
stream.WriteMore()
|
2023-04-14 00:56:11 -07:00
|
|
|
jsonutil.MarshalHistogram(p.H, stream)
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
}
|
2023-02-01 00:33:50 -08:00
|
|
|
|
2023-04-26 06:25:21 -07:00
|
|
|
func marshalPointJSONIsEmpty(unsafe.Pointer) bool {
|
2023-04-14 00:56:11 -07:00
|
|
|
return false
|
2023-02-01 00:33:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// marshalExemplarJSON writes.
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// labels: <labels>,
|
|
|
|
// value: "<string>",
|
|
|
|
// timestamp: <float>
|
|
|
|
// }
|
|
|
|
func marshalExemplarJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
p := *((*exemplar.Exemplar)(ptr))
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
|
|
|
|
// "labels" key.
|
|
|
|
stream.WriteObjectField(`labels`)
|
|
|
|
lbls, err := p.Labels.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
stream.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stream.SetBuffer(append(stream.Buffer(), lbls...))
|
|
|
|
|
|
|
|
// "value" key.
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField(`value`)
|
2023-04-14 00:56:11 -07:00
|
|
|
jsonutil.MarshalFloat(p.Value, stream)
|
2023-02-01 00:33:50 -08:00
|
|
|
|
|
|
|
// "timestamp" key.
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField(`timestamp`)
|
|
|
|
jsonutil.MarshalTimestamp(p.Ts, stream)
|
|
|
|
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
2023-04-26 06:25:21 -07:00
|
|
|
func marshalExemplarJSONEmpty(unsafe.Pointer) bool {
|
2023-02-01 00:33:50 -08:00
|
|
|
return false
|
|
|
|
}
|