Skip to content

Commit

Permalink
chore: Simplified filter expression (#1481)
Browse files Browse the repository at this point in the history
Co-authored-by: Hisar Balik <[email protected]>
  • Loading branch information
a-thaler and hisarbalik authored Oct 1, 2024
1 parent 27d5537 commit 976cd65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
32 changes: 12 additions & 20 deletions internal/otelcollector/config/metric/gateway/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,40 +183,32 @@ func createNamespacesConditions(namespaces []string) []string {
return namespacesConditions
}

// Drop the metrics scraped by k8s cluster, except for the pod, container metrics
// Drop the metrics scraped by k8s cluster which are not workload related, So all besides the pod and container metrics
// Complete list of the metrics is here: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/k8sclusterreceiver/documentation.md
func makeK8sClusterDropMetrics() *FilterProcessor {
metricNames := []string{
"^k8s.deployment.*",
"^k8s.cronjob.*",
"^k8s.daemonset.*",
"^k8s.hpa.*",
"^k8s.job.*",
"^k8s.replicaset.*",
"^k8s.resource_quota.*",
"^k8s.statefulset.*",
}
metricNameConditions := createIsMatchNameConditions(metricNames)
"deployment",
"cronjob",
"daemonset",
"hpa",
"job",
"replicaset",
"resource_quota",
"statefulset",
}

return &FilterProcessor{
Metrics: FilterProcessorMetrics{
Metric: []string{
ottlexpr.JoinWithAnd(
inputSourceEquals(metric.InputSourceRuntime),
ottlexpr.JoinWithOr(metricNameConditions...),
ottlexpr.IsMatch("name", fmt.Sprintf("^k8s.%s.*", ottlexpr.JoinWithRegExpOr(metricNames...))),
),
},
},
}
}

func createIsMatchNameConditions(names []string) []string {
var nameConditions []string
for _, name := range names {
nameConditions = append(nameConditions, ottlexpr.IsMatch("name", name))
}
return nameConditions
}

func inputSourceEquals(inputSourceType metric.InputSourceType) string {
return ottlexpr.ScopeNameEquals(metric.InstrumentationScope[inputSourceType])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ func TestProcessors(t *testing.T) {

t.Run("k8s cluster receiver filter metrics", func(t *testing.T) {
k8sClusterMetricsDrop := "instrumentation_scope.name == \"io.kyma-project.telemetry/runtime\"" +
" and (IsMatch(name, \"^k8s.deployment.*\") or IsMatch(name, \"^k8s.cronjob.*\") or IsMatch(name, \"^k8s.daemonset.*\") or IsMatch(name, \"^k8s.hpa.*\") or IsMatch(name, \"^k8s.job.*\")" +
" or IsMatch(name, \"^k8s.replicaset.*\") or IsMatch(name, \"^k8s.resource_quota.*\") or IsMatch(name, \"^k8s.statefulset.*\"))"
" and IsMatch(name, \"^k8s.(deployment|cronjob|daemonset|hpa|job|replicaset|resource_quota|statefulset).*\")"

collectorConfig, _, err := sut.Build(
ctx,
Expand Down
4 changes: 4 additions & 0 deletions internal/otelcollector/config/ottlexpr/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func JoinWithOr(parts ...string) string {
return fmt.Sprintf("(%s)", strings.Join(parts, " or "))
}

func JoinWithRegExpOr(parts ...string) string {
return fmt.Sprintf("(%s)", strings.Join(parts, "|"))
}

func JoinWithAnd(parts ...string) string {
return strings.Join(parts, " and ")
}
Expand Down

0 comments on commit 976cd65

Please sign in to comment.