diff --git a/module/apmgrpc/client.go b/module/apmgrpc/client.go index 6f479b163..9c339eb23 100644 --- a/module/apmgrpc/client.go +++ b/module/apmgrpc/client.go @@ -83,6 +83,9 @@ func NewUnaryClientInterceptor(o ...ClientOption) grpc.UnaryClientInterceptor { Resource: url.Host, }) } + span.Context.SetServiceTarget(apm.ServiceTargetSpanContext{ + Name: url.Host, + }) } return err } diff --git a/span.go b/span.go index 58e1e356b..d837df9e5 100644 --- a/span.go +++ b/span.go @@ -381,11 +381,9 @@ func (s *Span) End() { // manually set the destination.service.resource s.setExitSpanDestinationService() } - if s.exit { - // The span was created as an exit span, but the user did not - // manually set the service.target fields. - s.setExitSpanServiceTarget() - } + + s.updateSpanServiceTarget() + if s.Duration < 0 { s.Duration = time.Since(s.timestamp) } @@ -526,7 +524,13 @@ func (s *Span) setExitSpanDestinationService() { }) } -func (s *Span) setExitSpanServiceTarget() { +func (s *Span) updateSpanServiceTarget() { + if !s.exit { + // span.context.service.target.* fields should be omitted for non-exit spans. + s.Context.model.Service = nil + return + } + fallbackType := s.Subtype if fallbackType == "" { fallbackType = s.Type diff --git a/spancontext.go b/spancontext.go index 61d6ad36b..38ced9261 100644 --- a/spancontext.go +++ b/spancontext.go @@ -222,6 +222,9 @@ func (c *SpanContext) SetHTTPRequest(req *http.Request) { Name: destinationServiceURL.String(), Resource: destinationServiceResource, }) + c.SetServiceTarget(ServiceTargetSpanContext{ + Name: destinationServiceResource, + }) } // SetHTTPStatusCode records the HTTP response status code. diff --git a/spancontext_test.go b/spancontext_test.go index 80cd7393a..2c3805bc1 100644 --- a/spancontext_test.go +++ b/spancontext_test.go @@ -105,7 +105,9 @@ func TestSpanContextSetHTTPRequest(t *testing.T) { require.NoError(t, err) _, spans, _ := apmtest.WithTransaction(func(ctx context.Context) { - span, _ := apm.StartSpan(ctx, "name", "type") + span, _ := apm.StartSpanOptions(ctx, "name", "http", apm.SpanOptions{ + ExitSpan: true, + }) span.Context.SetHTTPRequest(&http.Request{URL: url}) span.End() }) @@ -120,6 +122,10 @@ func TestSpanContextSetHTTPRequest(t *testing.T) { Resource: tc.resource, }, }, spans[0].Context.Destination) + assert.Equal(t, &model.ServiceTargetSpanContext{ + Type: "http", + Name: tc.resource, + }, spans[0].Context.Service.Target) }) } }