From 0a4d96693496cd46be4ed3137476522d1a7fdae3 Mon Sep 17 00:00:00 2001 From: Dmitrii Anoshin Date: Fri, 10 Dec 2021 15:19:40 -0800 Subject: [PATCH] [receiver/zookeeper] Migrate scraper to new experimental metrics builder (#6522) --- receiver/zookeeperreceiver/codegen.go | 2 +- receiver/zookeeperreceiver/config.go | 3 + receiver/zookeeperreceiver/factory.go | 3 + .../internal/metadata/generated_metrics.go | 304 ------- .../internal/metadata/generated_metrics_v2.go | 775 ++++++++++++++++++ receiver/zookeeperreceiver/metadata.yaml | 62 +- receiver/zookeeperreceiver/metrics.go | 36 +- receiver/zookeeperreceiver/scraper.go | 24 +- receiver/zookeeperreceiver/scraper_test.go | 106 ++- 9 files changed, 916 insertions(+), 399 deletions(-) delete mode 100644 receiver/zookeeperreceiver/internal/metadata/generated_metrics.go create mode 100644 receiver/zookeeperreceiver/internal/metadata/generated_metrics_v2.go diff --git a/receiver/zookeeperreceiver/codegen.go b/receiver/zookeeperreceiver/codegen.go index cf46ae3d95e4..5e7d3e394c44 100644 --- a/receiver/zookeeperreceiver/codegen.go +++ b/receiver/zookeeperreceiver/codegen.go @@ -15,6 +15,6 @@ //go:build !windows // +build !windows -//go:generate mdatagen metadata.yaml +//go:generate mdatagen --experimental-gen metadata.yaml package zookeeperreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver" diff --git a/receiver/zookeeperreceiver/config.go b/receiver/zookeeperreceiver/config.go index abe54d43c748..3d276ebae073 100644 --- a/receiver/zookeeperreceiver/config.go +++ b/receiver/zookeeperreceiver/config.go @@ -19,11 +19,14 @@ import ( "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/receiver/scraperhelper" + + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver/internal/metadata" ) type Config struct { scraperhelper.ScraperControllerSettings `mapstructure:",squash"` confignet.TCPAddr `mapstructure:",squash"` + Metrics metadata.MetricsSettings `mapstructure:"metrics"` // Timeout within which requests should be completed. Timeout time.Duration `mapstructure:"timeout"` diff --git a/receiver/zookeeperreceiver/factory.go b/receiver/zookeeperreceiver/factory.go index a9dc6b25128a..47cc2d9c65ce 100644 --- a/receiver/zookeeperreceiver/factory.go +++ b/receiver/zookeeperreceiver/factory.go @@ -24,6 +24,8 @@ import ( "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/receiver/receiverhelper" "go.opentelemetry.io/collector/receiver/scraperhelper" + + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver/internal/metadata" ) const ( @@ -51,6 +53,7 @@ func createDefaultConfig() config.Receiver { Endpoint: ":2181", }, Timeout: defaultTimeout, + Metrics: metadata.DefaultMetricsSettings(), } } diff --git a/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go b/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go deleted file mode 100644 index cfe4dc440fa2..000000000000 --- a/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go +++ /dev/null @@ -1,304 +0,0 @@ -// Copyright The OpenTelemetry 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. - -// Code generated by mdatagen. DO NOT EDIT. - -package metadata - -import ( - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/model/pdata" -) - -// Type is the component type name. -const Type config.Type = "zookeeperreceiver" - -// MetricIntf is an interface to generically interact with generated metric. -type MetricIntf interface { - Name() string - New() pdata.Metric - Init(metric pdata.Metric) -} - -// Intentionally not exposing this so that it is opaque and can change freely. -type metricImpl struct { - name string - initFunc func(pdata.Metric) -} - -// Name returns the metric name. -func (m *metricImpl) Name() string { - return m.name -} - -// New creates a metric object preinitialized. -func (m *metricImpl) New() pdata.Metric { - metric := pdata.NewMetric() - m.Init(metric) - return metric -} - -// Init initializes the provided metric object. -func (m *metricImpl) Init(metric pdata.Metric) { - m.initFunc(metric) -} - -type metricStruct struct { - ZookeeperApproximateDateSize MetricIntf - ZookeeperConnectionsAlive MetricIntf - ZookeeperEphemeralNodes MetricIntf - ZookeeperFollowers MetricIntf - ZookeeperFsyncThresholdExceeds MetricIntf - ZookeeperLatencyAvg MetricIntf - ZookeeperLatencyMax MetricIntf - ZookeeperLatencyMin MetricIntf - ZookeeperMaxFileDescriptors MetricIntf - ZookeeperOpenFileDescriptors MetricIntf - ZookeeperOutstandingRequests MetricIntf - ZookeeperPacketsReceived MetricIntf - ZookeeperPacketsSent MetricIntf - ZookeeperPendingSyncs MetricIntf - ZookeeperSyncedFollowers MetricIntf - ZookeeperWatches MetricIntf - ZookeeperZnodes MetricIntf -} - -// Names returns a list of all the metric name strings. -func (m *metricStruct) Names() []string { - return []string{ - "zookeeper.approximate_date_size", - "zookeeper.connections_alive", - "zookeeper.ephemeral_nodes", - "zookeeper.followers", - "zookeeper.fsync_threshold_exceeds", - "zookeeper.latency.avg", - "zookeeper.latency.max", - "zookeeper.latency.min", - "zookeeper.max_file_descriptors", - "zookeeper.open_file_descriptors", - "zookeeper.outstanding_requests", - "zookeeper.packets.received", - "zookeeper.packets.sent", - "zookeeper.pending_syncs", - "zookeeper.synced_followers", - "zookeeper.watches", - "zookeeper.znodes", - } -} - -var metricsByName = map[string]MetricIntf{ - "zookeeper.approximate_date_size": Metrics.ZookeeperApproximateDateSize, - "zookeeper.connections_alive": Metrics.ZookeeperConnectionsAlive, - "zookeeper.ephemeral_nodes": Metrics.ZookeeperEphemeralNodes, - "zookeeper.followers": Metrics.ZookeeperFollowers, - "zookeeper.fsync_threshold_exceeds": Metrics.ZookeeperFsyncThresholdExceeds, - "zookeeper.latency.avg": Metrics.ZookeeperLatencyAvg, - "zookeeper.latency.max": Metrics.ZookeeperLatencyMax, - "zookeeper.latency.min": Metrics.ZookeeperLatencyMin, - "zookeeper.max_file_descriptors": Metrics.ZookeeperMaxFileDescriptors, - "zookeeper.open_file_descriptors": Metrics.ZookeeperOpenFileDescriptors, - "zookeeper.outstanding_requests": Metrics.ZookeeperOutstandingRequests, - "zookeeper.packets.received": Metrics.ZookeeperPacketsReceived, - "zookeeper.packets.sent": Metrics.ZookeeperPacketsSent, - "zookeeper.pending_syncs": Metrics.ZookeeperPendingSyncs, - "zookeeper.synced_followers": Metrics.ZookeeperSyncedFollowers, - "zookeeper.watches": Metrics.ZookeeperWatches, - "zookeeper.znodes": Metrics.ZookeeperZnodes, -} - -func (m *metricStruct) ByName(n string) MetricIntf { - return metricsByName[n] -} - -// Metrics contains a set of methods for each metric that help with -// manipulating those metrics. -var Metrics = &metricStruct{ - &metricImpl{ - "zookeeper.approximate_date_size", - func(metric pdata.Metric) { - metric.SetName("zookeeper.approximate_date_size") - metric.SetDescription("Size of data in bytes that a ZooKeeper server has in its data tree.") - metric.SetUnit("By") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.connections_alive", - func(metric pdata.Metric) { - metric.SetName("zookeeper.connections_alive") - metric.SetDescription("Number of active clients connected to a ZooKeeper server.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.ephemeral_nodes", - func(metric pdata.Metric) { - metric.SetName("zookeeper.ephemeral_nodes") - metric.SetDescription("Number of ephemeral nodes that a ZooKeeper server has in its data tree.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.followers", - func(metric pdata.Metric) { - metric.SetName("zookeeper.followers") - metric.SetDescription("The number of followers in sync with the leader. Only exposed by the leader.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.fsync_threshold_exceeds", - func(metric pdata.Metric) { - metric.SetName("zookeeper.fsync_threshold_exceeds") - metric.SetDescription("Number of times fsync duration has exceeded warning threshold.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeSum) - metric.Sum().SetIsMonotonic(true) - metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) - }, - }, - &metricImpl{ - "zookeeper.latency.avg", - func(metric pdata.Metric) { - metric.SetName("zookeeper.latency.avg") - metric.SetDescription("Average time in milliseconds for requests to be processed.") - metric.SetUnit("ms") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.latency.max", - func(metric pdata.Metric) { - metric.SetName("zookeeper.latency.max") - metric.SetDescription("Maximum time in milliseconds for requests to be processed.") - metric.SetUnit("ms") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.latency.min", - func(metric pdata.Metric) { - metric.SetName("zookeeper.latency.min") - metric.SetDescription("Minimum time in milliseconds for requests to be processed.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.max_file_descriptors", - func(metric pdata.Metric) { - metric.SetName("zookeeper.max_file_descriptors") - metric.SetDescription("Maximum number of file descriptors that a ZooKeeper server can open.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.open_file_descriptors", - func(metric pdata.Metric) { - metric.SetName("zookeeper.open_file_descriptors") - metric.SetDescription("Number of file descriptors that a ZooKeeper server has open.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.outstanding_requests", - func(metric pdata.Metric) { - metric.SetName("zookeeper.outstanding_requests") - metric.SetDescription("Number of currently executing requests.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.packets.received", - func(metric pdata.Metric) { - metric.SetName("zookeeper.packets.received") - metric.SetDescription("Number of ZooKeeper packets received by a server.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeSum) - metric.Sum().SetIsMonotonic(true) - metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) - }, - }, - &metricImpl{ - "zookeeper.packets.sent", - func(metric pdata.Metric) { - metric.SetName("zookeeper.packets.sent") - metric.SetDescription("Number of ZooKeeper packets sent by a server.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeSum) - metric.Sum().SetIsMonotonic(true) - metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) - }, - }, - &metricImpl{ - "zookeeper.pending_syncs", - func(metric pdata.Metric) { - metric.SetName("zookeeper.pending_syncs") - metric.SetDescription("The number of pending syncs from the followers. Only exposed by the leader.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.synced_followers", - func(metric pdata.Metric) { - metric.SetName("zookeeper.synced_followers") - metric.SetDescription("The number of followers in sync with the leader. Only exposed by the leader.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.watches", - func(metric pdata.Metric) { - metric.SetName("zookeeper.watches") - metric.SetDescription("Number of watches placed on Z-Nodes on a ZooKeeper server.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, - &metricImpl{ - "zookeeper.znodes", - func(metric pdata.Metric) { - metric.SetName("zookeeper.znodes") - metric.SetDescription("Number of z-nodes that a ZooKeeper server has in its data tree.") - metric.SetUnit("1") - metric.SetDataType(pdata.MetricDataTypeGauge) - }, - }, -} - -// M contains a set of methods for each metric that help with -// manipulating those metrics. M is an alias for Metrics -var M = Metrics - -// Attributes contains the possible metric attributes that can be used. -var Attributes = struct { - // ServerState (State of the Zookeeper server (leader, standalone or follower).) - ServerState string - // ZkVersion (Zookeeper version of the instance.) - ZkVersion string -}{ - "server.state", - "zk.version", -} - -// A is an alias for Attributes. -var A = Attributes diff --git a/receiver/zookeeperreceiver/internal/metadata/generated_metrics_v2.go b/receiver/zookeeperreceiver/internal/metadata/generated_metrics_v2.go new file mode 100644 index 000000000000..0d1f3f95a3a9 --- /dev/null +++ b/receiver/zookeeperreceiver/internal/metadata/generated_metrics_v2.go @@ -0,0 +1,775 @@ +// Copyright The OpenTelemetry 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. + +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "time" + + "go.opentelemetry.io/collector/model/pdata" +) + +// MetricSettings provides common settings for a particular metric. +type MetricSettings struct { + Enabled bool `mapstructure:"enabled"` +} + +// MetricsSettings provides settings for zookeeperreceiver metrics. +type MetricsSettings struct { + ZookeeperApproximateDateSize MetricSettings `mapstructure:"zookeeper.approximate_date_size"` + ZookeeperConnectionsAlive MetricSettings `mapstructure:"zookeeper.connections_alive"` + ZookeeperEphemeralNodes MetricSettings `mapstructure:"zookeeper.ephemeral_nodes"` + ZookeeperFollowers MetricSettings `mapstructure:"zookeeper.followers"` + ZookeeperFsyncThresholdExceeds MetricSettings `mapstructure:"zookeeper.fsync_threshold_exceeds"` + ZookeeperLatencyAvg MetricSettings `mapstructure:"zookeeper.latency.avg"` + ZookeeperLatencyMax MetricSettings `mapstructure:"zookeeper.latency.max"` + ZookeeperLatencyMin MetricSettings `mapstructure:"zookeeper.latency.min"` + ZookeeperMaxFileDescriptors MetricSettings `mapstructure:"zookeeper.max_file_descriptors"` + ZookeeperOpenFileDescriptors MetricSettings `mapstructure:"zookeeper.open_file_descriptors"` + ZookeeperOutstandingRequests MetricSettings `mapstructure:"zookeeper.outstanding_requests"` + ZookeeperPacketsReceived MetricSettings `mapstructure:"zookeeper.packets.received"` + ZookeeperPacketsSent MetricSettings `mapstructure:"zookeeper.packets.sent"` + ZookeeperPendingSyncs MetricSettings `mapstructure:"zookeeper.pending_syncs"` + ZookeeperSyncedFollowers MetricSettings `mapstructure:"zookeeper.synced_followers"` + ZookeeperWatches MetricSettings `mapstructure:"zookeeper.watches"` + ZookeeperZnodes MetricSettings `mapstructure:"zookeeper.znodes"` +} + +func DefaultMetricsSettings() MetricsSettings { + return MetricsSettings{ + ZookeeperApproximateDateSize: MetricSettings{ + Enabled: true, + }, + ZookeeperConnectionsAlive: MetricSettings{ + Enabled: true, + }, + ZookeeperEphemeralNodes: MetricSettings{ + Enabled: true, + }, + ZookeeperFollowers: MetricSettings{ + Enabled: true, + }, + ZookeeperFsyncThresholdExceeds: MetricSettings{ + Enabled: true, + }, + ZookeeperLatencyAvg: MetricSettings{ + Enabled: true, + }, + ZookeeperLatencyMax: MetricSettings{ + Enabled: true, + }, + ZookeeperLatencyMin: MetricSettings{ + Enabled: true, + }, + ZookeeperMaxFileDescriptors: MetricSettings{ + Enabled: true, + }, + ZookeeperOpenFileDescriptors: MetricSettings{ + Enabled: true, + }, + ZookeeperOutstandingRequests: MetricSettings{ + Enabled: true, + }, + ZookeeperPacketsReceived: MetricSettings{ + Enabled: true, + }, + ZookeeperPacketsSent: MetricSettings{ + Enabled: true, + }, + ZookeeperPendingSyncs: MetricSettings{ + Enabled: true, + }, + ZookeeperSyncedFollowers: MetricSettings{ + Enabled: true, + }, + ZookeeperWatches: MetricSettings{ + Enabled: true, + }, + ZookeeperZnodes: MetricSettings{ + Enabled: true, + }, + } +} + +type metrics struct { + ZookeeperApproximateDateSize pdata.Metric + ZookeeperConnectionsAlive pdata.Metric + ZookeeperEphemeralNodes pdata.Metric + ZookeeperFollowers pdata.Metric + ZookeeperFsyncThresholdExceeds pdata.Metric + ZookeeperLatencyAvg pdata.Metric + ZookeeperLatencyMax pdata.Metric + ZookeeperLatencyMin pdata.Metric + ZookeeperMaxFileDescriptors pdata.Metric + ZookeeperOpenFileDescriptors pdata.Metric + ZookeeperOutstandingRequests pdata.Metric + ZookeeperPacketsReceived pdata.Metric + ZookeeperPacketsSent pdata.Metric + ZookeeperPendingSyncs pdata.Metric + ZookeeperSyncedFollowers pdata.Metric + ZookeeperWatches pdata.Metric + ZookeeperZnodes pdata.Metric +} + +func newMetrics(config MetricsSettings) metrics { + ms := metrics{} + if config.ZookeeperApproximateDateSize.Enabled { + ms.ZookeeperApproximateDateSize = pdata.NewMetric() + } + if config.ZookeeperConnectionsAlive.Enabled { + ms.ZookeeperConnectionsAlive = pdata.NewMetric() + } + if config.ZookeeperEphemeralNodes.Enabled { + ms.ZookeeperEphemeralNodes = pdata.NewMetric() + } + if config.ZookeeperFollowers.Enabled { + ms.ZookeeperFollowers = pdata.NewMetric() + } + if config.ZookeeperFsyncThresholdExceeds.Enabled { + ms.ZookeeperFsyncThresholdExceeds = pdata.NewMetric() + } + if config.ZookeeperLatencyAvg.Enabled { + ms.ZookeeperLatencyAvg = pdata.NewMetric() + } + if config.ZookeeperLatencyMax.Enabled { + ms.ZookeeperLatencyMax = pdata.NewMetric() + } + if config.ZookeeperLatencyMin.Enabled { + ms.ZookeeperLatencyMin = pdata.NewMetric() + } + if config.ZookeeperMaxFileDescriptors.Enabled { + ms.ZookeeperMaxFileDescriptors = pdata.NewMetric() + } + if config.ZookeeperOpenFileDescriptors.Enabled { + ms.ZookeeperOpenFileDescriptors = pdata.NewMetric() + } + if config.ZookeeperOutstandingRequests.Enabled { + ms.ZookeeperOutstandingRequests = pdata.NewMetric() + } + if config.ZookeeperPacketsReceived.Enabled { + ms.ZookeeperPacketsReceived = pdata.NewMetric() + } + if config.ZookeeperPacketsSent.Enabled { + ms.ZookeeperPacketsSent = pdata.NewMetric() + } + if config.ZookeeperPendingSyncs.Enabled { + ms.ZookeeperPendingSyncs = pdata.NewMetric() + } + if config.ZookeeperSyncedFollowers.Enabled { + ms.ZookeeperSyncedFollowers = pdata.NewMetric() + } + if config.ZookeeperWatches.Enabled { + ms.ZookeeperWatches = pdata.NewMetric() + } + if config.ZookeeperZnodes.Enabled { + ms.ZookeeperZnodes = pdata.NewMetric() + } + return ms +} + +// MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations +// required to produce metric representation defined in metadata and user configuration. +type MetricsBuilder struct { + config MetricsSettings + startTime pdata.Timestamp + attributeServerStateCapacity int + attributeZkVersionCapacity int + metrics metrics +} + +// metricBuilderOption applies changes to default metrics builder. +type metricBuilderOption func(*MetricsBuilder) + +// WithStartTime sets startTime on the metrics builder. +func WithStartTime(startTime pdata.Timestamp) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.startTime = startTime + } +} + +// WithAttributeServerStateCapacity sets an expected number of values of server.state attribute that will be +// used to calculate data points capacity for each metric report. +func WithAttributeServerStateCapacity(cap int) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.attributeServerStateCapacity = cap + } +} + +// WithAttributeZkVersionCapacity sets an expected number of values of zk.version attribute that will be +// used to calculate data points capacity for each metric report. +func WithAttributeZkVersionCapacity(cap int) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.attributeZkVersionCapacity = cap + } +} + +func NewMetricsBuilder(config MetricsSettings, options ...metricBuilderOption) *MetricsBuilder { + mb := &MetricsBuilder{ + config: config, + startTime: pdata.NewTimestampFromTime(time.Now()), + metrics: newMetrics(config), + } + + for _, op := range options { + op(mb) + } + + mb.initMetrics() + return mb +} + +// Emit appends generated metrics to a pdata.MetricsSlice and updates the internal state to be ready for recording +// another set of data points. This function will be doing all transformations required to produce metric representation +// defined in metadata and user configuration, e.g. delta/cumulative translation. +func (mb *MetricsBuilder) Emit(metrics pdata.MetricSlice) { + if mb.config.ZookeeperApproximateDateSize.Enabled && mb.metrics.ZookeeperApproximateDateSize.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperApproximateDateSize.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperConnectionsAlive.Enabled && mb.metrics.ZookeeperConnectionsAlive.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperConnectionsAlive.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperEphemeralNodes.Enabled && mb.metrics.ZookeeperEphemeralNodes.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperEphemeralNodes.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperFollowers.Enabled && mb.metrics.ZookeeperFollowers.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperFollowers.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperFsyncThresholdExceeds.Enabled && mb.metrics.ZookeeperFsyncThresholdExceeds.Sum().DataPoints().Len() > 0 { + mb.metrics.ZookeeperFsyncThresholdExceeds.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperLatencyAvg.Enabled && mb.metrics.ZookeeperLatencyAvg.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperLatencyAvg.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperLatencyMax.Enabled && mb.metrics.ZookeeperLatencyMax.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperLatencyMax.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperLatencyMin.Enabled && mb.metrics.ZookeeperLatencyMin.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperLatencyMin.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperMaxFileDescriptors.Enabled && mb.metrics.ZookeeperMaxFileDescriptors.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperMaxFileDescriptors.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperOpenFileDescriptors.Enabled && mb.metrics.ZookeeperOpenFileDescriptors.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperOpenFileDescriptors.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperOutstandingRequests.Enabled && mb.metrics.ZookeeperOutstandingRequests.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperOutstandingRequests.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperPacketsReceived.Enabled && mb.metrics.ZookeeperPacketsReceived.Sum().DataPoints().Len() > 0 { + mb.metrics.ZookeeperPacketsReceived.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperPacketsSent.Enabled && mb.metrics.ZookeeperPacketsSent.Sum().DataPoints().Len() > 0 { + mb.metrics.ZookeeperPacketsSent.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperPendingSyncs.Enabled && mb.metrics.ZookeeperPendingSyncs.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperPendingSyncs.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperSyncedFollowers.Enabled && mb.metrics.ZookeeperSyncedFollowers.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperSyncedFollowers.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperWatches.Enabled && mb.metrics.ZookeeperWatches.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperWatches.MoveTo(metrics.AppendEmpty()) + } + if mb.config.ZookeeperZnodes.Enabled && mb.metrics.ZookeeperZnodes.Gauge().DataPoints().Len() > 0 { + mb.metrics.ZookeeperZnodes.MoveTo(metrics.AppendEmpty()) + } + + // Reset metric data points collection. + mb.initMetrics() +} + +// initZookeeperApproximateDateSizeMetric builds new zookeeper.approximate_date_size metric. +func (mb *MetricsBuilder) initZookeeperApproximateDateSizeMetric() { + metric := mb.metrics.ZookeeperApproximateDateSize + metric.SetName("zookeeper.approximate_date_size") + metric.SetDescription("Size of data in bytes that a ZooKeeper server has in its data tree.") + metric.SetUnit("By") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperConnectionsAliveMetric builds new zookeeper.connections_alive metric. +func (mb *MetricsBuilder) initZookeeperConnectionsAliveMetric() { + metric := mb.metrics.ZookeeperConnectionsAlive + metric.SetName("zookeeper.connections_alive") + metric.SetDescription("Number of active clients connected to a ZooKeeper server.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperEphemeralNodesMetric builds new zookeeper.ephemeral_nodes metric. +func (mb *MetricsBuilder) initZookeeperEphemeralNodesMetric() { + metric := mb.metrics.ZookeeperEphemeralNodes + metric.SetName("zookeeper.ephemeral_nodes") + metric.SetDescription("Number of ephemeral nodes that a ZooKeeper server has in its data tree.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperFollowersMetric builds new zookeeper.followers metric. +func (mb *MetricsBuilder) initZookeeperFollowersMetric() { + metric := mb.metrics.ZookeeperFollowers + metric.SetName("zookeeper.followers") + metric.SetDescription("The number of followers in sync with the leader. Only exposed by the leader.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperFsyncThresholdExceedsMetric builds new zookeeper.fsync_threshold_exceeds metric. +func (mb *MetricsBuilder) initZookeeperFsyncThresholdExceedsMetric() { + metric := mb.metrics.ZookeeperFsyncThresholdExceeds + metric.SetName("zookeeper.fsync_threshold_exceeds") + metric.SetDescription("Number of times fsync duration has exceeded warning threshold.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeSum) + metric.Sum().SetIsMonotonic(true) + metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) +} + +// initZookeeperLatencyAvgMetric builds new zookeeper.latency.avg metric. +func (mb *MetricsBuilder) initZookeeperLatencyAvgMetric() { + metric := mb.metrics.ZookeeperLatencyAvg + metric.SetName("zookeeper.latency.avg") + metric.SetDescription("Average time in milliseconds for requests to be processed.") + metric.SetUnit("ms") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperLatencyMaxMetric builds new zookeeper.latency.max metric. +func (mb *MetricsBuilder) initZookeeperLatencyMaxMetric() { + metric := mb.metrics.ZookeeperLatencyMax + metric.SetName("zookeeper.latency.max") + metric.SetDescription("Maximum time in milliseconds for requests to be processed.") + metric.SetUnit("ms") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperLatencyMinMetric builds new zookeeper.latency.min metric. +func (mb *MetricsBuilder) initZookeeperLatencyMinMetric() { + metric := mb.metrics.ZookeeperLatencyMin + metric.SetName("zookeeper.latency.min") + metric.SetDescription("Minimum time in milliseconds for requests to be processed.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperMaxFileDescriptorsMetric builds new zookeeper.max_file_descriptors metric. +func (mb *MetricsBuilder) initZookeeperMaxFileDescriptorsMetric() { + metric := mb.metrics.ZookeeperMaxFileDescriptors + metric.SetName("zookeeper.max_file_descriptors") + metric.SetDescription("Maximum number of file descriptors that a ZooKeeper server can open.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperOpenFileDescriptorsMetric builds new zookeeper.open_file_descriptors metric. +func (mb *MetricsBuilder) initZookeeperOpenFileDescriptorsMetric() { + metric := mb.metrics.ZookeeperOpenFileDescriptors + metric.SetName("zookeeper.open_file_descriptors") + metric.SetDescription("Number of file descriptors that a ZooKeeper server has open.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperOutstandingRequestsMetric builds new zookeeper.outstanding_requests metric. +func (mb *MetricsBuilder) initZookeeperOutstandingRequestsMetric() { + metric := mb.metrics.ZookeeperOutstandingRequests + metric.SetName("zookeeper.outstanding_requests") + metric.SetDescription("Number of currently executing requests.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperPacketsReceivedMetric builds new zookeeper.packets.received metric. +func (mb *MetricsBuilder) initZookeeperPacketsReceivedMetric() { + metric := mb.metrics.ZookeeperPacketsReceived + metric.SetName("zookeeper.packets.received") + metric.SetDescription("Number of ZooKeeper packets received by a server.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeSum) + metric.Sum().SetIsMonotonic(true) + metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) +} + +// initZookeeperPacketsSentMetric builds new zookeeper.packets.sent metric. +func (mb *MetricsBuilder) initZookeeperPacketsSentMetric() { + metric := mb.metrics.ZookeeperPacketsSent + metric.SetName("zookeeper.packets.sent") + metric.SetDescription("Number of ZooKeeper packets sent by a server.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeSum) + metric.Sum().SetIsMonotonic(true) + metric.Sum().SetAggregationTemporality(pdata.MetricAggregationTemporalityCumulative) +} + +// initZookeeperPendingSyncsMetric builds new zookeeper.pending_syncs metric. +func (mb *MetricsBuilder) initZookeeperPendingSyncsMetric() { + metric := mb.metrics.ZookeeperPendingSyncs + metric.SetName("zookeeper.pending_syncs") + metric.SetDescription("The number of pending syncs from the followers. Only exposed by the leader.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperSyncedFollowersMetric builds new zookeeper.synced_followers metric. +func (mb *MetricsBuilder) initZookeeperSyncedFollowersMetric() { + metric := mb.metrics.ZookeeperSyncedFollowers + metric.SetName("zookeeper.synced_followers") + metric.SetDescription("The number of followers in sync with the leader. Only exposed by the leader.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperWatchesMetric builds new zookeeper.watches metric. +func (mb *MetricsBuilder) initZookeeperWatchesMetric() { + metric := mb.metrics.ZookeeperWatches + metric.SetName("zookeeper.watches") + metric.SetDescription("Number of watches placed on Z-Nodes on a ZooKeeper server.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initZookeeperZnodesMetric builds new zookeeper.znodes metric. +func (mb *MetricsBuilder) initZookeeperZnodesMetric() { + metric := mb.metrics.ZookeeperZnodes + metric.SetName("zookeeper.znodes") + metric.SetDescription("Number of z-nodes that a ZooKeeper server has in its data tree.") + metric.SetUnit("1") + metric.SetDataType(pdata.MetricDataTypeGauge) +} + +// initMetrics initializes metrics. +func (mb *MetricsBuilder) initMetrics() { + if mb.config.ZookeeperApproximateDateSize.Enabled { + // TODO: Use mb.metrics.ZookeeperApproximateDateSize.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperApproximateDateSizeMetric() + } + if mb.config.ZookeeperConnectionsAlive.Enabled { + // TODO: Use mb.metrics.ZookeeperConnectionsAlive.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperConnectionsAliveMetric() + } + if mb.config.ZookeeperEphemeralNodes.Enabled { + // TODO: Use mb.metrics.ZookeeperEphemeralNodes.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperEphemeralNodesMetric() + } + if mb.config.ZookeeperFollowers.Enabled { + // TODO: Use mb.metrics.ZookeeperFollowers.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperFollowersMetric() + } + if mb.config.ZookeeperFsyncThresholdExceeds.Enabled { + // TODO: Use mb.metrics.ZookeeperFsyncThresholdExceeds.Sum().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperFsyncThresholdExceedsMetric() + } + if mb.config.ZookeeperLatencyAvg.Enabled { + // TODO: Use mb.metrics.ZookeeperLatencyAvg.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperLatencyAvgMetric() + } + if mb.config.ZookeeperLatencyMax.Enabled { + // TODO: Use mb.metrics.ZookeeperLatencyMax.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperLatencyMaxMetric() + } + if mb.config.ZookeeperLatencyMin.Enabled { + // TODO: Use mb.metrics.ZookeeperLatencyMin.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperLatencyMinMetric() + } + if mb.config.ZookeeperMaxFileDescriptors.Enabled { + // TODO: Use mb.metrics.ZookeeperMaxFileDescriptors.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperMaxFileDescriptorsMetric() + } + if mb.config.ZookeeperOpenFileDescriptors.Enabled { + // TODO: Use mb.metrics.ZookeeperOpenFileDescriptors.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperOpenFileDescriptorsMetric() + } + if mb.config.ZookeeperOutstandingRequests.Enabled { + // TODO: Use mb.metrics.ZookeeperOutstandingRequests.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperOutstandingRequestsMetric() + } + if mb.config.ZookeeperPacketsReceived.Enabled { + // TODO: Use mb.metrics.ZookeeperPacketsReceived.Sum().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperPacketsReceivedMetric() + } + if mb.config.ZookeeperPacketsSent.Enabled { + // TODO: Use mb.metrics.ZookeeperPacketsSent.Sum().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperPacketsSentMetric() + } + if mb.config.ZookeeperPendingSyncs.Enabled { + // TODO: Use mb.metrics.ZookeeperPendingSyncs.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperPendingSyncsMetric() + } + if mb.config.ZookeeperSyncedFollowers.Enabled { + // TODO: Use mb.metrics.ZookeeperSyncedFollowers.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperSyncedFollowersMetric() + } + if mb.config.ZookeeperWatches.Enabled { + // TODO: Use mb.metrics.ZookeeperWatches.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperWatchesMetric() + } + if mb.config.ZookeeperZnodes.Enabled { + // TODO: Use mb.metrics.ZookeeperZnodes.Gauge().DataPoints().Clear() instead of rebuilding + // the metrics once the Clear method is available. + mb.initZookeeperZnodesMetric() + } +} + +// RecordZookeeperApproximateDateSizeDataPoint adds a data point to zookeeper.approximate_date_size metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperApproximateDateSizeDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperApproximateDateSize.Enabled { + return + } + + dp := mb.metrics.ZookeeperApproximateDateSize.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperConnectionsAliveDataPoint adds a data point to zookeeper.connections_alive metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperConnectionsAliveDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperConnectionsAlive.Enabled { + return + } + + dp := mb.metrics.ZookeeperConnectionsAlive.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperEphemeralNodesDataPoint adds a data point to zookeeper.ephemeral_nodes metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperEphemeralNodesDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperEphemeralNodes.Enabled { + return + } + + dp := mb.metrics.ZookeeperEphemeralNodes.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperFollowersDataPoint adds a data point to zookeeper.followers metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperFollowersDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperFollowers.Enabled { + return + } + + dp := mb.metrics.ZookeeperFollowers.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperFsyncThresholdExceedsDataPoint adds a data point to zookeeper.fsync_threshold_exceeds metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperFsyncThresholdExceedsDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperFsyncThresholdExceeds.Enabled { + return + } + + dp := mb.metrics.ZookeeperFsyncThresholdExceeds.Sum().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperLatencyAvgDataPoint adds a data point to zookeeper.latency.avg metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperLatencyAvgDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperLatencyAvg.Enabled { + return + } + + dp := mb.metrics.ZookeeperLatencyAvg.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperLatencyMaxDataPoint adds a data point to zookeeper.latency.max metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperLatencyMaxDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperLatencyMax.Enabled { + return + } + + dp := mb.metrics.ZookeeperLatencyMax.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperLatencyMinDataPoint adds a data point to zookeeper.latency.min metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperLatencyMinDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperLatencyMin.Enabled { + return + } + + dp := mb.metrics.ZookeeperLatencyMin.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperMaxFileDescriptorsDataPoint adds a data point to zookeeper.max_file_descriptors metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperMaxFileDescriptorsDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperMaxFileDescriptors.Enabled { + return + } + + dp := mb.metrics.ZookeeperMaxFileDescriptors.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperOpenFileDescriptorsDataPoint adds a data point to zookeeper.open_file_descriptors metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperOpenFileDescriptorsDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperOpenFileDescriptors.Enabled { + return + } + + dp := mb.metrics.ZookeeperOpenFileDescriptors.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperOutstandingRequestsDataPoint adds a data point to zookeeper.outstanding_requests metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperOutstandingRequestsDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperOutstandingRequests.Enabled { + return + } + + dp := mb.metrics.ZookeeperOutstandingRequests.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperPacketsReceivedDataPoint adds a data point to zookeeper.packets.received metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperPacketsReceivedDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperPacketsReceived.Enabled { + return + } + + dp := mb.metrics.ZookeeperPacketsReceived.Sum().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperPacketsSentDataPoint adds a data point to zookeeper.packets.sent metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperPacketsSentDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperPacketsSent.Enabled { + return + } + + dp := mb.metrics.ZookeeperPacketsSent.Sum().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperPendingSyncsDataPoint adds a data point to zookeeper.pending_syncs metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperPendingSyncsDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperPendingSyncs.Enabled { + return + } + + dp := mb.metrics.ZookeeperPendingSyncs.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperSyncedFollowersDataPoint adds a data point to zookeeper.synced_followers metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperSyncedFollowersDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperSyncedFollowers.Enabled { + return + } + + dp := mb.metrics.ZookeeperSyncedFollowers.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperWatchesDataPoint adds a data point to zookeeper.watches metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperWatchesDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperWatches.Enabled { + return + } + + dp := mb.metrics.ZookeeperWatches.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// RecordZookeeperZnodesDataPoint adds a data point to zookeeper.znodes metric. +// Any attribute of AttributeValueTypeEmpty type will be skipped. +func (mb *MetricsBuilder) RecordZookeeperZnodesDataPoint(ts pdata.Timestamp, val int64) { + if !mb.config.ZookeeperZnodes.Enabled { + return + } + + dp := mb.metrics.ZookeeperZnodes.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(mb.startTime) + dp.SetTimestamp(ts) + dp.SetIntVal(val) +} + +// Attributes contains the possible metric attributes that can be used. +var Attributes = struct { + // ServerState (State of the Zookeeper server (leader, standalone or follower).) + ServerState string + // ZkVersion (Zookeeper version of the instance.) + ZkVersion string +}{ + "server.state", + "zk.version", +} + +// A is an alias for Attributes. +var A = Attributes diff --git a/receiver/zookeeperreceiver/metadata.yaml b/receiver/zookeeperreceiver/metadata.yaml index a6c7b84e7177..6aad17dbfe4a 100644 --- a/receiver/zookeeperreceiver/metadata.yaml +++ b/receiver/zookeeperreceiver/metadata.yaml @@ -8,76 +8,110 @@ attributes: metrics: zookeeper.followers: + enabled: true description: The number of followers in sync with the leader. Only exposed by the leader. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.synced_followers: + enabled: true description: The number of followers in sync with the leader. Only exposed by the leader. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.pending_syncs: + enabled: true description: The number of pending syncs from the followers. Only exposed by the leader. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.latency.avg: + enabled: true description: Average time in milliseconds for requests to be processed. unit: ms - gauge: {} + gauge: + number_type: int zookeeper.latency.max: + enabled: true description: Maximum time in milliseconds for requests to be processed. unit: ms - gauge: {} + gauge: + number_type: int zookeeper.latency.min: + enabled: true description: Minimum time in milliseconds for requests to be processed. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.connections_alive: + enabled: true description: Number of active clients connected to a ZooKeeper server. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.outstanding_requests: + enabled: true description: Number of currently executing requests. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.znodes: + enabled: true description: Number of z-nodes that a ZooKeeper server has in its data tree. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.watches: + enabled: true description: Number of watches placed on Z-Nodes on a ZooKeeper server. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.ephemeral_nodes: + enabled: true description: Number of ephemeral nodes that a ZooKeeper server has in its data tree. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.approximate_date_size: + enabled: true description: Size of data in bytes that a ZooKeeper server has in its data tree. unit: By - gauge: {} + gauge: + number_type: int zookeeper.open_file_descriptors: + enabled: true description: Number of file descriptors that a ZooKeeper server has open. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.max_file_descriptors: + enabled: true description: Maximum number of file descriptors that a ZooKeeper server can open. unit: 1 - gauge: {} + gauge: + number_type: int zookeeper.packets.received: + enabled: true description: Number of ZooKeeper packets received by a server. unit: 1 sum: + number_type: int monotonic: true aggregation: cumulative zookeeper.packets.sent: + enabled: true description: Number of ZooKeeper packets sent by a server. unit: 1 sum: + number_type: int monotonic: true aggregation: cumulative zookeeper.fsync_threshold_exceeds: + enabled: true description: Number of times fsync duration has exceeded warning threshold. unit: 1 sum: + number_type: int monotonic: true aggregation: cumulative diff --git a/receiver/zookeeperreceiver/metrics.go b/receiver/zookeeperreceiver/metrics.go index 2a0ab6bb809f..77f8cbbef816 100644 --- a/receiver/zookeeperreceiver/metrics.go +++ b/receiver/zookeeperreceiver/metrics.go @@ -46,42 +46,42 @@ const ( zkVersionKey = "zk_version" ) -func getOTLPInitFunc(metric string) func(metric pdata.Metric) { +func recordDataPointsFunc(mb *metadata.MetricsBuilder, metric string) func(ts pdata.Timestamp, val int64) { switch metric { case followersMetricKey: - return metadata.Metrics.ZookeeperFollowers.Init + return mb.RecordZookeeperFollowersDataPoint case syncedFollowersMetricKey: - return metadata.Metrics.ZookeeperSyncedFollowers.Init + return mb.RecordZookeeperSyncedFollowersDataPoint case pendingSyncsMetricKey: - return metadata.Metrics.ZookeeperPendingSyncs.Init + return mb.RecordZookeeperPendingSyncsDataPoint case avgLatencyMetricKey: - return metadata.Metrics.ZookeeperLatencyAvg.Init + return mb.RecordZookeeperLatencyAvgDataPoint case maxLatencyMetricKey: - return metadata.Metrics.ZookeeperLatencyMax.Init + return mb.RecordZookeeperLatencyMaxDataPoint case minLatencyMetricKey: - return metadata.Metrics.ZookeeperLatencyMin.Init + return mb.RecordZookeeperLatencyMinDataPoint case numAliveConnectionsMetricKey: - return metadata.Metrics.ZookeeperConnectionsAlive.Init + return mb.RecordZookeeperConnectionsAliveDataPoint case outstandingRequestsMetricKey: - return metadata.Metrics.ZookeeperOutstandingRequests.Init + return mb.RecordZookeeperOutstandingRequestsDataPoint case zNodeCountMetricKey: - return metadata.Metrics.ZookeeperZnodes.Init + return mb.RecordZookeeperZnodesDataPoint case watchCountMetricKey: - return metadata.Metrics.ZookeeperWatches.Init + return mb.RecordZookeeperWatchesDataPoint case ephemeralsCountMetricKey: - return metadata.Metrics.ZookeeperEphemeralNodes.Init + return mb.RecordZookeeperEphemeralNodesDataPoint case approximateDataSizeMetricKey: - return metadata.Metrics.ZookeeperApproximateDateSize.Init + return mb.RecordZookeeperApproximateDateSizeDataPoint case openFileDescriptorCountMetricKey: - return metadata.Metrics.ZookeeperOpenFileDescriptors.Init + return mb.RecordZookeeperOpenFileDescriptorsDataPoint case maxFileDescriptorCountMetricKey: - return metadata.Metrics.ZookeeperMaxFileDescriptors.Init + return mb.RecordZookeeperMaxFileDescriptorsDataPoint case fSyncThresholdExceedCountMetricKey: - return metadata.Metrics.ZookeeperFsyncThresholdExceeds.Init + return mb.RecordZookeeperFsyncThresholdExceedsDataPoint case packetsReceivedMetricKey: - return metadata.Metrics.ZookeeperPacketsReceived.Init + return mb.RecordZookeeperPacketsReceivedDataPoint case packetsSentMetricKey: - return metadata.Metrics.ZookeeperPacketsSent.Init + return mb.RecordZookeeperPacketsSentDataPoint } return nil diff --git a/receiver/zookeeperreceiver/scraper.go b/receiver/zookeeperreceiver/scraper.go index 72eafefc3e75..387b8fdeac71 100644 --- a/receiver/zookeeperreceiver/scraper.go +++ b/receiver/zookeeperreceiver/scraper.go @@ -40,6 +40,7 @@ type zookeeperMetricsScraper struct { logger *zap.Logger config *Config cancel context.CancelFunc + mb *metadata.MetricsBuilder // For mocking. closeConnection func(net.Conn) error @@ -64,6 +65,7 @@ func newZookeeperMetricsScraper(logger *zap.Logger, config *Config) (*zookeeperM return &zookeeperMetricsScraper{ logger: logger, config: config, + mb: metadata.NewMetricsBuilder(config.Metrics), closeConnection: closeConnection, setConnectionDeadline: setConnectionDeadline, sendCmd: sendCmd, @@ -123,7 +125,6 @@ func (z *zookeeperMetricsScraper) appendMetrics(scanner *bufio.Scanner, rms pdat rm := pdata.NewResourceMetrics() ilm := rm.InstrumentationLibraryMetrics().AppendEmpty() ilm.InstrumentationLibrary().SetName("otelcol/zookeeper") - keepRM := false for scanner.Scan() { line := scanner.Text() parts := zookeeperFormatRE.FindStringSubmatch(line) @@ -146,8 +147,8 @@ func (z *zookeeperMetricsScraper) appendMetrics(scanner *bufio.Scanner, rms pdat continue default: // Skip metric if there is no descriptor associated with it. - initMetric := getOTLPInitFunc(metricKey) - if initMetric == nil { + recordDataPoints := recordDataPointsFunc(z.mb, metricKey) + if recordDataPoints == nil { // Unexported metric, just move to the next line. continue } @@ -159,22 +160,11 @@ func (z *zookeeperMetricsScraper) appendMetrics(scanner *bufio.Scanner, rms pdat ) continue } - metric := ilm.Metrics().AppendEmpty() - initMetric(metric) - switch metric.DataType() { - case pdata.MetricDataTypeGauge: - dp := metric.Gauge().DataPoints().AppendEmpty() - dp.SetTimestamp(now) - dp.SetIntVal(int64Val) - case pdata.MetricDataTypeSum: - dp := metric.Sum().DataPoints().AppendEmpty() - dp.SetTimestamp(now) - dp.SetIntVal(int64Val) - } - keepRM = true + recordDataPoints(now, int64Val) } } - if keepRM { + z.mb.Emit(ilm.Metrics()) + if ilm.Metrics().Len() > 0 { rm.CopyTo(rms.AppendEmpty()) } } diff --git a/receiver/zookeeperreceiver/scraper_test.go b/receiver/zookeeperreceiver/scraper_test.go index 499436e0666a..c80259ce6835 100644 --- a/receiver/zookeeperreceiver/scraper_test.go +++ b/receiver/zookeeperreceiver/scraper_test.go @@ -25,7 +25,6 @@ import ( "time" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/model/pdata" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -41,30 +40,33 @@ type logMsg struct { } func TestZookeeperMetricsScraperScrape(t *testing.T) { - commonMetrics := []pdata.Metric{ - metadata.Metrics.ZookeeperLatencyAvg.New(), - metadata.Metrics.ZookeeperLatencyMax.New(), - metadata.Metrics.ZookeeperLatencyMin.New(), - metadata.Metrics.ZookeeperPacketsReceived.New(), - metadata.Metrics.ZookeeperPacketsSent.New(), - metadata.Metrics.ZookeeperConnectionsAlive.New(), - metadata.Metrics.ZookeeperOutstandingRequests.New(), - metadata.Metrics.ZookeeperZnodes.New(), - metadata.Metrics.ZookeeperWatches.New(), - metadata.Metrics.ZookeeperEphemeralNodes.New(), - metadata.Metrics.ZookeeperApproximateDateSize.New(), - metadata.Metrics.ZookeeperOpenFileDescriptors.New(), - metadata.Metrics.ZookeeperMaxFileDescriptors.New(), + commonMetrics := map[string]bool{ + "zookeeper.latency.avg": true, + "zookeeper.latency.max": true, + "zookeeper.latency.min": true, + "zookeeper.packets.received": true, + "zookeeper.packets.sent": true, + "zookeeper.connections_alive": true, + "zookeeper.outstanding_requests": true, + "zookeeper.znodes": true, + "zookeeper.watches": true, + "zookeeper.ephemeral_nodes": true, + "zookeeper.approximate_date_size": true, + "zookeeper.open_file_descriptors": true, + "zookeeper.max_file_descriptors": true, } - var metricsV3414 []pdata.Metric - metricsV3414 = append(metricsV3414, commonMetrics...) - metricsV3414 = append(metricsV3414, metadata.Metrics.ZookeeperFsyncThresholdExceeds.New()) + metricsV3414 := make(map[string]bool, len(commonMetrics)+1) + for k, v := range commonMetrics { + metricsV3414[k] = v + } + metricsV3414["zookeeper.fsync_threshold_exceeds"] = true tests := []struct { name string - expectedMetrics []pdata.Metric + expectedMetrics map[string]bool expectedResourceAttributes map[string]string + metricsSettings func() metadata.MetricsSettings mockedZKOutputSourceFilename string mockZKConnectionErr bool expectedLogs []logMsg @@ -87,15 +89,14 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) { { name: "Test correctness with v3.5.5", mockedZKOutputSourceFilename: "mntr-3.5.5", - expectedMetrics: func() []pdata.Metric { - out := make([]pdata.Metric, 0, len(commonMetrics)+3) - out = append(out, commonMetrics...) - - out = append(out, []pdata.Metric{ - metadata.Metrics.ZookeeperFollowers.New(), - metadata.Metrics.ZookeeperSyncedFollowers.New(), - metadata.Metrics.ZookeeperPendingSyncs.New(), - }...) + expectedMetrics: func() map[string]bool { + out := make(map[string]bool, len(commonMetrics)+3) + for k, v := range commonMetrics { + out[k] = v + } + out["zookeeper.followers"] = true + out["zookeeper.synced_followers"] = true + out["zookeeper.pending_syncs"] = true return out }(), expectedResourceAttributes: map[string]string{ @@ -188,6 +189,28 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) { return nil, errors.New("") }, }, + { + name: "Disable zookeeper.watches metric", + metricsSettings: func() metadata.MetricsSettings { + ms := metadata.DefaultMetricsSettings() + ms.ZookeeperWatches.Enabled = false + return ms + }, + mockedZKOutputSourceFilename: "mntr-3.4.14", + expectedMetrics: func() map[string]bool { + out := make(map[string]bool, len(metricsV3414)) + for k, v := range metricsV3414 { + out[k] = v + } + delete(out, "zookeeper.watches") + return out + }(), + expectedResourceAttributes: map[string]string{ + "server.state": "standalone", + "zk.version": "3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf", + }, + expectedNumResourceMetrics: 1, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -198,11 +221,10 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) { <-ms.ready } - cfg := &Config{ - TCPAddr: confignet.TCPAddr{ - Endpoint: localAddr, - }, - Timeout: defaultTimeout, + cfg := createDefaultConfig().(*Config) + cfg.TCPAddr.Endpoint = localAddr + if tt.metricsSettings != nil { + cfg.Metrics = tt.metricsSettings() } core, observedLogs := observer.New(zap.DebugLevel) @@ -251,12 +273,12 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) { ilms := got.ResourceMetrics().At(0).InstrumentationLibraryMetrics() require.Equal(t, 1, ilms.Len()) - metrics := ilms.At(0).Metrics() + require.Equal(t, len(tt.expectedMetrics), metrics.Len()) - for i, metric := range tt.expectedMetrics { - assertMetricValid(t, metrics.At(i), metric) + for i := 0; i < metrics.Len(); i++ { + assertMetricValid(t, metrics.At(i), tt.expectedMetrics) } } @@ -265,8 +287,9 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) { } } -func assertMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Metric) { - assertDescriptorEqual(t, descriptor, metric) +func assertMetricValid(t *testing.T, metric pdata.Metric, expectedMetrics map[string]bool) { + ok := expectedMetrics[metric.Name()] + require.True(t, ok, "emitted metric %s is not expected", metric.Name()) switch metric.DataType() { case pdata.MetricDataTypeGauge: require.GreaterOrEqual(t, metric.Gauge().DataPoints().Len(), 1) @@ -275,13 +298,6 @@ func assertMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Metri } } -func assertDescriptorEqual(t *testing.T, expected pdata.Metric, actual pdata.Metric) { - require.Equal(t, expected.Name(), actual.Name()) - require.Equal(t, expected.Description(), actual.Description()) - require.Equal(t, expected.Unit(), actual.Unit()) - require.Equal(t, expected.DataType(), actual.DataType()) -} - type mockedServer struct { ready chan bool }