Skip to content

Commit

Permalink
Do not try to determine IP address if it is already passed via tracer…
Browse files Browse the repository at this point in the history
… tags (jaegertracing#402)

* NewTracer suport passing iptag [fixes jaegertracing#401]

old codes did not support passing a iptag to NewTracer because NewTracer
will overwrite the iptag. Now modify the NewTracer method, make it
support passing iptag

two private method of Tracer add : getTag, setTag
And testing function is also added into tracer_test.go, all test of
NewTracer passed.

Signed-off-by: 吴灿柳 <[email protected]>

* remove a redundant return

remove a redundant return in order to pass GolangCI check

Signed-off-by: 吴灿柳 <[email protected]>

* Delete setTag, add assertions in unit test, etc

1.delete setTag, only use append
2.getTag method return (interface{}, bool) for indicating found or not
3.no validation for external provided iptag, buyer beware
4.unit test add assertions, and remove the fmt print.

Signed-off-by: 吴灿柳 <[email protected]>
  • Loading branch information
NikoKVCS authored and yurishkuro committed Jul 29, 2019
1 parent 402bec9 commit e895c7a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ func NewTracer(
if hostname, err := os.Hostname(); err == nil {
t.tags = append(t.tags, Tag{key: TracerHostnameTagKey, value: hostname})
}
if ip, err := utils.HostIP(); err == nil {
if ipval, ok := t.getTag(TracerIPTagKey); ok {
ipv4, err := utils.ParseIPToUint32(ipval.(string))
if err != nil {
t.hostIPv4 = 0
t.logger.Error("Unable to convert the externally provided ip to uint32: " + err.Error())
} else {
t.hostIPv4 = ipv4
}
} else if ip, err := utils.HostIP(); err == nil {
t.tags = append(t.tags, Tag{key: TracerIPTagKey, value: ip.String()})
t.hostIPv4 = utils.PackIPAsUint32(ip)
} else {
Expand Down Expand Up @@ -347,6 +355,16 @@ func (t *Tracer) Tags() []opentracing.Tag {
return tags
}

// getTag returns the value of specific tag, if not exists, return nil.
func (t *Tracer) getTag(key string) (interface{}, bool) {
for _, tag := range t.tags {
if tag.key == key {
return tag.value, true
}
}
return nil, false
}

// newSpan returns an instance of a clean Span object.
// If options.PoolSpans is true, the spans are retrieved from an object pool.
func (t *Tracer) newSpan() *Span {
Expand Down
29 changes: 29 additions & 0 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,35 @@ func TestThrottling_DebugHeader(t *testing.T) {
assert.False(t, sp.context.IsDebug(), "debug should not be allowed by the throttler")
}

func TestSetGetTag(t *testing.T) {
opentracer, tc := NewTracer("x", NewConstSampler(true), NewNullReporter())
tracer := opentracer.(*Tracer)
defer tc.Close()
value, ok := tracer.getTag(TracerIPTagKey)
assert.True(t, ok)
_, ok = value.(string)
assert.True(t, ok)
assert.True(t, tracer.hostIPv4 != 0)

ipStr := "11.22.33.44"
opentracer, tc = NewTracer("x", NewConstSampler(true), NewNullReporter(), TracerOptions.Tag(TracerIPTagKey, ipStr))
tracer = opentracer.(*Tracer)
defer tc.Close()
value, ok = tracer.getTag(TracerIPTagKey)
assert.True(t, ok)
assert.True(t, value == ipStr)
assert.True(t, tracer.hostIPv4 != 0)

ipStrInvalid := "an invalid input"
opentracer, tc = NewTracer("x", NewConstSampler(true), NewNullReporter(), TracerOptions.Tag(TracerIPTagKey, ipStrInvalid))
tracer = opentracer.(*Tracer)
defer tc.Close()
value, ok = tracer.getTag(TracerIPTagKey)
assert.True(t, ok)
assert.True(t, value == ipStrInvalid)
assert.True(t, tracer.hostIPv4 == 0)
}

type dummyPropagator struct{}
type dummyCarrier struct {
ok bool
Expand Down

0 comments on commit e895c7a

Please sign in to comment.