|
| 1 | +// Copyright 2013 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package model |
| 15 | + |
| 16 | +import ( |
| 17 | + "math" |
| 18 | + "strconv" |
| 19 | + |
| 20 | + jsoniter "github.com/json-iterator/go" |
| 21 | +) |
| 22 | + |
| 23 | +// from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go |
| 24 | +// MarshalTimestamp marshals a point timestamp using the passed jsoniter stream. |
| 25 | +func MarshalTimestamp(t int64, stream *jsoniter.Stream) { |
| 26 | + // Write out the timestamp as a float divided by 1000. |
| 27 | + // This is ~3x faster than converting to a float. |
| 28 | + if t < 0 { |
| 29 | + stream.WriteRaw(`-`) |
| 30 | + t = -t |
| 31 | + } |
| 32 | + stream.WriteInt64(t / 1000) |
| 33 | + fraction := t % 1000 |
| 34 | + if fraction != 0 { |
| 35 | + stream.WriteRaw(`.`) |
| 36 | + if fraction < 100 { |
| 37 | + stream.WriteRaw(`0`) |
| 38 | + } |
| 39 | + if fraction < 10 { |
| 40 | + stream.WriteRaw(`0`) |
| 41 | + } |
| 42 | + stream.WriteInt64(fraction) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// adapted from https://github.com/prometheus/prometheus/blob/main/util/jsonutil/marshal.go |
| 47 | +// MarshalValue marshals a point value using the passed jsoniter stream. |
| 48 | +func MarshalValue(f FloatString, stream *jsoniter.Stream) { |
| 49 | + v := float64(f) |
| 50 | + stream.WriteRaw(`"`) |
| 51 | + // Taken from https://github.com/json-iterator/go/blob/master/stream_float.go#L71 as a workaround |
| 52 | + // to https://github.com/json-iterator/go/issues/365 (jsoniter, to follow json standard, doesn't allow inf/nan). |
| 53 | + buf := stream.Buffer() |
| 54 | + abs := math.Abs(v) |
| 55 | + fmt := byte('f') |
| 56 | + // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. |
| 57 | + if abs != 0 { |
| 58 | + if abs < 1e-6 || abs >= 1e21 { |
| 59 | + fmt = 'e' |
| 60 | + } |
| 61 | + } |
| 62 | + buf = strconv.AppendFloat(buf, v, fmt, -1, 64) |
| 63 | + stream.SetBuffer(buf) |
| 64 | + stream.WriteRaw(`"`) |
| 65 | +} |
| 66 | + |
| 67 | +// adapted from https://github.com/prometheus/prometheus/blob/main/web/api/v1/api.go |
| 68 | +func MarshalHistogramBucket(b HistogramBucket, stream *jsoniter.Stream) { |
| 69 | + stream.WriteArrayStart() |
| 70 | + stream.WriteInt32(b.Boundaries) |
| 71 | + stream.WriteMore() |
| 72 | + MarshalValue(b.Lower, stream) |
| 73 | + stream.WriteMore() |
| 74 | + MarshalValue(b.Upper, stream) |
| 75 | + stream.WriteMore() |
| 76 | + MarshalValue(b.Count, stream) |
| 77 | + stream.WriteArrayEnd() |
| 78 | +} |
0 commit comments