prometheus/web/api/v1/json_codec.go

220 lines
5.7 KiB
Go
Raw Normal View History

Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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)
jsoniter.RegisterTypeEncoderFunc("promql.FPoint", marshalFPointJSON, marshalPointJSONIsEmpty)
jsoniter.RegisterTypeEncoderFunc("promql.HPoint", marshalHPointJSON, marshalPointJSONIsEmpty)
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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{}
func (j JSONCodec) ContentType() MIMEType {
return MIMEType{Type: "application", SubType: "json"}
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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": [
// [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ],
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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...))
for i, p := range s.Floats {
stream.WriteMore()
if i == 0 {
stream.WriteObjectField(`values`)
stream.WriteArrayStart()
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
}
marshalFPointJSON(unsafe.Pointer(&p), stream)
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
}
if len(s.Floats) > 0 {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteArrayEnd()
}
for i, p := range s.Histograms {
stream.WriteMore()
if i == 0 {
stream.WriteObjectField(`histograms`)
stream.WriteArrayStart()
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
}
marshalHPointJSON(unsafe.Pointer(&p), stream)
}
if len(s.Histograms) > 0 {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteArrayEnd()
}
stream.WriteObjectEnd()
}
func marshalSeriesJSONIsEmpty(unsafe.Pointer) bool {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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"
// },
// "value": [ 1435781451.781, "1.234" ]
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
// },
//
// For histogram samples, it writes something like this:
//
// {
// "metric" : {
// "__name__" : "up",
// "job" : "prometheus",
// "instance" : "localhost:9090"
// },
// "histogram": [ 1435781451.781, { < histogram, see jsonutil.MarshalHistogram > } ]
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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()
if s.H == nil {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteObjectField(`value`)
} else {
stream.WriteObjectField(`histogram`)
}
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()
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteObjectEnd()
}
func marshalSampleJSONIsEmpty(unsafe.Pointer) bool {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
return false
}
// marshalFPointJSON writes `[ts, "1.234"]`.
func marshalFPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
p := *((*promql.FPoint)(ptr))
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteArrayStart()
jsonutil.MarshalTimestamp(p.T, stream)
stream.WriteMore()
jsonutil.MarshalFloat(p.F, stream)
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteArrayEnd()
}
// 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)
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
stream.WriteMore()
jsonutil.MarshalHistogram(p.H, stream)
stream.WriteArrayEnd()
}
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
func marshalPointJSONIsEmpty(unsafe.Pointer) bool {
return false
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
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`)
jsonutil.MarshalFloat(p.Value, stream)
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
// "timestamp" key.
stream.WriteMore()
stream.WriteObjectField(`timestamp`)
jsonutil.MarshalTimestamp(p.Ts, stream)
stream.WriteObjectEnd()
}
func marshalExemplarJSONEmpty(unsafe.Pointer) bool {
Add extension point for returning different content types from API endpoints (#412) * Add initial sketch of Codec interface. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Introduce JSON codec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Expose Response type so that consuming applications (eg. Mimir) can implement their own Codecs. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add sketch of what supporting different codecs could look like. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename fallbackCodec to defaultCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Remove defaultCodec as a field on API. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename AddCodec() and clarify expected behaviour. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Modify TestRespond to test JsonCodec directly. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Refactor existing respond() test in preparation for content negotiation test cases. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add tests for content negotiation. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add missing documentation comments. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Add another test case. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Rename JsonCodec to JSONCodec. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fix linting issue. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Fallback to JSON codec if no acceptable codec can be found for the Accept header. Signed-off-by: Charles Korn <charles.korn@grafana.com> * Move custom jsoniter code into json_codec.go. Signed-off-by: Charles Korn <charles.korn@grafana.com> --------- Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-02-01 00:33:50 -08:00
return false
}