Skip to content

Commit

Permalink
pkg/trace/event: lower-case all keys for legacy extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
gbbr committed Mar 5, 2019
1 parent 0b588d6 commit 8208fda
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type AgentConfig struct {
// It maps tag keys to a set of replacements. Only supported in A6.
ReplaceTags []*ReplaceRule

// transaction analytics, all map keys are lower-cased by Viper.
// transaction analytics
AnalyzedRateByServiceLegacy map[string]float64
AnalyzedSpansByService map[string]map[string]float64

Expand Down
12 changes: 9 additions & 3 deletions pkg/trace/event/extractor_fixed_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ type fixedRateExtractor struct {

// NewFixedRateExtractor returns an APM event extractor that decides whether to extract APM events from spans following
// the provided extraction rates for a span's (service name, operation name) pair.
// The map keys must strictly be lower-cased, as provided by Viper.
func NewFixedRateExtractor(rateByServiceAndName map[string]map[string]float64) Extractor {
return &fixedRateExtractor{
rateByServiceAndName: rateByServiceAndName,
// lower-case keys for case insensitive matching (see #3113)
rbs := make(map[string]map[string]float64, len(rateByServiceAndName))
for service, opRates := range rateByServiceAndName {
k := strings.ToLower(service)
rbs[k] = make(map[string]float64, len(opRates))
for op, rate := range opRates {
rbs[k][strings.ToLower(op)] = rate
}
}
return &fixedRateExtractor{rateByServiceAndName: rbs}
}

// Extract decides to extract an apm event from a span if its service and name have a corresponding extraction rate
Expand Down
16 changes: 8 additions & 8 deletions pkg/trace/event/extractor_fixed_rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func createTestSpans(serviceName string, operationName string) []*pb.Span {
func TestFixedCases(t *testing.T) {
assert := assert.New(t)
e := NewFixedRateExtractor(map[string]map[string]float64{
"service1": {
"op1": 1,
"sERvice1": {
"OP1": 1,
"op2": 0.5,
},
})
Expand All @@ -39,14 +39,14 @@ func TestFixedCases(t *testing.T) {

func TestAnalyzedExtractor(t *testing.T) {
config := make(map[string]map[string]float64)
config["servicea"] = make(map[string]float64)
config["servicea"]["opa"] = 0
config["serviceA"] = make(map[string]float64)
config["serviceA"]["opA"] = 0

config["serviceb"] = make(map[string]float64)
config["serviceb"]["opb"] = 0.5
config["serviceB"] = make(map[string]float64)
config["serviceB"]["opB"] = 0.5

config["servicec"] = make(map[string]float64)
config["servicec"]["opc"] = 1
config["serviceC"] = make(map[string]float64)
config["serviceC"]["opC"] = 1

tests := []extractorTestCase{
// Name: <priority>/(<no match reason>/<extraction rate>)
Expand Down
9 changes: 6 additions & 3 deletions pkg/trace/event/extractor_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ type legacyExtractor struct {
}

// NewLegacyExtractor returns an APM event extractor that decides whether to extract APM events from spans following the
// specified extraction rates for a span's service. The map keys must strictly be lower-cased.
// specified extraction rates for a span's service.
func NewLegacyExtractor(rateByService map[string]float64) Extractor {
return &legacyExtractor{
rateByService: rateByService,
// lower-case keys for case insensitive matching (see #3113)
rbs := make(map[string]float64, len(rateByService))
for k, v := range rateByService {
rbs[strings.ToLower(k)] = v
}
return &legacyExtractor{rateByService: rbs}
}

// Extract decides to extract an apm event from the provided span if there's an extraction rate configured for that
Expand Down
2 changes: 1 addition & 1 deletion pkg/trace/event/extractor_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestLegacyCases(t *testing.T) {
assert := assert.New(t)
e := NewLegacyExtractor(map[string]float64{"service1": 1})
e := NewLegacyExtractor(map[string]float64{"serviCE1": 1})
span := &pb.Span{Service: "SeRvIcE1"}
traceutil.SetTopLevel(span, true)

Expand Down

0 comments on commit 8208fda

Please sign in to comment.