From 60fdd26c5f9c02a2c67b518d80eb3a479f4b3c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=97=20Tr=E1=BB=8Dng=20H=E1=BA=A3i?= <41283691+hainenber@users.noreply.github.com> Date: Fri, 22 Mar 2024 02:39:22 +0700 Subject: [PATCH] feat(exporter/otelcol): support converting `vcenterreceiver` (#6714) * feat(otelcol/exporter): support converting `vcenterreceiver` Signed-off-by: hainenber * fix(converter/otelcol): add converter to `MetricsBuilderConfig` attribute + use helper for converting TLS setting Signed-off-by: hainenber --------- Signed-off-by: hainenber Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> --- .../converter_vcenterreceiver.go | 145 ++++++++++++++++++ .../testdata/vcenterreceiver.river | 28 ++++ .../testdata/vcenterreceiver.yaml | 24 +++ 3 files changed, 197 insertions(+) create mode 100644 internal/converter/internal/otelcolconvert/converter_vcenterreceiver.go create mode 100644 internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.river create mode 100644 internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.yaml diff --git a/internal/converter/internal/otelcolconvert/converter_vcenterreceiver.go b/internal/converter/internal/otelcolconvert/converter_vcenterreceiver.go new file mode 100644 index 000000000000..b4b791d8ffbb --- /dev/null +++ b/internal/converter/internal/otelcolconvert/converter_vcenterreceiver.go @@ -0,0 +1,145 @@ +package otelcolconvert + +import ( + "fmt" + + "github.com/grafana/agent/internal/component/otelcol" + "github.com/grafana/agent/internal/component/otelcol/receiver/vcenter" + "github.com/grafana/agent/internal/converter/diag" + "github.com/grafana/agent/internal/converter/internal/common" + "github.com/grafana/river/rivertypes" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver" + "go.opentelemetry.io/collector/component" +) + +func init() { + converters = append(converters, vcenterReceiverConverter{}) +} + +type vcenterReceiverConverter struct{} + +func (vcenterReceiverConverter) Factory() component.Factory { return vcenterreceiver.NewFactory() } + +func (vcenterReceiverConverter) InputComponentName() string { return "" } + +func (vcenterReceiverConverter) ConvertAndAppend(state *state, id component.InstanceID, cfg component.Config) diag.Diagnostics { + var diags diag.Diagnostics + + label := state.FlowComponentLabel() + + args := toVcenterReceiver(state, id, cfg.(*vcenterreceiver.Config)) + block := common.NewBlockWithOverride([]string{"otelcol", "receiver", "vcenter"}, label, args) + + diags.Add( + diag.SeverityLevelInfo, + fmt.Sprintf("Converted %s into %s", stringifyInstanceID(id), stringifyBlock(block)), + ) + + state.Body().AppendBlock(block) + return diags +} + +func toVcenterReceiver(state *state, id component.InstanceID, cfg *vcenterreceiver.Config) *vcenter.Arguments { + var ( + nextMetrics = state.Next(id, component.DataTypeMetrics) + nextTraces = state.Next(id, component.DataTypeTraces) + ) + + return &vcenter.Arguments{ + Endpoint: cfg.Endpoint, + Username: cfg.Username, + Password: rivertypes.Secret(cfg.Password), + + DebugMetrics: common.DefaultValue[vcenter.Arguments]().DebugMetrics, + + MetricsBuilderConfig: toMetricsBuildConfig(encodeMapstruct(cfg.MetricsBuilderConfig)), + + ScraperControllerArguments: otelcol.ScraperControllerArguments{ + CollectionInterval: cfg.CollectionInterval, + InitialDelay: cfg.InitialDelay, + Timeout: cfg.Timeout, + }, + + TLS: toTLSClientArguments(cfg.TLSClientSetting), + + Output: &otelcol.ConsumerArguments{ + Metrics: toTokenizedConsumers(nextMetrics), + Traces: toTokenizedConsumers(nextTraces), + }, + } +} + +func toMetricsBuildConfig(cfg map[string]any) vcenter.MetricsBuilderConfig { + return vcenter.MetricsBuilderConfig{ + Metrics: toVcenterMetricsConfig(encodeMapstruct(cfg["metrics"])), + ResourceAttributes: toVcenterResourceAttributesConfig(encodeMapstruct(cfg["resource_attributes"])), + } +} + +func toVcenterMetricConfig(cfg map[string]any) vcenter.MetricConfig { + return vcenter.MetricConfig{ + Enabled: cfg["enabled"].(bool), + } +} + +func toVcenterMetricsConfig(cfg map[string]any) vcenter.MetricsConfig { + return vcenter.MetricsConfig{ + VcenterClusterCPUEffective: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.cpu.effective"])), + VcenterClusterCPULimit: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.cpu.limit"])), + VcenterClusterHostCount: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.host.count"])), + VcenterClusterMemoryEffective: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.memory.effective"])), + VcenterClusterMemoryLimit: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.memory.limit"])), + VcenterClusterMemoryUsed: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.memory.used"])), + VcenterClusterVMCount: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.cluster.vm.count"])), + VcenterDatastoreDiskUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.datastore.disk.usage"])), + VcenterDatastoreDiskUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.datastore.disk.utilization"])), + VcenterHostCPUUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.cpu.usage"])), + VcenterHostCPUUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.cpu.utilization"])), + VcenterHostDiskLatencyAvg: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.disk.latency.avg"])), + VcenterHostDiskLatencyMax: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.disk.latency.max"])), + VcenterHostDiskThroughput: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.disk.throughput"])), + VcenterHostMemoryUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.memory.usage"])), + VcenterHostMemoryUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.memory.utilization"])), + VcenterHostNetworkPacketCount: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.network.packet.count"])), + VcenterHostNetworkPacketErrors: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.network.packet.errors"])), + VcenterHostNetworkThroughput: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.network.throughput"])), + VcenterHostNetworkUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.host.network.usage"])), + VcenterResourcePoolCPUShares: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.resource_pool.cpu.shares"])), + VcenterResourcePoolCPUUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.resource_pool.cpu.usage"])), + VcenterResourcePoolMemoryShares: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.resource_pool.memory.shares"])), + VcenterResourcePoolMemoryUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.resource_pool.memory.usage"])), + VcenterVMCPUUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.cpu.usage"])), + VcenterVMCPUUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.cpu.utilization"])), + VcenterVMDiskLatencyAvg: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.disk.latency.avg"])), + VcenterVMDiskLatencyMax: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.disk.latency.max"])), + VcenterVMDiskThroughput: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.disk.throughput"])), + VcenterVMDiskUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.disk.usage"])), + VcenterVMDiskUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.disk.utilization"])), + VcenterVMMemoryBallooned: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.memory.ballooned"])), + VcenterVMMemorySwapped: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.memory.swapped"])), + VcenterVMMemorySwappedSsd: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.memory.swapped_ssd"])), + VcenterVMMemoryUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.memory.usage"])), + VcenterVMMemoryUtilization: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.memory.utilization"])), + VcenterVMNetworkPacketCount: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.network.packet.count"])), + VcenterVMNetworkThroughput: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.network.throughput"])), + VcenterVMNetworkUsage: toVcenterMetricConfig(encodeMapstruct(cfg["vcenter.vm.network.usage"])), + } +} + +func toVcenterResourceAttributesConfig(cfg map[string]any) vcenter.ResourceAttributesConfig { + return vcenter.ResourceAttributesConfig{ + VcenterClusterName: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.cluster.name"])), + VcenterDatastoreName: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.datastore.name"])), + VcenterHostName: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.host.name"])), + VcenterResourcePoolInventoryPath: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.resource_pool.inventory_path"])), + VcenterResourcePoolName: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.resource_pool.name"])), + VcenterVMID: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.vm.id"])), + VcenterVMName: toVcenterResourceAttributeConfig(encodeMapstruct(cfg["vcenter.vm.name"])), + } +} + +func toVcenterResourceAttributeConfig(cfg map[string]any) vcenter.ResourceAttributeConfig { + return vcenter.ResourceAttributeConfig{ + Enabled: cfg["enabled"].(bool), + } +} diff --git a/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.river b/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.river new file mode 100644 index 000000000000..905459d7c6c8 --- /dev/null +++ b/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.river @@ -0,0 +1,28 @@ +otelcol.receiver.vcenter "default" { + endpoint = "http://localhost:15672" + username = "otelu" + password = "abc" + + metrics { + vcenter.host.cpu.utilization { + enabled = false + } + } + + resource_attributes { + vcenter.cluster.name { + enabled = false + } + } + + output { + metrics = [] + traces = [otelcol.exporter.otlp.default.input] + } +} + +otelcol.exporter.otlp "default" { + client { + endpoint = "database:4317" + } +} diff --git a/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.yaml b/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.yaml new file mode 100644 index 000000000000..d0c37fb707b9 --- /dev/null +++ b/internal/converter/internal/otelcolconvert/testdata/vcenterreceiver.yaml @@ -0,0 +1,24 @@ +receivers: + vcenter: + endpoint: http://localhost:15672 + username: otelu + password: "abc" + collection_interval: 1m + initial_delay: 1s + metrics: + vcenter.host.cpu.utilization: + enabled: false + resource_attributes: + vcenter.cluster.name: + enabled: false + +exporters: + otlp: + endpoint: database:4317 + +service: + pipelines: + traces: + receivers: [vcenter] + processors: [] + exporters: [otlp]