-
Notifications
You must be signed in to change notification settings - Fork 140
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
Add back Azure Functions correlation context hook #1070
Conversation
AutoCollection/AzureFunctionsHook.ts
Outdated
|
||
private _createIncomingRequestTelemetry(ctx: Context, request: HttpRequest, startTime: number, parentId: string) { | ||
let statusCode = 200; //Default | ||
if (ctx.res) { |
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.
It's annoying, but there's actually three ways for users to set the response. Instead of just using ctx.res
, here's a method I would use to get the response:
private _getResponse(postInvocationContext: PostInvocationContext, ctx: Context): HttpResponse {
const httpOutputBinding = ctx.bindingDefinitions.find(b => b.direction === "out" && b.type.toLowerCase() === "http");
if (httpOutputBinding.name === "$return") {
return postInvocationContext.result;
} else if (ctx.bindings[httpOutputBinding.name] !== undefined) {
return ctx.bindings[httpOutputBinding.name];
} else {
return ctx.res;
}
}
If you're curious, here is sample code for each way you can set the response. (Also documented here to some extent)
Option 1
index.ts:
context.res = {
// status: 200, /* Defaults to 200 */
body: 'Hello context.res',
statusCode: 201,
};
function.json:
{
"type": "http",
"direction": "out",
"name": "canBeAnything" // This is ignored if they use `context.res`
}
Option 2
index.ts:
context.bindings.res123 = {
// status: 200, /* Defaults to 200 */
body: 'Hello res123 binding',
statusCode: 202,
};
function.json:
{
"type": "http",
"direction": "out",
"name": "res123" // must match if they use `context.bindings`
}
Option 3
index.ts:
return {
// status: 200, /* Defaults to 200 */
body: 'Hello return',
statusCode: 203,
};
function.json:
{
"type": "http",
"direction": "out",
"name": "$return"
}
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.
🎉
No description provided.