-
Notifications
You must be signed in to change notification settings - Fork 238
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
feat: base opentelemetry integration #363
Open
PacoDu
wants to merge
4
commits into
mercurius-js:master
Choose a base branch
from
PacoDu:feat-base-opentelemetry-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9cb48c7
feat(opentelemetry): implement basic tracer, span and cached attribute
238b680
test(opentelemetry): should add span and cached attribute
f52a850
test(opentelemetry): refine span context testing
ac6a9e9
test(opentelemetry): remove unused span context from TestTracer
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
const api = require('@opentelemetry/api') | ||
const meta = require('../package.json') | ||
|
||
module.exports = api.trace.getTracer(meta.name, meta.version) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"@graphql-tools/merge": "^6.2.4", | ||
"@graphql-tools/schema": "^6.2.4", | ||
"@graphql-tools/utils": "^6.2.4", | ||
"@opentelemetry/api": "^0.13.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"@sinonjs/fake-timers": "^6.0.1", | ||
"@types/node": "^14.0.23", | ||
"@types/ws": "^7.2.6", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict' | ||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const api = require('@opentelemetry/api') | ||
const GQL = require('..') | ||
|
||
test('Should add opentelemetry span and cached attribute', async (t) => { | ||
t.plan(6) | ||
|
||
const testSpanContext = { | ||
traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
spanId: '6e0c63257de34c92', | ||
traceFlags: api.TraceFlags.NONE | ||
} | ||
class TestSpan extends api.NoopSpan { | ||
setAttribute (name, value) { | ||
t.is(name, 'mercurius.cached') | ||
t.is(value, false) | ||
} | ||
|
||
end () { | ||
t.pass() | ||
} | ||
} | ||
const dummySpan = new TestSpan(testSpanContext) | ||
class TestTracer extends api.NoopTracer { | ||
startSpan (name, opts) { | ||
t.is(name, 'mercurius - graphql') | ||
t.deepEquals(opts.parent.context(), dummySpan.context()) | ||
return new TestSpan() | ||
} | ||
|
||
getCurrentSpan () { | ||
return dummySpan | ||
} | ||
} | ||
class TestTracerProvider extends api.NoopTracerProvider { | ||
getTracer () { | ||
return new TestTracer() | ||
} | ||
} | ||
api.trace.setGlobalTracerProvider(new TestTracerProvider()) | ||
|
||
const app = Fastify() | ||
const schema = ` | ||
type Query { | ||
add(x: Int, y: Int): Int | ||
} | ||
` | ||
|
||
const resolvers = { | ||
add: async ({ x, y }) => x + y | ||
} | ||
|
||
app.register(GQL, { | ||
schema, | ||
resolvers | ||
}) | ||
|
||
app.get('/', async function (req, reply) { | ||
const query = '{ add(x: 2, y: 2) }' | ||
return reply.graphql(query) | ||
}) | ||
|
||
const res = await app.inject({ | ||
method: 'GET', | ||
url: '/' | ||
}) | ||
|
||
t.deepEqual(JSON.parse(res.body), { | ||
data: { | ||
add: 4 | ||
} | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually on erros error info is added to span as status code
As well as
OK
status on success.Also to avoid multiple
span.end()
I prefer following pattern:But it might be little bit less performant. And this one is available in most recent opentelemetry API, not sure about version from package.json.