Skip to content

Commit f7ec08d

Browse files
committed
Require non-empty values in SetDestinationService
SetDestinationService must not send an empty Name if a non-empty Resource is specified (and vice versa), as this will lead to a validation error in apm-server. Change SetDestinationService to set neither if either one is empty.
1 parent f68715a commit f7ec08d

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

spancontext.go

+6
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ func (c *SpanContext) SetDestinationAddress(addr string, port int) {
197197
}
198198

199199
// SetDestinationService sets the destination service info in the context.
200+
//
201+
// Both service.Name and service.Resource are required. If either is empty,
202+
// then SetDestinationService is a no-op.
200203
func (c *SpanContext) SetDestinationService(service DestinationServiceSpanContext) {
204+
if service.Name == "" || service.Resource == "" {
205+
return
206+
}
201207
c.destinationService.Name = truncateString(service.Name)
202208
c.destinationService.Resource = truncateString(service.Resource)
203209
c.destination.Service = &c.destinationService

spancontext_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apm_test
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"net/http"
2324
"net/url"
2425
"testing"
@@ -122,3 +123,52 @@ func TestSpanContextSetHTTPRequest(t *testing.T) {
122123
})
123124
}
124125
}
126+
127+
func TestSetDestinationService(t *testing.T) {
128+
type testcase struct {
129+
name string
130+
resource string
131+
expectEmpty bool
132+
}
133+
134+
testcases := []testcase{{
135+
name: "",
136+
resource: "",
137+
expectEmpty: true,
138+
}, {
139+
name: "",
140+
resource: "nonempty",
141+
expectEmpty: true,
142+
}, {
143+
name: "nonempty",
144+
resource: "",
145+
expectEmpty: true,
146+
}, {
147+
name: "nonempty",
148+
resource: "nonempty",
149+
}}
150+
151+
for _, tc := range testcases {
152+
t.Run(fmt.Sprintf("%s_%s", tc.name, tc.resource), func(t *testing.T) {
153+
_, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
154+
span, _ := apm.StartSpan(ctx, "name", "span_type")
155+
span.Context.SetDestinationAddress("testing.invalid", 123)
156+
span.Context.SetDestinationService(apm.DestinationServiceSpanContext{
157+
Name: tc.name,
158+
Resource: tc.resource,
159+
})
160+
span.End()
161+
})
162+
require.Len(t, spans, 1)
163+
if tc.expectEmpty {
164+
assert.Nil(t, spans[0].Context.Destination.Service)
165+
} else {
166+
assert.Equal(t, &model.DestinationServiceSpanContext{
167+
Name: tc.name,
168+
Resource: tc.resource,
169+
Type: "span_type",
170+
}, spans[0].Context.Destination.Service)
171+
}
172+
})
173+
}
174+
}

validation_test.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,18 @@ func TestValidateDatabaseSpanContext(t *testing.T) {
118118
}
119119

120120
func TestValidateDestinationSpanContext(t *testing.T) {
121-
validateSpan(t, func(s *apm.Span) {
122-
s.Context.SetDestinationAddress(strings.Repeat("x", 1025), 0)
123-
s.Context.SetDestinationService(apm.DestinationServiceSpanContext{
124-
Name: strings.Repeat("x", 1025),
125-
Resource: strings.Repeat("x", 1025),
126-
})
127-
})
121+
overlong := strings.Repeat("x", 1025)
122+
for _, name := range []string{"", overlong} {
123+
for _, resource := range []string{"", overlong} {
124+
validateSpan(t, func(s *apm.Span) {
125+
s.Context.SetDestinationAddress(overlong, 0)
126+
s.Context.SetDestinationService(apm.DestinationServiceSpanContext{
127+
Name: name,
128+
Resource: resource,
129+
})
130+
})
131+
}
132+
}
128133
}
129134

130135
func TestValidateContextUser(t *testing.T) {

0 commit comments

Comments
 (0)