Skip to content

Commit

Permalink
Collector tags - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radekg committed Oct 11, 2019
1 parent 3eeb346 commit d6e8587
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
3 changes: 1 addition & 2 deletions cmd/collector/app/builder/builder_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import (
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/collector/app"
"github.com/jaegertracing/jaeger/cmd/flags"
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/ports"

"github.com/jaegertracing/jaeger/cmd/flags"
)

const (
Expand Down
3 changes: 3 additions & 0 deletions cmd/collector/app/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ func TestAllOptionSet(t *testing.T) {
Options.Sanitizer(func(span *model.Span) *model.Span { return span }),
Options.QueueSize(10),
Options.PreSave(func(span *model.Span) {}),
Options.CollectorTags(map[string]string{"extra": "tags"}),
)
assert.EqualValues(t, 5, opts.numWorkers)
assert.EqualValues(t, 10, opts.queueSize)
assert.EqualValues(t, map[string]string{"extra": "tags"}, opts.collectorTags)
}

func TestNoOptionsSet(t *testing.T) {
opts := Options.apply()
assert.EqualValues(t, DefaultNumWorkers, opts.numWorkers)
assert.EqualValues(t, 0, opts.queueSize)
assert.Nil(t, opts.collectorTags)
assert.False(t, opts.reportBusy)
assert.False(t, opts.blockingSubmit)
assert.NotPanics(t, func() { opts.preProcessSpans(nil) })
Expand Down
11 changes: 8 additions & 3 deletions cmd/collector/app/span_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func (sp *spanProcessor) processItemFromQueue(item *queueItem) {
sp.metrics.InQueueLatency.Record(time.Since(item.queuedTime))
}

func (sp *spanProcessor) addCollectorTags(span *model.Span) {
// TODO add support for deduping tags, https://github.com/jaegertracing/jaeger/issues/1778
for k, v := range sp.collectorTags {
span.Tags = append(span.Tags, model.String(k, v))
}
}

func (sp *spanProcessor) enqueueSpan(span *model.Span, originalFormat SpanFormat, transport InboundTransport) bool {
spanCounts := sp.metrics.GetCountsForFormat(originalFormat, transport)
spanCounts.ReceivedBySvc.ReportServiceNameForSpan(span)
Expand All @@ -162,9 +169,7 @@ func (sp *spanProcessor) enqueueSpan(span *model.Span, originalFormat SpanFormat
span.Tags = append(span.Tags, model.String("internal.span.format", string(originalFormat)))

// append the collector tags
for k, v := range sp.collectorTags {
span.Tags = append(span.Tags, model.String(k, v))
}
sp.addCollectorTags(span)

item := &queueItem{
queuedTime: time.Now(),
Expand Down
27 changes: 27 additions & 0 deletions cmd/collector/app/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,30 @@ func TestSpanProcessorWithNilProcess(t *testing.T) {
}}
mb.AssertCounterMetrics(t, expected...)
}

func TestSpanProcessorWithCollectorTags(t *testing.T) {

testCollectorTags := map[string]string{
"extra": "tag",
}

w := &fakeSpanWriter{}
p := NewSpanProcessor(w, Options.CollectorTags(testCollectorTags)).(*spanProcessor)
defer p.Stop()

span := &model.Span{}

p.addCollectorTags(span)

for k, v := range testCollectorTags {
var foundTag bool
for _, tag := range span.Tags {
if tag.GetKey() == k {
assert.Equal(t, v, tag.AsString())
foundTag = true
break
}
}
assert.True(t, foundTag)
}
}
6 changes: 3 additions & 3 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func TryLoadConfigFile(v *viper.Viper) error {
}

// ParseJaegerTags parses the Jaeger tags string into a map.
func ParseJaegerTags(agentTags string) map[string]string {
if agentTags == "" {
func ParseJaegerTags(jaegerTags string) map[string]string {
if jaegerTags == "" {
return nil
}
tagPairs := strings.Split(string(agentTags), ",")
tagPairs := strings.Split(string(jaegerTags), ",")
tags := make(map[string]string)
for _, p := range tagPairs {
kv := strings.SplitN(p, "=", 2)
Expand Down
52 changes: 52 additions & 0 deletions cmd/flags/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseJaegerTags(t *testing.T) {

jaegerTags := fmt.Sprintf("%s,%s,%s,%s,%s,%s",
"key=value",
"envVar1=${envKey1:defaultVal1}",
"envVar2=${envKey2:defaultVal2}",
"envVar3=${envKey3}",
"envVar4=${envKey4}",
"envVar5=${envVar5:}",
)

os.Setenv("envKey1", "envVal1")
defer os.Unsetenv("envKey1")

os.Setenv("envKey4", "envVal4")
defer os.Unsetenv("envKey4")

expectedTags := map[string]string{
"key": "value",
"envVar1": "envVal1",
"envVar2": "defaultVal2",
"envVar4": "envVal4",
"envVar5": "",
}

assert.Equal(t, expectedTags, ParseJaegerTags(jaegerTags))
}

0 comments on commit d6e8587

Please sign in to comment.