VictoriaMetrics/vendor/go.opentelemetry.io/collector/pdata/pmetric/metrics.go

96 lines
3.0 KiB
Go
Raw Normal View History

2023-09-07 12:34:14 +02:00
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric"
import (
"go.opentelemetry.io/collector/pdata/internal"
otlpcollectormetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/metrics/v1"
)
// Metrics is the top-level struct that is propagated through the metrics pipeline.
// Use NewMetrics to create new instance, zero-initialized instance is not valid for use.
type Metrics internal.Metrics
func newMetrics(orig *otlpcollectormetrics.ExportMetricsServiceRequest) Metrics {
2023-10-12 11:17:53 +02:00
state := internal.StateMutable
return Metrics(internal.NewMetrics(orig, &state))
2023-09-07 12:34:14 +02:00
}
func (ms Metrics) getOrig() *otlpcollectormetrics.ExportMetricsServiceRequest {
return internal.GetOrigMetrics(internal.Metrics(ms))
}
2023-10-31 20:19:51 +01:00
func (ms Metrics) getState() *internal.State {
return internal.GetMetricsState(internal.Metrics(ms))
}
2023-09-07 12:34:14 +02:00
// NewMetrics creates a new Metrics struct.
func NewMetrics() Metrics {
return newMetrics(&otlpcollectormetrics.ExportMetricsServiceRequest{})
}
2023-10-31 20:19:51 +01:00
// IsReadOnly returns true if this Metrics instance is read-only.
func (ms Metrics) IsReadOnly() bool {
return *ms.getState() == internal.StateReadOnly
}
2023-09-07 12:34:14 +02:00
// CopyTo copies the Metrics instance overriding the destination.
func (ms Metrics) CopyTo(dest Metrics) {
ms.ResourceMetrics().CopyTo(dest.ResourceMetrics())
}
// ResourceMetrics returns the ResourceMetricsSlice associated with this Metrics.
func (ms Metrics) ResourceMetrics() ResourceMetricsSlice {
2023-10-12 11:17:53 +02:00
return newResourceMetricsSlice(&ms.getOrig().ResourceMetrics, internal.GetMetricsState(internal.Metrics(ms)))
2023-09-07 12:34:14 +02:00
}
// MetricCount calculates the total number of metrics.
func (ms Metrics) MetricCount() int {
metricCount := 0
rms := ms.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
ilms := rm.ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
metricCount += ilm.Metrics().Len()
}
}
return metricCount
}
// DataPointCount calculates the total number of data points.
func (ms Metrics) DataPointCount() (dataPointCount int) {
rms := ms.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
ilms := rm.ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
ms := ilm.Metrics()
for k := 0; k < ms.Len(); k++ {
m := ms.At(k)
switch m.Type() {
case MetricTypeGauge:
dataPointCount += m.Gauge().DataPoints().Len()
case MetricTypeSum:
dataPointCount += m.Sum().DataPoints().Len()
case MetricTypeHistogram:
dataPointCount += m.Histogram().DataPoints().Len()
case MetricTypeExponentialHistogram:
dataPointCount += m.ExponentialHistogram().DataPoints().Len()
case MetricTypeSummary:
dataPointCount += m.Summary().DataPoints().Len()
}
}
}
}
return
}
2023-10-12 11:17:53 +02:00
// MarkReadOnly marks the Metrics as shared so that no further modifications can be done on it.
func (ms Metrics) MarkReadOnly() {
internal.SetMetricsState(internal.Metrics(ms), internal.StateReadOnly)
}