MCPcopy
hub / github.com/prometheus/client_golang / NewConstNativeHistogram

Function NewConstNativeHistogram

prometheus/histogram.go:1913–1965  ·  view source on GitHub ↗

NewConstNativeHistogram returns a metric representing a Prometheus native histogram with fixed values for the count, sum, and positive/negative/zero bucket counts. As those parameters cannot be changed, the returned value does not implement the Histogram interface (but only the Metric interface). Us

(
	desc *Desc,
	count uint64,
	sum float64,
	positiveBuckets, negativeBuckets map[int]int64,
	zeroBucket uint64,
	schema int32,
	zeroThreshold float64,
	createdTimestamp time.Time,
	labelValues ...string,
)

Source from the content-addressed store, hash-verified

1911//
1912// See https://opentelemetry.io/docs/specs/otel/compatibility/prometheus_and_openmetrics/#exponential-histograms for more details about the conversion from OTel to Prometheus.
1913func NewConstNativeHistogram(
1914 desc *Desc,
1915 count uint64,
1916 sum float64,
1917 positiveBuckets, negativeBuckets map[int]int64,
1918 zeroBucket uint64,
1919 schema int32,
1920 zeroThreshold float64,
1921 createdTimestamp time.Time,
1922 labelValues ...string,
1923) (Metric, error) {
1924 if desc.err != nil {
1925 return nil, desc.err
1926 }
1927 if err := validateLabelValues(labelValues, len(desc.variableLabels.names)); err != nil {
1928 return nil, err
1929 }
1930 if schema > nativeHistogramSchemaMaximum || schema < nativeHistogramSchemaMinimum {
1931 return nil, errors.New("invalid native histogram schema")
1932 }
1933 if err := validateCount(sum, count, negativeBuckets, positiveBuckets, zeroBucket); err != nil {
1934 return nil, err
1935 }
1936
1937 NegativeSpan, NegativeDelta := makeBucketsFromMap(negativeBuckets)
1938 PositiveSpan, PositiveDelta := makeBucketsFromMap(positiveBuckets)
1939 ret := &constNativeHistogram{
1940 desc: desc,
1941 Histogram: dto.Histogram{
1942 CreatedTimestamp: timestamppb.New(createdTimestamp),
1943 Schema: &schema,
1944 ZeroThreshold: &zeroThreshold,
1945 SampleCount: &count,
1946 SampleSum: &sum,
1947
1948 NegativeSpan: NegativeSpan,
1949 NegativeDelta: NegativeDelta,
1950
1951 PositiveSpan: PositiveSpan,
1952 PositiveDelta: PositiveDelta,
1953
1954 ZeroCount: proto.Uint64(zeroBucket),
1955 },
1956 labelPairs: MakeLabelPairs(desc, labelValues),
1957 }
1958 if *ret.ZeroThreshold == 0 && *ret.ZeroCount == 0 && len(ret.PositiveSpan) == 0 && len(ret.NegativeSpan) == 0 {
1959 ret.PositiveSpan = []*dto.BucketSpan{{
1960 Offset: proto.Int32(0),
1961 Length: proto.Uint32(0),
1962 }}
1963 }
1964 return ret, nil
1965}
1966
1967// MustNewConstNativeHistogram is a version of NewConstNativeHistogram that panics where
1968// NewConstNativeHistogram would have returned an error.

Callers 2

TestConstNativeHistogramFunction · 0.85

Calls 4

validateLabelValuesFunction · 0.85
validateCountFunction · 0.85
makeBucketsFromMapFunction · 0.85
MakeLabelPairsFunction · 0.85

Tested by 1

TestConstNativeHistogramFunction · 0.68