Skip to content

Commit

Permalink
contrib/99designs/gqlgen/tracer.go: nil check response (DataDog#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
dienvoandpadcojp authored and MNThomson committed Oct 21, 2024
1 parent 6e8e8e6 commit 34980fa
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
23 changes: 15 additions & 8 deletions contrib/99designs/gqlgen/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,21 @@ func (t *gqlTracer) InterceptOperation(ctx context.Context, next graphql.Operati
}
defer span.Finish(tracer.WithError(err))
}
query.Finish(graphqlsec.ExecutionOperationRes{
Data: response.Data, // NB - This is raw data, but rather not parse it (possibly expensive).
Error: response.Errors,
})
req.Finish(span, graphqlsec.RequestOperationRes{
Data: response.Data, // NB - This is raw data, but rather not parse it (possibly expensive).
Error: response.Errors,
})

var (
executionOperationRes graphqlsec.ExecutionOperationRes
requestOperationRes graphqlsec.RequestOperationRes
)
if response != nil {
executionOperationRes.Data = response.Data
executionOperationRes.Error = response.Errors

requestOperationRes.Data = response.Data
requestOperationRes.Error = response.Errors
}

query.Finish(executionOperationRes)
req.Finish(span, requestOperationRes)
return response
}
}
Expand Down
88 changes: 88 additions & 0 deletions contrib/99designs/gqlgen/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,91 @@ func newTestClient(t *testing.T, h *testserver.TestServer, tracer graphql.Handle
h.Use(tracer)
return client.New(h)
}

func TestInterceptOperation(t *testing.T) {
assertions := assert.New(t)
graphqlTestSrv := testserver.New()
c := newTestClient(t, graphqlTestSrv, NewTracer())

t.Run("intercept operation with graphQL Query", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

err := c.Post(`{ name }`, &testServerResponse{})
assertions.Nil(err)

allSpans := mt.FinishedSpans()
var root mocktracer.Span
var resNames []string
var opNames []string
for _, span := range allSpans {
if span.ParentID() == 0 {
root = span
}
resNames = append(resNames, span.Tag(ext.ResourceName).(string))
opNames = append(opNames, span.OperationName())
assertions.Equal("99designs/gqlgen", span.Tag(ext.Component))
}
assertions.ElementsMatch(resNames, []string{readOp, parsingOp, validationOp, "Query.name", `{ name }`})
assertions.ElementsMatch(opNames, []string{readOp, parsingOp, validationOp, fieldOp, "graphql.query"})
assertions.NotNil(root)
assertions.Nil(root.Tag(ext.Error))
})

t.Run("intercept operation with graphQL Mutation", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

err := c.Post(`mutation Name { name }`, &testServerResponse{})
// due to testserver.New() implementation, mutation is not supported
assertions.NotNil(err)

allSpans := mt.FinishedSpans()
var root mocktracer.Span
var resNames []string
var opNames []string
for _, span := range allSpans {
if span.ParentID() == 0 {
root = span
}
resNames = append(resNames, span.Tag(ext.ResourceName).(string))
opNames = append(opNames, span.OperationName())
assertions.Equal("99designs/gqlgen", span.Tag(ext.Component))
}
assertions.ElementsMatch(resNames, []string{readOp, parsingOp, validationOp, `mutation Name { name }`})
assertions.ElementsMatch(opNames, []string{readOp, parsingOp, validationOp, "graphql.mutation"})
assertions.NotNil(root)
assertions.NotNil(root.Tag(ext.Error))
})

t.Run("intercept operation with graphQL Subscription", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

go func() {
graphqlTestSrv.SendCompleteSubscriptionMessage()
}()

// using raw post because post try to access nil response's Data field
resp, err := c.RawPost(`subscription Name { name }`)
assertions.Nil(err)
assertions.Nil(resp)

allSpans := mt.FinishedSpans()
var root mocktracer.Span
var resNames []string
var opNames []string
for _, span := range allSpans {
if span.ParentID() == 0 {
root = span
}
resNames = append(resNames, span.Tag(ext.ResourceName).(string))
opNames = append(opNames, span.OperationName())
assertions.Equal("99designs/gqlgen", span.Tag(ext.Component))
}
assertions.ElementsMatch(resNames, []string{`subscription Name { name }`, `subscription Name { name }`, "subscription Name { name }"})
assertions.ElementsMatch(opNames, []string{readOp, parsingOp, validationOp})
assertions.NotNil(root)
assertions.Nil(root.Tag(ext.Error))
})
}

0 comments on commit 34980fa

Please sign in to comment.