Skip to content

Commit

Permalink
Merge pull request #175 from prometheus/beorn7/histogram
Browse files Browse the repository at this point in the history
Fix issue with histogram rendering
  • Loading branch information
jan--f committed Jul 11, 2024
2 parents f80a193 + 472bd39 commit db513a9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
43 changes: 39 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ Example input from stdin:
Note that all numbers are encoded as strings. Some parsers want it
that way. Also, Prometheus allows sample values like `NaN` or `+Inf`,
which cannot be encoded as JSON numbers.
Native histograms are formated similarly as [the query
API](https://prometheus.io/docs/prometheus/latest/querying/api/#native-histograms)
would return.

A histogram is formatted as a native histogram if it has at least one span. It
is then formatted in a similar way as [the Prometehus query
API](https://prometheus.io/docs/prometheus/latest/querying/api/#native-histograms)
does it.

```json
[
Expand Down Expand Up @@ -136,7 +138,7 @@ would return.
{
"name": "http_request_duration_seconds",
"type": "HISTOGRAM",
"help": "More HTTP request latencies in seconds.",
"help": "This is a native histogram.",
"metrics": [
{
"labels": {
Expand Down Expand Up @@ -166,6 +168,39 @@ would return.
"sum": "29969.50000000001"
}
]
},
{
"name": "some_weird_normal_distribution",
"type": "HISTOGRAM",
"help": "This is a classic histogram.",
"metrics": [
{
"buckets": {
"-0.0001899999999999998": "17",
"-0.0002899999999999998": "6",
"-0.0003899999999999998": "2",
"-0.0004899999999999998": "2",
"-0.0005899999999999998": "0",
"-0.0006899999999999999": "0",
"-0.0007899999999999999": "0",
"-0.00089": "0",
"-0.00099": "0",
"-8.999999999999979e-05": "33",
"0.00011000000000000022": "75",
"0.00021000000000000023": "92",
"0.0003100000000000002": "100",
"0.0004100000000000002": "103",
"0.0005100000000000003": "105",
"0.0006100000000000003": "106",
"0.0007100000000000003": "107",
"0.0008100000000000004": "107",
"0.0009100000000000004": "107",
"1.0000000000000216e-05": "50"
},
"count": "107",
"sum": "0.001792103516591124"
}
]
}
]
```
Expand Down
14 changes: 8 additions & 6 deletions prom2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,24 @@ func getValue(m *dto.Metric) float64 {
}

func makeHistogram(m *dto.Metric) Histogram {
dtoH := m.GetHistogram()
hist := Histogram{
Labels: makeLabels(m),
TimestampMs: makeTimestamp(m),
Count: fmt.Sprint(m.GetHistogram().GetSampleCount()),
Sum: fmt.Sprint(m.GetHistogram().GetSampleSum()),
Count: fmt.Sprint(dtoH.GetSampleCount()),
Sum: fmt.Sprint(dtoH.GetSampleSum()),
}
if b := makeBuckets(m); len(b) > 0 {
hist.Buckets = b
} else {
h, fh := histogram.NewModelHistogram(m.GetHistogram())
// A native histogram is marked by at least one span.
if len(dtoH.GetNegativeSpan())+len(dtoH.GetPositiveSpan()) > 0 {
h, fh := histogram.NewModelHistogram(dtoH)
if h == nil {
// float histogram
hist.Buckets = histogram.BucketsAsJson[float64](histogram.GetAPIFloatBuckets(fh))
} else {
hist.Buckets = histogram.BucketsAsJson[uint64](histogram.GetAPIBuckets(h))
}
} else {
hist.Buckets = makeBuckets(m)
}
return hist
}
Expand Down

0 comments on commit db513a9

Please sign in to comment.