-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for pod annotation based scraping (#414)
- Loading branch information
1 parent
070eff5
commit d84a02b
Showing
10 changed files
with
238 additions
and
36 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
otelcollector/configmapparser/default-prom-configs/podannotationsDefault.yml
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,25 @@ | ||
scrape_configs: | ||
- job_name: 'kubernetes-pods' | ||
kubernetes_sd_configs: | ||
- role: pod | ||
relabel_configs: | ||
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] | ||
action: keep | ||
regex: true | ||
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path] | ||
action: replace | ||
target_label: __metrics_path__ | ||
regex: (.+) | ||
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port] | ||
action: replace | ||
regex: ([^:]+)(?::\d+)?;(\d+) | ||
replacement: $1:$2 | ||
target_label: __address__ | ||
- action: labelmap | ||
regex: __meta_kubernetes_pod_label_(.+) | ||
- source_labels: [__meta_kubernetes_namespace] | ||
action: replace | ||
target_label: kubernetes_namespace | ||
- source_labels: [__meta_kubernetes_pod_name] | ||
action: replace | ||
target_label: kubernetes_pod_name |
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
69 changes: 69 additions & 0 deletions
69
otelcollector/configmapparser/tomlparser-pod-annotation-based-scraping.rb
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,69 @@ | ||
#!/usr/local/bin/ruby | ||
# frozen_string_literal: true | ||
|
||
require "tomlrb" | ||
require "yaml" | ||
require_relative "ConfigParseErrorLogger" | ||
require_relative "tomlparser-utils" | ||
|
||
LOGGING_PREFIX = "pod-annotation-based-scraping" | ||
@configMapMountPath = "/etc/config/settings/pod-annotation-based-scraping" | ||
@podannotationNamespaceRegex = "" | ||
|
||
# Use parser to parse the configmap toml file to a ruby structure | ||
def parseConfigMap | ||
begin | ||
# Check to see if config map is created | ||
if (File.file?(@configMapMountPath)) | ||
parsedConfig = Tomlrb.load_file(@configMapMountPath, symbolize_keys: true) | ||
return parsedConfig | ||
else | ||
ConfigParseErrorLogger.log(LOGGING_PREFIX, "configmap section not mounted, using defaults") | ||
return nil | ||
end | ||
rescue => errorStr | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Exception while parsing config map: #{errorStr}, using defaults, please check config map for errors") | ||
return nil | ||
end | ||
end | ||
|
||
# Use the ruby structure created after config parsing to set the right values to be used for otel collector settings | ||
def populateSettingValuesFromConfigMap(parsedConfig) | ||
begin | ||
podannotationRegex = parsedConfig[:podannotationnamepsaceregex] | ||
if !podannotationRegex.nil? && podannotationRegex.kind_of?(String) && !podannotationRegex.empty? | ||
if isValidRegex(podannotationRegex) == true | ||
@podannotationNamespaceRegex = podannotationRegex | ||
ConfigParseErrorLogger.log(LOGGING_PREFIX, "Using configmap namepace regex for podannotations") | ||
else | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Invalid namespace regex for podannotations") | ||
end | ||
else | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "podannotations namespace regex either not specified or not of type string") | ||
end | ||
end | ||
end | ||
|
||
ConfigParseErrorLogger.logSection(LOGGING_PREFIX, "Start Processing") | ||
configMapSettings = parseConfigMap | ||
if !configMapSettings.nil? | ||
populateSettingValuesFromConfigMap(configMapSettings) | ||
elsif (File.file?(@configMapMountPath)) | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Error loading configmap section - using defaults") | ||
end | ||
|
||
# Write the settings to file, so that they can be set as environment variables | ||
file = File.open("/opt/microsoft/configmapparser/config_def_pod_annotation_based_scraping", "w") | ||
|
||
namespaceRegexHash = {} | ||
namespaceRegexHash["POD_ANNOTATION_NAMESPACES_REGEX"] = @podannotationNamespaceRegex | ||
|
||
if !file.nil? | ||
# Close file after writing scrape interval list hash | ||
# Writing it as yaml as it is easy to read and write hash | ||
file.write("export AZMON_PROMETHEUS_POD_ANNOTATION_NAMESPACES_REGEX=#{@podannotationNamespaceRegex}\n") | ||
file.close | ||
else | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Exception while opening file for writing regex config hash") | ||
end | ||
ConfigParseErrorLogger.logSection(LOGGING_PREFIX, "End default-targets-namespace-keep-list-regex-settings Processing") |
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,39 @@ | ||
#!/usr/local/bin/ruby | ||
# frozen_string_literal: true | ||
|
||
require_relative "ConfigParseErrorLogger" | ||
|
||
if (!ENV["OS_TYPE"].nil? && ENV["OS_TYPE"].downcase == "linux") | ||
require "re2" | ||
end | ||
|
||
# RE2 is not supported for windows | ||
def isValidRegex_linux(str) | ||
begin | ||
# invalid regex example -> 'sel/\\' | ||
re2Regex = RE2::Regexp.new(str) | ||
return re2Regex.ok? | ||
rescue => errorStr | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Exception while validating regex for target metric keep list - #{errorStr}, regular expression str - #{str}") | ||
return false | ||
end | ||
end | ||
|
||
def isValidRegex_windows(str) | ||
begin | ||
# invalid regex example -> 'sel/\\' | ||
re2Regex = Regexp.new(str) | ||
return true | ||
rescue => errorStr | ||
ConfigParseErrorLogger.logError(LOGGING_PREFIX, "Exception while validating regex for target metric keep list - #{errorStr}, regular expression str - #{str}") | ||
return false | ||
end | ||
end | ||
|
||
def isValidRegex(str) | ||
if ENV["OS_TYPE"] == "linux" | ||
return isValidRegex_linux(str) | ||
else | ||
return isValidRegex_windows(str) | ||
end | ||
end |
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
Oops, something went wrong.