Skip to content

Commit

Permalink
Relocate spanmetrics.FilterPolicy to sharedconfig package and impleme…
Browse files Browse the repository at this point in the history
…nt overrides

Signed-off-by: Zach Leslie <[email protected]>
  • Loading branch information
zalegrala committed Apr 12, 2023
1 parent 731dff8 commit 126f390
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 112 deletions.
2 changes: 2 additions & 0 deletions modules/generator/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"github.com/grafana/tempo/modules/generator/registry"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/pkg/sharedconfig"
)

type metricsGeneratorOverrides interface {
Expand All @@ -14,6 +15,7 @@ type metricsGeneratorOverrides interface {
MetricsGeneratorProcessorSpanMetricsHistogramBuckets(userID string) []float64
MetricsGeneratorProcessorSpanMetricsDimensions(userID string) []string
MetricsGeneratorProcessorSpanMetricsIntrinsicDimensions(userID string) map[string]bool
MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID string) []sharedconfig.FilterPolicy
}

var _ metricsGeneratorOverrides = (*overrides.Overrides)(nil)
11 changes: 10 additions & 1 deletion modules/generator/overrides_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package generator

import "time"
import (
"time"

"github.com/grafana/tempo/pkg/sharedconfig"
)

type mockOverrides struct {
processors map[string]struct{}
Expand All @@ -9,6 +13,7 @@ type mockOverrides struct {
spanMetricsHistogramBuckets []float64
spanMetricsDimensions []string
spanMetricsIntrinsicDimensions map[string]bool
spanMetricsFilterPolicies []sharedconfig.FilterPolicy
}

var _ metricsGeneratorOverrides = (*mockOverrides)(nil)
Expand Down Expand Up @@ -48,3 +53,7 @@ func (m *mockOverrides) MetricsGeneratorProcessorSpanMetricsDimensions(userID st
func (m *mockOverrides) MetricsGeneratorProcessorSpanMetricsIntrinsicDimensions(userID string) map[string]bool {
return m.spanMetricsIntrinsicDimensions
}

func (m *mockOverrides) MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID string) []sharedconfig.FilterPolicy {
return m.spanMetricsFilterPolicies
}
25 changes: 2 additions & 23 deletions modules/generator/processor/spanmetrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spanmetrics
import (
"flag"

"github.com/grafana/tempo/pkg/sharedconfig"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -32,7 +33,7 @@ type Config struct {
SpanMultiplierKey string `yaml:"span_multiplier_key"`

// FilterPolicies is a list of policies that will be applied to spans for inclusion or exlusion.
FilterPolicies []FilterPolicy `yaml:"filter_policies"`
FilterPolicies []sharedconfig.FilterPolicy `yaml:"filter_policies"`
}

func (cfg *Config) RegisterFlagsAndApplyDefaults(prefix string, f *flag.FlagSet) {
Expand Down Expand Up @@ -70,25 +71,3 @@ func (ic *IntrinsicDimensions) ApplyFromMap(dimensions map[string]bool) error {
}
return nil
}

type FilterPolicy struct {
Include *PolicyMatch `yaml:"include"`
Exclude *PolicyMatch `yaml:"exclude"`
}

type MatchType string

const (
Strict MatchType = "strict"
Regex MatchType = "regex"
)

type PolicyMatch struct {
MatchType MatchType `yaml:"match_type"`
Attributes []MatchPolicyAttribute `yaml:"attributes"`
}

type MatchPolicyAttribute struct {
Key string `yaml:"key"`
Value interface{} `yaml:"value"`
}
33 changes: 17 additions & 16 deletions modules/generator/processor/spanmetrics/spanmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
gen "github.com/grafana/tempo/modules/generator/processor"
processor_util "github.com/grafana/tempo/modules/generator/processor/util"
"github.com/grafana/tempo/modules/generator/registry"
"github.com/grafana/tempo/pkg/sharedconfig"
"github.com/grafana/tempo/pkg/tempopb"
v1_common "github.com/grafana/tempo/pkg/tempopb/common/v1"
v1 "github.com/grafana/tempo/pkg/tempopb/resource/v1"
Expand Down Expand Up @@ -50,9 +51,9 @@ type filterPolicy struct {

// SplitPolicy is the result of parsing a policy from the config file to be specific about the area the given policy is applied to.
type splitPolicy struct {
ResourceMatch *PolicyMatch
SpanMatch *PolicyMatch
IntrinsicMatch *PolicyMatch
ResourceMatch *sharedconfig.PolicyMatch
SpanMatch *sharedconfig.PolicyMatch
IntrinsicMatch *sharedconfig.PolicyMatch
}

func New(cfg Config, registry registry.Registry) gen.Processor {
Expand Down Expand Up @@ -199,7 +200,7 @@ func policyMatch(policy *splitPolicy, rs *v1.Resource, span *v1_trace.Span) bool
}

// policyMatchIntrinsicAttrs returns true when all intrinsic values in the polciy match the span.
func policyMatchIntrinsicAttrs(policy *PolicyMatch, span *v1_trace.Span) bool {
func policyMatchIntrinsicAttrs(policy *sharedconfig.PolicyMatch, span *v1_trace.Span) bool {
matches := 0
for _, pa := range policy.Attributes {
attr := traceql.MustParseIdentifier(pa.Key)
Expand All @@ -226,7 +227,7 @@ func policyMatchIntrinsicAttrs(policy *PolicyMatch, span *v1_trace.Span) bool {
}

// policyMatchAttrs returns true if all attributes in the policy match the attributes in the span. String, bool, int, and floats are supported. Regex MatchType may be applied to string span attributes.
func policyMatchAttrs(policy *PolicyMatch, attrs []*v1_common.KeyValue) bool {
func policyMatchAttrs(policy *sharedconfig.PolicyMatch, attrs []*v1_common.KeyValue) bool {

matches := 0
var v *v1_common.AnyValue
Expand Down Expand Up @@ -297,44 +298,44 @@ func isIntrinsicDimension(name string) bool {
name == dimStatusMessage
}

func stringMatch(matchType MatchType, s, pattern string) bool {
func stringMatch(matchType sharedconfig.MatchType, s, pattern string) bool {
switch matchType {
case Strict:
case sharedconfig.Strict:
return s == pattern
case Regex:
case sharedconfig.Regex:
re := regexp.MustCompile(pattern)
return re.MatchString(s)
default:
return false
}
}

func getSplitPolicy(policy *PolicyMatch) *splitPolicy {
func getSplitPolicy(policy *sharedconfig.PolicyMatch) *splitPolicy {
if policy == nil {
return nil
}

// A policy to match against the resource attributes
resourcePolicy := &PolicyMatch{
resourcePolicy := &sharedconfig.PolicyMatch{
MatchType: policy.MatchType,
Attributes: make([]MatchPolicyAttribute, 0),
Attributes: make([]sharedconfig.MatchPolicyAttribute, 0),
}

// A policy to match against the span attributes
spanPolicy := &PolicyMatch{
spanPolicy := &sharedconfig.PolicyMatch{
MatchType: policy.MatchType,
Attributes: make([]MatchPolicyAttribute, 0),
Attributes: make([]sharedconfig.MatchPolicyAttribute, 0),
}

intrinsicPolicy := &PolicyMatch{
intrinsicPolicy := &sharedconfig.PolicyMatch{
MatchType: policy.MatchType,
Attributes: make([]MatchPolicyAttribute, 0),
Attributes: make([]sharedconfig.MatchPolicyAttribute, 0),
}

for _, pa := range policy.Attributes {
attr := traceql.MustParseIdentifier(pa.Key)

attribute := MatchPolicyAttribute{
attribute := sharedconfig.MatchPolicyAttribute{
Key: attr.Name,
Value: pa.Value,
}
Expand Down
Loading

0 comments on commit 126f390

Please sign in to comment.