Skip to content

Commit 8aad626

Browse files
authored
Merge pull request #112386 from ggailey777/brett
Adding v2 custom Javascript telemetry sample to use traceContext
2 parents 5efc3be + 3e6504c commit 8aad626

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

articles/azure-functions/functions-monitoring.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ Don't set `telemetryClient.Context.Operation.Id`. This global setting causes inc
529529

530530
## Log custom telemetry in JavaScript functions
531531

532-
Here is a sample code snippet that sends custom telemetry with the [Application Insights Node.js SDK](https://github.com/microsoft/applicationinsights-node.js):
532+
Here are sample code snippets that sends custom telemetry with the [Application Insights Node.js SDK](https://github.com/microsoft/applicationinsights-node.js):
533+
534+
### Version 2.x and later
533535

534536
```javascript
535537
const appInsights = require("applicationinsights");
@@ -539,12 +541,39 @@ const client = appInsights.defaultClient;
539541
module.exports = function (context, req) {
540542
context.log('JavaScript HTTP trigger function processed a request.');
541543

542-
client.trackEvent({name: "my custom event", tagOverrides:{"ai.operation.id": context.invocationId}, properties: {customProperty2: "custom property value"}});
543-
client.trackException({exception: new Error("handled exceptions can be logged with this method"), tagOverrides:{"ai.operation.id": context.invocationId}});
544-
client.trackMetric({name: "custom metric", value: 3, tagOverrides:{"ai.operation.id": context.invocationId}});
545-
client.trackTrace({message: "trace message", tagOverrides:{"ai.operation.id": context.invocationId}});
546-
client.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL", tagOverrides:{"ai.operation.id": context.invocationId}});
547-
client.trackRequest({name:"GET /customers", url:"http://myserver/customers", duration:309, resultCode:200, success:true, tagOverrides:{"ai.operation.id": context.invocationId}});
544+
// Use this with 'tagOverrides' to correlate custom telemetry to the parent function invocation.
545+
var operationIdOverride = {"ai.operation.id":context.traceContext.traceparent};
546+
547+
client.trackEvent({name: "my custom event", tagOverrides:operationIdOverride, properties: {customProperty2: "custom property value"}});
548+
client.trackException({exception: new Error("handled exceptions can be logged with this method"), tagOverrides:operationIdOverride);
549+
client.trackMetric({name: "custom metric", value: 3, tagOverrides:operationIdOverride});
550+
client.trackTrace({message: "trace message", tagOverrides:operationIdOverride});
551+
client.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL", tagOverrides:operationIdOverride});
552+
client.trackRequest({name:"GET /customers", url:"http://myserver/customers", duration:309, resultCode:200, success:true, tagOverrides:operationIdOverride});
553+
554+
context.done();
555+
};
556+
```
557+
558+
### Version 1.x
559+
560+
```javascript
561+
const appInsights = require("applicationinsights");
562+
appInsights.setup();
563+
const client = appInsights.defaultClient;
564+
565+
module.exports = function (context, req) {
566+
context.log('JavaScript HTTP trigger function processed a request.');
567+
568+
// Use this with 'tagOverrides' to correlate custom telemetry to the parent function invocation.
569+
var operationIdOverride = {"ai.operation.id":context.operationId};
570+
571+
client.trackEvent({name: "my custom event", tagOverrides:operationIdOverride, properties: {customProperty2: "custom property value"}});
572+
client.trackException({exception: new Error("handled exceptions can be logged with this method"), tagOverrides:operationIdOverride);
573+
client.trackMetric({name: "custom metric", value: 3, tagOverrides:operationIdOverride});
574+
client.trackTrace({message: "trace message", tagOverrides:operationIdOverride});
575+
client.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL", tagOverrides:operationIdOverride});
576+
client.trackRequest({name:"GET /customers", url:"http://myserver/customers", duration:309, resultCode:200, success:true, tagOverrides:operationIdOverride});
548577

549578
context.done();
550579
};

0 commit comments

Comments
 (0)