|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package kubernetesmetrics |
| 19 | + |
| 20 | +import ( |
| 21 | +"path" |
| 22 | +"strings" |
| 23 | + |
| 24 | +"go.opentelemetry.io/collector/pdata/pcommon" |
| 25 | +"go.opentelemetry.io/collector/pdata/pmetric" |
| 26 | +"go.uber.org/zap" |
| 27 | +) |
| 28 | + |
| 29 | +var scraperToElasticDataset = map[string]string{ |
| 30 | +"kubeletstatsreceiver": "kubernetes.pod", |
| 31 | +"k8sclusterreceiver": "kubernetes.node", |
| 32 | +} |
| 33 | + |
| 34 | +type remapFunc func(metrics pmetric.MetricSlice, out pmetric.MetricSlice, resource pcommon.Resource, dataset string) error |
| 35 | + |
| 36 | +// Remapper maps the OTel Kubernetes to Elastic Kubernetes metrics. These remapped |
| 37 | +// metrics power the curated Kibana dashboards. Each datapoint translated using |
| 38 | +// the remapper has the `event.processor` attribute set to `kubernetes`. |
| 39 | +type Remapper struct { |
| 40 | +logger *zap.Logger |
| 41 | +cfg config |
| 42 | +} |
| 43 | + |
| 44 | +// NewRemapper creates a new instance of kubernetes remapper. |
| 45 | +func NewRemapper(logger *zap.Logger, opts ...Option) *Remapper { |
| 46 | +return &Remapper{ |
| 47 | +cfg: newConfig(opts...), |
| 48 | +logger: logger, |
| 49 | +} |
| 50 | +} |
| 51 | + |
| 52 | +var remapFuncs = map[string]remapFunc{ |
| 53 | +"kubeletstatsreceiver": addKubeletMetrics, |
| 54 | +"k8sclusterreceiver": addClusterMetrics, |
| 55 | +} |
| 56 | + |
| 57 | +// Remap remaps an OTel ScopeMetrics to a list of OTel metrics such that the |
| 58 | +// remapped metrics could be trivially converted into Elastic system metrics. |
| 59 | +// It accepts the resource attributes to enrich the remapped metrics as per |
| 60 | +// Elastic convention. The current remapping logic assumes that each Metric |
| 61 | +// in the ScopeMetric will have datapoints for a single timestamp only. The |
| 62 | +// remapped metrics are added to the output `MetricSlice`. |
| 63 | +func (r *Remapper) Remap( |
| 64 | +src pmetric.ScopeMetrics, |
| 65 | +out pmetric.MetricSlice, |
| 66 | +resource pcommon.Resource, |
| 67 | +) { |
| 68 | +if !r.Valid(src) { |
| 69 | +return |
| 70 | +} |
| 71 | + |
| 72 | +scope := src.Scope() |
| 73 | +scraper := path.Base(scope.Name()) |
| 74 | + |
| 75 | +var dataset string // an empty dataset defers setting dataset to the caller |
| 76 | +if r.cfg.KubernetesIntegrationDataset { |
| 77 | +var ok bool |
| 78 | +dataset, ok = scraperToElasticDataset[scraper] |
| 79 | +if !ok { |
| 80 | +r.logger.Warn("no dataset defined for scraper", zap.String("scraper", scraper)) |
| 81 | +return |
| 82 | +} |
| 83 | +} |
| 84 | + |
| 85 | +remapFunc, ok := remapFuncs[scraper] |
| 86 | +if !ok { |
| 87 | +return |
| 88 | +} |
| 89 | + |
| 90 | +err := remapFunc(src.Metrics(), out, resource, dataset) |
| 91 | +if err != nil { |
| 92 | +r.logger.Warn( |
| 93 | +"failed to remap OTel kubernetes", |
| 94 | +zap.String("scope", scope.Name()), |
| 95 | +zap.Error(err), |
| 96 | +) |
| 97 | +} |
| 98 | +} |
| 99 | + |
| 100 | +// Valid validates a ScopeMetric against the kubernetes metrics remapper requirements. |
| 101 | +// Kubernetes remapper only remaps metrics from kubeletstatsreceiver or k8sclusterreceiver. |
| 102 | +func (r *Remapper) Valid(sm pmetric.ScopeMetrics) bool { |
| 103 | +return strings.HasPrefix(sm.Scope().Name(), "otelcol/kubeletstatsreceiver") || strings.HasPrefix(sm.Scope().Name(), "otelcol/k8sclusterreceiver") |
| 104 | +} |
0 commit comments