@@ -529,7 +529,9 @@ Don't set `telemetryClient.Context.Operation.Id`. This global setting causes inc
529
529
530
530
## Log custom telemetry in JavaScript functions
531
531
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
533
535
534
536
```javascript
535
537
const appInsights = require (" applicationinsights" );
@@ -539,12 +541,39 @@ const client = appInsights.defaultClient;
539
541
module .exports = function (context , req ) {
540
542
context .log ('JavaScript HTTP trigger function processed a request.' );
541
543
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 });
548
577
549
578
context .done ();
550
579
};
0 commit comments