-
Notifications
You must be signed in to change notification settings - Fork 539
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
fix(detector-gpc): suppress tracing when requesting for GCP metadata #2321
Changes from 7 commits
04f0f40
dda1a0d
22f4e99
6e4cd4b
d8bcefd
24f8532
1dbe92f
0aaaf29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. nit: missing license header, and mixed 4- and 2-space indents in this file. Unfortunately the eslint setup doesn't lint .js files. 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. thanks! maybe we could also make eslint work in js (but not in this PR) 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. Definite not this PR. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { createTestNodeSdk } = require ('@opentelemetry/contrib-test-utils'); | ||
const { HttpInstrumentation } = require ('@opentelemetry/instrumentation-http'); | ||
const { gcpDetector } = require ('../../build/src/index.js'); | ||
|
||
|
||
const sdk = createTestNodeSdk({ | ||
serviceName: 'use-detector-gcp', | ||
instrumentations: [ | ||
new HttpInstrumentation(), | ||
], | ||
resourceDetectors: [gcpDetector], | ||
}); | ||
|
||
sdk.start(); | ||
|
||
const http = require('http'); | ||
|
||
const server = http.createServer((req,res) => { | ||
console.log('incoming request: %s %s %s', req.method, req.url, req.headers); | ||
|
||
req.resume(); | ||
req.on('end', function () { | ||
const body = 'pong'; | ||
res.writeHead(200, { | ||
'content-type': 'text/plain', | ||
'content-length': body.length, | ||
}); | ||
res.end(body); | ||
}); | ||
}); | ||
|
||
server.listen(0, '127.0.0.1', async function () { | ||
const port = server.address().port; | ||
|
||
// First request to show a client error. | ||
const startTime = Date.now(); | ||
await new Promise((resolve) => { | ||
const clientReq = http.request( | ||
`http://127.0.0.1:${port}/ping`, | ||
function (cres) { | ||
console.log( | ||
'client response: %s %s', | ||
cres.statusCode, | ||
cres.headers | ||
); | ||
const chunks = []; | ||
cres.on('data', function (chunk) { | ||
chunks.push(chunk); | ||
}); | ||
cres.on('end', function () { | ||
const body = chunks.join(''); | ||
console.log('client response body: %j', body); | ||
resolve(); | ||
}); | ||
} | ||
); | ||
clientReq.write('ping'); | ||
clientReq.end(); | ||
}); | ||
|
||
// flush any left spans | ||
await sdk.shutdown(); | ||
await new Promise(resolve => server.close(resolve)); | ||
}); |
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.
(I guess I forgot to hit "save" in my first review.)
Wow this was a fair amount of effort to write a test case for this.
I don't know how others would feel, but I wouldn't require similar test code be added for suppressTracing usage in other detectors.
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.
Yup it was a fair amount of work 😓. But I needed
sdk
running so I had to create the fixturenock
since real requests won't happen so we won't be really testinggcp-metadata
to get responses. I could let it fail but I found that the request time vary in a way that makes the test flaky.there is another approach I can take but 1st I wanted some feeedback for this one. If we finally use this one I may be inclined to move the mock server to
@opentelemetry/contrib-test-utils
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.
About the 3rd point in my previous comment I found that if the async attributes of the resource are resolved after calling
sdk.shutdown()
we loose some spans. Without knowing to much about the mechanics of the processors/exporters I'd guess that these spans are not yet in the queue that is flushed during shutdown. I think this could be a potential issue for apps that have a short execution time (thinking on lamba or similar)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.
@trentm the other approach seems to work and it's shorter 🎉 (although we're getting a resource with empty values). Changes are
waitForAsyncAttributes
A couple of observations:
I'm keen to use this simplified way but it is possible we have to do the server mock for other detectors (I hope not). WDYT?