-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/receiver_creator] Add support for metrics' hints
Signed-off-by: ChrsMark <[email protected]>
- Loading branch information
Showing
6 changed files
with
599 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: receivercreator | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for generating metrics receivers based on provided annotations' hints | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34427] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package receivercreator // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" | ||
) | ||
|
||
const ( | ||
otelHints = "io.opentelemetry.collector.receiver-creator" | ||
metricsHint = "metrics" | ||
hintsMetricsReceiver = "receiver" | ||
hintsMetricsEndpoint = "endpoint" | ||
hintsMetricsCollectionInterval = "collection_interval" | ||
hintsMetricsTimeout = "timeout" | ||
hintsMetricsUsername = "username" | ||
hintsMetricsPassword = "password" | ||
) | ||
|
||
// HintsTemplatesBuilder creates configuration templates from provided hints. | ||
type HintsTemplatesBuilder interface { | ||
createReceiverTemplatesFromHints() ([]receiverTemplate, error) | ||
} | ||
|
||
// K8sHintsBuilder creates configurations from hints provided as Pod's annotations. | ||
type K8sHintsBuilder struct { | ||
logger *zap.Logger | ||
config K8sHintsConfig | ||
} | ||
|
||
// createReceiverTemplateFromHints creates a receiver configuration based on the provided hints. | ||
// Hints are extracted from Pod's annotations. | ||
// Metrics configurations are only created for Port Endpoints. | ||
// TODO: Logs configurations are only created for Pod Container Endpoints. | ||
func (builder *K8sHintsBuilder) createReceiverTemplateFromHints(env observer.EndpointEnv) (*receiverTemplate, error) { | ||
var endpointType string | ||
var podUID string | ||
var annotations map[string]string | ||
|
||
builder.logger.Debug("handling hints for added endpoint", zap.Any("env", env)) | ||
|
||
if pod, ok := env["pod"]; ok { | ||
endpointPod, ok := pod.(observer.EndpointEnv) | ||
if !ok { | ||
return nil, fmt.Errorf("could not extract endpoint's pod: %v", zap.Any("endpointPod", pod)) | ||
} | ||
ann := endpointPod["annotations"] | ||
if ann != nil { | ||
annotations, ok = ann.(map[string]string) | ||
if !ok { | ||
return nil, fmt.Errorf("could not extract annotations: %v", zap.Any("annotations", ann)) | ||
} | ||
} | ||
podUID = endpointPod["uid"].(string) | ||
} else { | ||
return nil, nil | ||
} | ||
|
||
if valType, ok := env["type"]; ok { | ||
endpointType, ok = valType.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("could not extract endpointType: %v", zap.Any("endpointType", valType)) | ||
} | ||
} else { | ||
return nil, fmt.Errorf("could not get endpoint type: %v", zap.Any("env", env)) | ||
} | ||
|
||
if len(annotations) > 0 { | ||
if endpointType == string(observer.PortType) && builder.config.Metrics.Enabled { | ||
// Only handle Endpoints of type port for metrics | ||
return builder.createMetricsReceiver(annotations, env, podUID) | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (builder *K8sHintsBuilder) createMetricsReceiver( | ||
annotations map[string]string, | ||
env observer.EndpointEnv, | ||
podUID string) (*receiverTemplate, error) { | ||
|
||
var port uint16 | ||
|
||
portName := env["name"].(string) | ||
subreceiverKey := getHintAnnotation(annotations, metricsHint, hintsMetricsReceiver, portName) | ||
|
||
if subreceiverKey == "" { | ||
// no metrics hints detected | ||
return nil, nil | ||
} | ||
builder.logger.Debug("handling added hinted receiver", zap.Any("subreceiverKey", subreceiverKey)) | ||
|
||
userConfMap := createMetricsConfig(annotations, env, portName) | ||
|
||
if p, ok := env["port"]; ok { | ||
port = p.(uint16) | ||
if port == 0 { | ||
return nil, fmt.Errorf("could not extract port: %v", zap.Any("env", env)) | ||
} | ||
} else { | ||
return nil, fmt.Errorf("could not extract port: %v", zap.Any("env", env)) | ||
} | ||
subreceiver, err := newReceiverTemplate(fmt.Sprintf("%v/%v_%v", subreceiverKey, podUID, port), userConfMap) | ||
if err != nil { | ||
builder.logger.Error("error adding subreceiver", zap.Any("err", err)) | ||
return nil, err | ||
} | ||
|
||
builder.logger.Debug("adding hinted receiver", zap.Any("subreceiver", subreceiver)) | ||
return &subreceiver, nil | ||
|
||
} | ||
|
||
func createMetricsConfig(annotations map[string]string, env observer.EndpointEnv, portName string) userConfigMap { | ||
confMap := map[string]any{} | ||
|
||
defaultEndpoint := env["endpoint"] | ||
// get endpoint directly from the Port endpoint | ||
if defaultEndpoint != "" { | ||
confMap["endpoint"] = defaultEndpoint | ||
} | ||
|
||
subreceiverEndpoint := getHintAnnotation(annotations, metricsHint, hintsMetricsEndpoint, portName) | ||
if subreceiverEndpoint != "" { | ||
confMap["endpoint"] = subreceiverEndpoint | ||
} | ||
subreceiverColInterval := getHintAnnotation(annotations, metricsHint, hintsMetricsCollectionInterval, portName) | ||
if subreceiverColInterval != "" { | ||
confMap["collection_interval"] = subreceiverColInterval | ||
} | ||
subreceiverTimeout := getHintAnnotation(annotations, metricsHint, hintsMetricsTimeout, portName) | ||
if subreceiverTimeout != "" { | ||
confMap["timeout"] = subreceiverTimeout | ||
} | ||
subreceiverUsername := getHintAnnotation(annotations, metricsHint, hintsMetricsUsername, portName) | ||
if subreceiverUsername != "" { | ||
confMap["username"] = subreceiverUsername | ||
} | ||
subreceiverPassword := getHintAnnotation(annotations, metricsHint, hintsMetricsPassword, portName) | ||
if subreceiverPassword != "" { | ||
confMap["password"] = subreceiverPassword | ||
} | ||
return confMap | ||
} | ||
|
||
func getHintAnnotation(annotations map[string]string, hintType string, hintKey string, suffix string) string { | ||
// try to scope the hint more on container level by suffixing with .<port_name> | ||
containerLevelHint := annotations[fmt.Sprintf("%s.%s.%s/%s", otelHints, hintType, suffix, hintKey)] | ||
if containerLevelHint != "" { | ||
return containerLevelHint | ||
} | ||
|
||
// if there is no container level hint defined try to use the Pod level hint | ||
podHintKey := fmt.Sprintf("%s.%s/%s", otelHints, hintType, hintKey) | ||
podLevelHint := annotations[podHintKey] | ||
if podLevelHint != "" { | ||
return podLevelHint | ||
} | ||
return "" | ||
} |
Oops, something went wrong.