|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package receivercreator // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator" |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "go.uber.org/zap" |
| 10 | + |
| 11 | + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + hintsMetricsReceiver = "io.opentelemetry.collector.receiver-creator.metrics/receiver" |
| 16 | + hintsMetricsEndpoint = "io.opentelemetry.collector.receiver-creator.metrics/endpoint" |
| 17 | + hintsMetricsCollectionInterval = "io.opentelemetry.collector.receiver-creator.metrics/collection_interval" |
| 18 | + hintsMetricsTimeout = "io.opentelemetry.collector.receiver-creator.metrics/timeout" |
| 19 | + hintsMetricsUsername = "io.opentelemetry.collector.receiver-creator.metrics/username" |
| 20 | + hintsMetricsPassword = "io.opentelemetry.collector.receiver-creator.metrics/password" |
| 21 | +) |
| 22 | + |
| 23 | +// HintsTemplatesBuilder creates configuration templates from provided hints. |
| 24 | +type HintsTemplatesBuilder interface { |
| 25 | + createReceiverTemplatesFromHints() ([]receiverTemplate, error) |
| 26 | +} |
| 27 | + |
| 28 | +// K8sHintsBuilder creates configurations from hints provided as Pod's annotations. |
| 29 | +type K8sHintsBuilder struct { |
| 30 | + logger *zap.Logger |
| 31 | +} |
| 32 | + |
| 33 | +func (builder *K8sHintsBuilder) createReceiverTemplatesFromHints(env observer.EndpointEnv) ([]receiverTemplate, error) { |
| 34 | + var endpointType string |
| 35 | + var podUID string |
| 36 | + var port uint16 |
| 37 | + var annotations map[string]string |
| 38 | + var receiverTemplates []receiverTemplate |
| 39 | + |
| 40 | + builder.logger.Debug("handling hints for added endpoint", zap.Any("env", env)) |
| 41 | + |
| 42 | + if pod, ok := env["pod"]; ok { |
| 43 | + endpointPod, ok := pod.(observer.EndpointEnv) |
| 44 | + if !ok { |
| 45 | + return receiverTemplates, fmt.Errorf("could not extract endpoint's pod: %v", zap.Any("endpointPod", pod)) |
| 46 | + } |
| 47 | + ann := endpointPod["annotations"] |
| 48 | + if ann != nil { |
| 49 | + annotations, ok = ann.(map[string]string) |
| 50 | + if !ok { |
| 51 | + return receiverTemplates, fmt.Errorf("could not extract annotations: %v", zap.Any("annotations", ann)) |
| 52 | + } |
| 53 | + } |
| 54 | + podUID = endpointPod["uid"].(string) |
| 55 | + } else { |
| 56 | + return receiverTemplates, nil |
| 57 | + } |
| 58 | + |
| 59 | + if valType, ok := env["type"]; ok { |
| 60 | + endpointType, ok = valType.(string) |
| 61 | + if !ok { |
| 62 | + return receiverTemplates, fmt.Errorf("could not extract endpointType: %v", zap.Any("endpointType", valType)) |
| 63 | + } |
| 64 | + } else { |
| 65 | + return receiverTemplates, fmt.Errorf("could not get endpoint type: %v", zap.Any("env", env)) |
| 66 | + } |
| 67 | + |
| 68 | + if len(annotations) > 0 { |
| 69 | + if endpointType == string(observer.PortType) { |
| 70 | + // Only handle Endpoints of type port for metrics |
| 71 | + portName := env["name"].(string) |
| 72 | + metricsReceiverEnabled := getHintAnnotation(annotations, hintsMetricsReceiver, portName) |
| 73 | + if metricsReceiverEnabled != "" { |
| 74 | + subreceiverKey := metricsReceiverEnabled |
| 75 | + if subreceiverKey == "" { |
| 76 | + return receiverTemplates, nil |
| 77 | + } |
| 78 | + builder.logger.Debug("handling added hinted receiver", zap.Any("subreceiverKey", subreceiverKey)) |
| 79 | + |
| 80 | + userConfMap := createMetricsConfig(annotations, env, portName) |
| 81 | + |
| 82 | + if p, ok := env["port"]; ok { |
| 83 | + port = p.(uint16) |
| 84 | + if port == 0 { |
| 85 | + return receiverTemplates, fmt.Errorf("could not extract port: %v", zap.Any("env", env)) |
| 86 | + } |
| 87 | + } else { |
| 88 | + return receiverTemplates, fmt.Errorf("could not extract port: %v", zap.Any("env", env)) |
| 89 | + } |
| 90 | + subreceiver, err := newReceiverTemplate(fmt.Sprintf("%v/%v_%v", subreceiverKey, podUID, port), userConfMap) |
| 91 | + if err != nil { |
| 92 | + builder.logger.Error("error adding subreceiver", zap.Any("err", err)) |
| 93 | + return receiverTemplates, err |
| 94 | + } |
| 95 | + |
| 96 | + subreceiver.Rule = fmt.Sprintf("type == \"port\" && port ==%v", port) // |
| 97 | + subreceiver.rule, err = newRule(subreceiver.Rule) |
| 98 | + if err != nil { |
| 99 | + builder.logger.Error("error adding subreceiver rule", zap.Any("err", err)) |
| 100 | + return receiverTemplates, err |
| 101 | + } |
| 102 | + builder.logger.Debug("adding hinted receiver", zap.Any("subreceiver", subreceiver)) |
| 103 | + receiverTemplates = append(receiverTemplates, subreceiver) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return receiverTemplates, nil |
| 108 | +} |
| 109 | + |
| 110 | +func createMetricsConfig(annotations map[string]string, env observer.EndpointEnv, portName string) userConfigMap { |
| 111 | + confMap := map[string]any{} |
| 112 | + |
| 113 | + defaultEndpoint := env["endpoint"] |
| 114 | + // get endpoint directly from the Port endpoint |
| 115 | + if defaultEndpoint != "" { |
| 116 | + confMap["endpoint"] = defaultEndpoint |
| 117 | + } |
| 118 | + |
| 119 | + subreceiverEndpoint := getHintAnnotation(annotations, hintsMetricsEndpoint, portName) |
| 120 | + if subreceiverEndpoint != "" { |
| 121 | + confMap["endpoint"] = subreceiverEndpoint |
| 122 | + } |
| 123 | + subreceiverColInterval := getHintAnnotation(annotations, hintsMetricsCollectionInterval, portName) |
| 124 | + if subreceiverColInterval != "" { |
| 125 | + confMap["collection_interval"] = subreceiverColInterval |
| 126 | + } |
| 127 | + subreceiverTimeout := getHintAnnotation(annotations, hintsMetricsTimeout, portName) |
| 128 | + if subreceiverTimeout != "" { |
| 129 | + confMap["timeout"] = subreceiverTimeout |
| 130 | + } |
| 131 | + subreceiverUsername := getHintAnnotation(annotations, hintsMetricsUsername, portName) |
| 132 | + if subreceiverUsername != "" { |
| 133 | + confMap["username"] = subreceiverUsername |
| 134 | + } |
| 135 | + subreceiverPassword := getHintAnnotation(annotations, hintsMetricsPassword, portName) |
| 136 | + if subreceiverPassword != "" { |
| 137 | + confMap["password"] = subreceiverPassword |
| 138 | + } |
| 139 | + return confMap |
| 140 | +} |
| 141 | + |
| 142 | +func getHintAnnotation(annotations map[string]string, hintKey string, portName string) string { |
| 143 | + containerLevelHint := annotations[fmt.Sprintf("%s.%s", hintKey, portName)] |
| 144 | + if containerLevelHint != "" { |
| 145 | + return containerLevelHint |
| 146 | + } |
| 147 | + |
| 148 | + // if there is no container level hint defined try to scope the hint more on container level by suffixing with .<port_name> |
| 149 | + podLevelHint := annotations[hintKey] |
| 150 | + if podLevelHint != "" { |
| 151 | + return podLevelHint |
| 152 | + } |
| 153 | + return "" |
| 154 | +} |
0 commit comments