-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jan Fajerski <[email protected]>
- Loading branch information
Showing
8 changed files
with
317 additions
and
1,712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
module github.com/prometheus/prom2json | ||
|
||
go 1.17 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 | ||
github.com/prometheus/client_model v0.6.1 | ||
github.com/prometheus/common v0.53.0 | ||
github.com/prometheus/common v0.54.0 | ||
github.com/prometheus/prometheus v0.53.0 | ||
) | ||
|
||
require ( | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
google.golang.org/protobuf v1.34.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2020 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 histogram | ||
|
||
import ( | ||
"fmt" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
model "github.com/prometheus/prometheus/model/histogram" | ||
) | ||
|
||
type APIBucket[BC model.BucketCount] struct { | ||
Boundaries uint64 | ||
Lower, Upper float64 | ||
Count BC | ||
} | ||
|
||
func NewModelHistogram(ch *dto.Histogram) (*model.Histogram, *model.FloatHistogram) { | ||
if ch.GetSampleCountFloat() > 0 || ch.GetZeroCountFloat() > 0 { | ||
// It is a float histogram. | ||
fh := model.FloatHistogram{ | ||
Count: ch.GetSampleCountFloat(), | ||
Sum: ch.GetSampleSum(), | ||
ZeroThreshold: ch.GetZeroThreshold(), | ||
ZeroCount: ch.GetZeroCountFloat(), | ||
Schema: ch.GetSchema(), | ||
PositiveSpans: make([]model.Span, len(ch.GetPositiveSpan())), | ||
PositiveBuckets: ch.GetPositiveCount(), | ||
NegativeSpans: make([]model.Span, len(ch.GetNegativeSpan())), | ||
NegativeBuckets: ch.GetNegativeCount(), | ||
} | ||
for i, span := range ch.GetPositiveSpan() { | ||
fh.PositiveSpans[i].Offset = span.GetOffset() | ||
fh.PositiveSpans[i].Length = span.GetLength() | ||
} | ||
for i, span := range ch.GetNegativeSpan() { | ||
fh.NegativeSpans[i].Offset = span.GetOffset() | ||
fh.NegativeSpans[i].Length = span.GetLength() | ||
} | ||
return nil, &fh | ||
} | ||
h := model.Histogram{ | ||
Count: ch.GetSampleCount(), | ||
Sum: ch.GetSampleSum(), | ||
ZeroThreshold: ch.GetZeroThreshold(), | ||
ZeroCount: ch.GetZeroCount(), | ||
Schema: ch.GetSchema(), | ||
PositiveSpans: make([]model.Span, len(ch.GetPositiveSpan())), | ||
PositiveBuckets: ch.GetPositiveDelta(), | ||
NegativeSpans: make([]model.Span, len(ch.GetNegativeSpan())), | ||
NegativeBuckets: ch.GetNegativeDelta(), | ||
} | ||
for i, span := range ch.GetPositiveSpan() { | ||
h.PositiveSpans[i].Offset = span.GetOffset() | ||
h.PositiveSpans[i].Length = span.GetLength() | ||
} | ||
for i, span := range ch.GetNegativeSpan() { | ||
h.NegativeSpans[i].Offset = span.GetOffset() | ||
h.NegativeSpans[i].Length = span.GetLength() | ||
} | ||
return &h, nil | ||
} | ||
|
||
func BucketsAsJson[BC model.BucketCount](buckets []APIBucket[BC]) [][]interface{} { | ||
ret := make([][]interface{}, len(buckets)) | ||
for i, b := range buckets { | ||
ret[i] = []interface{}{b.Boundaries, fmt.Sprintf("%v", b.Lower), fmt.Sprintf("%v", b.Upper), fmt.Sprintf("%v", b.Count)} | ||
} | ||
return ret | ||
} | ||
|
||
func GetAPIBuckets(h *model.Histogram) []APIBucket[uint64] { | ||
var apiBuckets []APIBucket[uint64] | ||
var nBuckets []model.Bucket[uint64] | ||
for it := h.NegativeBucketIterator(); it.Next(); { | ||
bucket := it.At() | ||
if bucket.Count != 0 { | ||
nBuckets = append(nBuckets, it.At()) | ||
} | ||
} | ||
for i := len(nBuckets) - 1; i >= 0; i-- { | ||
apiBuckets = append(apiBuckets, makeBucket[uint64](nBuckets[i])) | ||
} | ||
|
||
if h.ZeroCount != 0 { | ||
apiBuckets = append(apiBuckets, makeBucket[uint64](h.ZeroBucket())) | ||
} | ||
|
||
for it := h.PositiveBucketIterator(); it.Next(); { | ||
bucket := it.At() | ||
if bucket.Count != 0 { | ||
apiBuckets = append(apiBuckets, makeBucket[uint64](bucket)) | ||
} | ||
} | ||
return apiBuckets | ||
} | ||
|
||
func GetAPIFloatBuckets(h *model.FloatHistogram) []APIBucket[float64] { | ||
var apiBuckets []APIBucket[float64] | ||
var nBuckets []model.Bucket[float64] | ||
for it := h.NegativeBucketIterator(); it.Next(); { | ||
bucket := it.At() | ||
if bucket.Count != 0 { | ||
nBuckets = append(nBuckets, it.At()) | ||
} | ||
} | ||
for i := len(nBuckets) - 1; i >= 0; i-- { | ||
apiBuckets = append(apiBuckets, makeBucket[float64](nBuckets[i])) | ||
} | ||
|
||
if h.ZeroCount != 0 { | ||
apiBuckets = append(apiBuckets, makeBucket[float64](h.ZeroBucket())) | ||
} | ||
|
||
for it := h.PositiveBucketIterator(); it.Next(); { | ||
bucket := it.At() | ||
if bucket.Count != 0 { | ||
apiBuckets = append(apiBuckets, makeBucket[float64](bucket)) | ||
} | ||
} | ||
return apiBuckets | ||
} | ||
|
||
func makeBucket[BC model.BucketCount](bucket model.Bucket[BC]) APIBucket[BC] { | ||
boundaries := uint64(2) // () Exclusive on both sides AKA open interval. | ||
if bucket.LowerInclusive { | ||
if bucket.UpperInclusive { | ||
boundaries = 3 // [] Inclusive on both sides AKA closed interval. | ||
} else { | ||
boundaries = 1 // [) Inclusive only on lower end AKA right open. | ||
} | ||
} else { | ||
if bucket.UpperInclusive { | ||
boundaries = 0 // (] Inclusive only on upper end AKA left open. | ||
} | ||
} | ||
return APIBucket[BC]{ | ||
Boundaries: boundaries, | ||
Lower: bucket.Lower, | ||
Upper: bucket.Upper, | ||
Count: bucket.Count, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.