Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-9916] Add current semantic convention attributes to spans for GQL #33

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions semconv/v1.0.0/graphql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package semconv

import (
"github.com/TykTechnologies/opentelemetry/trace"
"go.opentelemetry.io/otel/attribute"
)

const (
// GraphQLOperationPrefix is the base prefix for all the GraphQL operation attributes
GraphQLOperationPrefix = "operation."
)

const (
// GraphQLOperationNameKey represents the name of the operation being executed.
GraphQLOperationNameKey = attribute.Key(GraphQLOperationPrefix + "name")

// GraphQLOperationTypeKey The type of the operation being executed.
GraphQLOperationTypeKey = attribute.Key(GraphQLOperationPrefix + "type")
)

const (
// GraphQLDocumentKey represents The GraphQL document being executed.
GraphQLDocumentKey = attribute.Key("document")
)

// GraphQLOperationName returns an attribute KeyValue conforming to the
// "operation.name" semantic convention.
func GraphQLOperationName(name string) trace.Attribute {
return GraphQLOperationNameKey.String(name)
}

// GraphQLOperationType returns an attribute KeyValue conforming to the
// "operation.type" semantic convention.
func GraphQLOperationType(operationType string) trace.Attribute {
return GraphQLOperationTypeKey.String(operationType)
}

// GraphQLDocument returns an attribute KeyValue conforming to the
// "document" semantic convention.
func GraphQLDocument(document string) trace.Attribute {
return GraphQLDocumentKey.String(document)
}
29 changes: 29 additions & 0 deletions semconv/v1.0.0/graphql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package semconv

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
)

func TestGraphQLOperationName(t *testing.T) {
name := "MyQuery"
expectedAttribute := attribute.Key(GraphQLOperationPrefix + "name").String(name)
actualAttribute := GraphQLOperationName(name)
assert.Equal(t, expectedAttribute, actualAttribute, "The attributes should be equal")
}

func TestGraphQLOperationType(t *testing.T) {
operationType := "mutation"
expectedAttribute := attribute.Key(GraphQLOperationPrefix + "type").String(operationType)
actualAttribute := GraphQLOperationType(operationType)
assert.Equal(t, expectedAttribute, actualAttribute, "The attributes should be equal")
}

func TestGraphQLDocument(t *testing.T) {
document := "query{}"
expectedAttribute := GraphQLDocumentKey.String(document)
actualAttribute := GraphQLDocument(document)
assert.Equal(t, expectedAttribute, actualAttribute, "The attributes should be equal")
}
Loading