@azure/monitor-opentelemetry-exporter 1.0.0-beta.8 → 1.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +47 -9
  2. package/dist/index.js +294 -134
  3. package/dist-esm/src/config.js +1 -14
  4. package/dist-esm/src/config.js.map +1 -1
  5. package/dist-esm/src/export/base.js +20 -18
  6. package/dist-esm/src/export/base.js.map +1 -1
  7. package/dist-esm/src/export/metric.js +22 -8
  8. package/dist-esm/src/export/metric.js.map +1 -1
  9. package/dist-esm/src/export/trace.js +12 -1
  10. package/dist-esm/src/export/trace.js.map +1 -1
  11. package/dist-esm/src/generated/applicationInsightsClient.js +36 -3
  12. package/dist-esm/src/generated/applicationInsightsClient.js.map +1 -1
  13. package/dist-esm/src/generated/index.js +0 -1
  14. package/dist-esm/src/generated/index.js.map +1 -1
  15. package/dist-esm/src/generated/models/index.js +35 -0
  16. package/dist-esm/src/generated/models/index.js.map +1 -1
  17. package/dist-esm/src/index.js +1 -0
  18. package/dist-esm/src/index.js.map +1 -1
  19. package/dist-esm/src/platform/nodejs/httpSender.js +5 -8
  20. package/dist-esm/src/platform/nodejs/httpSender.js.map +1 -1
  21. package/dist-esm/src/platform/nodejs/persist/fileSystemHelpers.js +2 -1
  22. package/dist-esm/src/platform/nodejs/persist/fileSystemHelpers.js.map +1 -1
  23. package/dist-esm/src/platform/nodejs/persist/fileSystemPersist.js +10 -5
  24. package/dist-esm/src/platform/nodejs/persist/fileSystemPersist.js.map +1 -1
  25. package/dist-esm/src/sampling.js +81 -0
  26. package/dist-esm/src/sampling.js.map +1 -0
  27. package/dist-esm/src/utils/breezeUtils.js +3 -1
  28. package/dist-esm/src/utils/breezeUtils.js.map +1 -1
  29. package/dist-esm/src/utils/constants/applicationinsights.js +2 -1
  30. package/dist-esm/src/utils/constants/applicationinsights.js.map +1 -1
  31. package/dist-esm/src/utils/metricUtils.js +34 -20
  32. package/dist-esm/src/utils/metricUtils.js.map +1 -1
  33. package/dist-esm/src/utils/spanUtils.js +21 -11
  34. package/dist-esm/src/utils/spanUtils.js.map +1 -1
  35. package/package.json +14 -14
  36. package/types/monitor-opentelemetry-exporter.d.ts +94 -24
  37. package/dist-esm/src/generated/applicationInsightsClientContext.js +0 -34
  38. package/dist-esm/src/generated/applicationInsightsClientContext.js.map +0 -1
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
5
6
  var api = require('@opentelemetry/api');
6
7
  var core = require('@opentelemetry/core');
7
8
  var url = require('url');
@@ -13,7 +14,7 @@ var path = require('path');
13
14
  var child_process = require('child_process');
14
15
  var util = require('util');
15
16
  var semanticConventions = require('@opentelemetry/semantic-conventions');
16
- var sdkMetricsBase = require('@opentelemetry/sdk-metrics-base');
17
+ var sdkMetrics = require('@opentelemetry/sdk-metrics');
17
18
 
18
19
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
20
 
@@ -36,6 +37,7 @@ function _interopNamespace(e) {
36
37
  }
37
38
 
38
39
  var url__default = /*#__PURE__*/_interopDefaultLegacy(url);
40
+ var coreRestPipeline__namespace = /*#__PURE__*/_interopNamespace(coreRestPipeline);
39
41
  var coreClient__namespace = /*#__PURE__*/_interopNamespace(coreClient);
40
42
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
41
43
  var os__namespace = /*#__PURE__*/_interopNamespace(os);
@@ -43,6 +45,117 @@ var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
43
45
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
44
46
  var child_process__namespace = /*#__PURE__*/_interopNamespace(child_process);
45
47
 
48
+ // Copyright (c) Microsoft Corporation.
49
+ // Licensed under the MIT license.
50
+ /**
51
+ * AI MS Links.
52
+ * @internal
53
+ */
54
+ const MS_LINKS = "_MS.links";
55
+ /**
56
+ * AI enqueued time attribute.
57
+ * @internal
58
+ */
59
+ const ENQUEUED_TIME = "enqueuedTime";
60
+ /**
61
+ * AI time since enqueued attribute.
62
+ * @internal
63
+ */
64
+ const TIME_SINCE_ENQUEUED = "timeSinceEnqueued";
65
+ /**
66
+ * AzureMonitorTraceExporter version.
67
+ * @internal
68
+ */
69
+ const packageVersion = "1.0.0-beta.9";
70
+ var DependencyTypes;
71
+ (function (DependencyTypes) {
72
+ DependencyTypes["InProc"] = "InProc";
73
+ DependencyTypes["QueueMessage"] = "Queue Message";
74
+ DependencyTypes["Sql"] = "SQL";
75
+ DependencyTypes["Http"] = "Http";
76
+ DependencyTypes["Grpc"] = "GRPC";
77
+ })(DependencyTypes || (DependencyTypes = {}));
78
+ const AzureMonitorSampleRate = "_MS.sampleRate";
79
+
80
+ /**
81
+ * ApplicationInsightsSampler is responsible for the following:
82
+ * Implements same trace id hashing algorithm so that traces are sampled the same across multiple nodes
83
+ * Adds item count to span attribute if span is sampled (needed for ingestion service)
84
+ * @param samplingRatio - 0 to 1 value.
85
+ */
86
+ class ApplicationInsightsSampler {
87
+ constructor(samplingRatio = 1) {
88
+ this._samplingRatio = samplingRatio;
89
+ if (this._samplingRatio > 1) {
90
+ throw new Error("Wrong sampling rate, data will not be sampled out");
91
+ }
92
+ this._sampleRate = Math.round(this._samplingRatio * 100);
93
+ }
94
+ /**
95
+ * Checks whether span needs to be created and tracked.
96
+ *
97
+ * @param context Parent Context which may contain a span.
98
+ * @param traceId of the span to be created. It can be different from the
99
+ * traceId in the {@link SpanContext}. Typically in situations when the
100
+ * span to be created starts a new trace.
101
+ * @param spanName of the span to be created.
102
+ * @param spanKind of the span to be created.
103
+ * @param attributes Initial set of SpanAttributes for the Span being constructed.
104
+ * @param links Collection of links that will be associated with the Span to
105
+ * be created. Typically useful for batch operations.
106
+ * @returns a {@link SamplingResult}.
107
+ */
108
+ shouldSample(
109
+ // @ts-ignore
110
+ context, traceId,
111
+ // @ts-ignore
112
+ spanName,
113
+ // @ts-ignore
114
+ spanKind, attributes,
115
+ // @ts-ignore
116
+ links) {
117
+ let isSampledIn = false;
118
+ if (this._sampleRate == 100) {
119
+ isSampledIn = true;
120
+ }
121
+ else if (this._sampleRate == 0) {
122
+ isSampledIn = false;
123
+ }
124
+ else {
125
+ isSampledIn = this._getSamplingHashCode(traceId) < this._sampleRate;
126
+ }
127
+ // Add sample rate as span attribute
128
+ attributes = attributes || {};
129
+ attributes[AzureMonitorSampleRate] = this._sampleRate;
130
+ return isSampledIn
131
+ ? { decision: sdkTraceBase.SamplingDecision.RECORD_AND_SAMPLED, attributes: attributes }
132
+ : { decision: sdkTraceBase.SamplingDecision.NOT_RECORD, attributes: attributes };
133
+ }
134
+ /**
135
+ * Return Sampler description
136
+ */
137
+ toString() {
138
+ return `ApplicationInsightsSampler{${this._samplingRatio}}`;
139
+ }
140
+ _getSamplingHashCode(input) {
141
+ var csharpMin = -2147483648;
142
+ var csharpMax = 2147483647;
143
+ var hash = 5381;
144
+ if (!input) {
145
+ return 0;
146
+ }
147
+ while (input.length < 8) {
148
+ input = input + input;
149
+ }
150
+ for (var i = 0; i < input.length; i++) {
151
+ // JS doesn't respond to integer overflow by wrapping around. Simulate it with bitwise operators ( | 0)
152
+ hash = ((((hash << 5) + hash) | 0) + input.charCodeAt(i)) | 0;
153
+ }
154
+ hash = hash <= csharpMin ? csharpMax : Math.abs(hash);
155
+ return (hash / csharpMax) * 100;
156
+ }
157
+ }
158
+
46
159
  // Copyright (c) Microsoft Corporation.
47
160
  // Licensed under the MIT license.
48
161
  /**
@@ -64,7 +177,7 @@ const DEFAULT_BREEZE_ENDPOINT = "https://dc.services.visualstudio.com";
64
177
  * Default Breeze API version.
65
178
  * @internal
66
179
  */
67
- const DEFAULT_BREEZE_API_VERSION = exports.ServiceApiVersion.V2;
180
+ exports.ServiceApiVersion.V2;
68
181
  /**
69
182
  * Default Live Metrics endpoint.
70
183
  * @internal
@@ -194,20 +307,6 @@ class ConnectionStringParser {
194
307
  ConnectionStringParser.FIELDS_SEPARATOR = ";";
195
308
  ConnectionStringParser.FIELD_KEY_VALUE_SEPARATOR = "=";
196
309
 
197
- const DEFAULT_BATCH_SEND_RETRY_INTERVAL_MS = 60000;
198
- const DEFAULT_MAX_CONSECUTIVE_FAILURES_BEFORE_WARNING = 10;
199
- /**
200
- * Internal default Azure exporter configuration
201
- * @internal
202
- */
203
- const DEFAULT_EXPORTER_CONFIG = {
204
- instrumentationKey: "",
205
- endpointUrl: DEFAULT_BREEZE_ENDPOINT,
206
- batchSendRetryIntervalMs: DEFAULT_BATCH_SEND_RETRY_INTERVAL_MS,
207
- maxConsecutiveFailuresBeforeWarning: DEFAULT_MAX_CONSECUTIVE_FAILURES_BEFORE_WARNING,
208
- apiVersion: DEFAULT_BREEZE_API_VERSION,
209
- };
210
-
211
310
  // Copyright (c) Microsoft Corporation.
212
311
  class FileAccessControl {
213
312
  // Check if file access control could be enabled
@@ -410,7 +509,8 @@ const confirmDirExists = async (directory) => {
410
509
  catch (err) {
411
510
  if (err && err.code === "ENOENT") {
412
511
  try {
413
- await mkdirAsync(directory);
512
+ const options = { recursive: true };
513
+ await mkdirAsync(directory, options);
414
514
  }
415
515
  catch (mkdirErr) {
416
516
  if (mkdirErr && mkdirErr.code !== "EEXIST") {
@@ -433,25 +533,31 @@ const writeFileAsync = util.promisify(fs__namespace.writeFile);
433
533
  * @internal
434
534
  */
435
535
  class FileSystemPersist {
436
- constructor(options = {}) {
536
+ constructor(instrumentationKey, _options) {
537
+ var _a, _b;
538
+ this._options = _options;
437
539
  this.fileRetemptionPeriod = 7 * 24 * 60 * 60 * 1000; // 7 days
438
540
  this.cleanupTimeOut = 60 * 60 * 1000; // 1 hour
439
541
  this.maxBytesOnDisk = 50000000; // ~50MB
440
542
  this._tempDirectory = "";
441
543
  this._fileCleanupTimer = null;
442
- this._options = Object.assign(Object.assign({}, DEFAULT_EXPORTER_CONFIG), options);
544
+ this._instrumentationKey = instrumentationKey;
545
+ if ((_a = this._options) === null || _a === void 0 ? void 0 : _a.disableOfflineStorage) {
546
+ this._enabled = false;
547
+ return;
548
+ }
443
549
  this._enabled = true;
444
550
  FileAccessControl.checkFileProtection();
445
551
  if (!FileAccessControl.OS_PROVIDES_FILE_PROTECTION) {
446
552
  this._enabled = false;
447
553
  api.diag.error("Sufficient file protection capabilities were not detected. Files will not be persisted");
448
554
  }
449
- if (!this._options.instrumentationKey) {
555
+ if (!this._instrumentationKey) {
450
556
  this._enabled = false;
451
557
  api.diag.error(`No instrumentation key was provided to FileSystemPersister. Files will not be persisted`);
452
558
  }
453
559
  if (this._enabled) {
454
- this._tempDirectory = path__namespace.join(os__namespace.tmpdir(), FileSystemPersist.TEMPDIR_PREFIX + this._options.instrumentationKey);
560
+ this._tempDirectory = path__namespace.join(((_b = this._options) === null || _b === void 0 ? void 0 : _b.storageDirectory) || os__namespace.tmpdir(), "Microsoft", "AzureMonitor", FileSystemPersist.TEMPDIR_PREFIX + this._instrumentationKey);
455
561
  // Starts file cleanup task
456
562
  if (!this._fileCleanupTimer) {
457
563
  this._fileCleanupTimer = setTimeout(() => {
@@ -597,48 +703,83 @@ FileSystemPersist.FILENAME_SUFFIX = ".ai.json";
597
703
  /** Known values of {@link DataPointType} that the service accepts. */
598
704
  var KnownDataPointType;
599
705
  (function (KnownDataPointType) {
706
+ /** Measurement */
600
707
  KnownDataPointType["Measurement"] = "Measurement";
708
+ /** Aggregation */
601
709
  KnownDataPointType["Aggregation"] = "Aggregation";
602
710
  })(KnownDataPointType || (KnownDataPointType = {}));
603
711
  /** Known values of {@link SeverityLevel} that the service accepts. */
604
712
  var KnownSeverityLevel;
605
713
  (function (KnownSeverityLevel) {
714
+ /** Verbose */
606
715
  KnownSeverityLevel["Verbose"] = "Verbose";
716
+ /** Information */
607
717
  KnownSeverityLevel["Information"] = "Information";
718
+ /** Warning */
608
719
  KnownSeverityLevel["Warning"] = "Warning";
720
+ /** Error */
609
721
  KnownSeverityLevel["Error"] = "Error";
722
+ /** Critical */
610
723
  KnownSeverityLevel["Critical"] = "Critical";
611
724
  })(KnownSeverityLevel || (KnownSeverityLevel = {}));
612
725
  /** Known values of {@link ContextTagKeys} that the service accepts. */
613
726
  var KnownContextTagKeys;
614
727
  (function (KnownContextTagKeys) {
728
+ /** AiApplicationVer */
615
729
  KnownContextTagKeys["AiApplicationVer"] = "ai.application.ver";
730
+ /** AiDeviceId */
616
731
  KnownContextTagKeys["AiDeviceId"] = "ai.device.id";
732
+ /** AiDeviceLocale */
617
733
  KnownContextTagKeys["AiDeviceLocale"] = "ai.device.locale";
734
+ /** AiDeviceModel */
618
735
  KnownContextTagKeys["AiDeviceModel"] = "ai.device.model";
736
+ /** AiDeviceOemName */
619
737
  KnownContextTagKeys["AiDeviceOemName"] = "ai.device.oemName";
738
+ /** AiDeviceOsVersion */
620
739
  KnownContextTagKeys["AiDeviceOsVersion"] = "ai.device.osVersion";
740
+ /** AiDeviceType */
621
741
  KnownContextTagKeys["AiDeviceType"] = "ai.device.type";
742
+ /** AiLocationIp */
622
743
  KnownContextTagKeys["AiLocationIp"] = "ai.location.ip";
744
+ /** AiLocationCountry */
623
745
  KnownContextTagKeys["AiLocationCountry"] = "ai.location.country";
746
+ /** AiLocationProvince */
624
747
  KnownContextTagKeys["AiLocationProvince"] = "ai.location.province";
748
+ /** AiLocationCity */
625
749
  KnownContextTagKeys["AiLocationCity"] = "ai.location.city";
750
+ /** AiOperationId */
626
751
  KnownContextTagKeys["AiOperationId"] = "ai.operation.id";
752
+ /** AiOperationName */
627
753
  KnownContextTagKeys["AiOperationName"] = "ai.operation.name";
754
+ /** AiOperationParentId */
628
755
  KnownContextTagKeys["AiOperationParentId"] = "ai.operation.parentId";
756
+ /** AiOperationSyntheticSource */
629
757
  KnownContextTagKeys["AiOperationSyntheticSource"] = "ai.operation.syntheticSource";
758
+ /** AiOperationCorrelationVector */
630
759
  KnownContextTagKeys["AiOperationCorrelationVector"] = "ai.operation.correlationVector";
760
+ /** AiSessionId */
631
761
  KnownContextTagKeys["AiSessionId"] = "ai.session.id";
762
+ /** AiSessionIsFirst */
632
763
  KnownContextTagKeys["AiSessionIsFirst"] = "ai.session.isFirst";
764
+ /** AiUserAccountId */
633
765
  KnownContextTagKeys["AiUserAccountId"] = "ai.user.accountId";
766
+ /** AiUserId */
634
767
  KnownContextTagKeys["AiUserId"] = "ai.user.id";
768
+ /** AiUserAuthUserId */
635
769
  KnownContextTagKeys["AiUserAuthUserId"] = "ai.user.authUserId";
770
+ /** AiCloudRole */
636
771
  KnownContextTagKeys["AiCloudRole"] = "ai.cloud.role";
772
+ /** AiCloudRoleVer */
637
773
  KnownContextTagKeys["AiCloudRoleVer"] = "ai.cloud.roleVer";
774
+ /** AiCloudRoleInstance */
638
775
  KnownContextTagKeys["AiCloudRoleInstance"] = "ai.cloud.roleInstance";
776
+ /** AiCloudLocation */
639
777
  KnownContextTagKeys["AiCloudLocation"] = "ai.cloud.location";
778
+ /** AiInternalSdkVersion */
640
779
  KnownContextTagKeys["AiInternalSdkVersion"] = "ai.internal.sdkVersion";
780
+ /** AiInternalAgentVersion */
641
781
  KnownContextTagKeys["AiInternalAgentVersion"] = "ai.internal.agentVersion";
782
+ /** AiInternalNodeName */
642
783
  KnownContextTagKeys["AiInternalNodeName"] = "ai.internal.nodeName";
643
784
  })(KnownContextTagKeys || (KnownContextTagKeys = {}));
644
785
 
@@ -1572,12 +1713,13 @@ var Mappers = /*#__PURE__*/Object.freeze({
1572
1713
  * Code generated by Microsoft (R) AutoRest Code Generator.
1573
1714
  * Changes may cause incorrect behavior and will be lost if the code is regenerated.
1574
1715
  */
1575
- class ApplicationInsightsClientContext extends coreClient__namespace.ServiceClient {
1716
+ class ApplicationInsightsClient extends coreClient__namespace.ServiceClient {
1576
1717
  /**
1577
- * Initializes a new instance of the ApplicationInsightsClientContext class.
1718
+ * Initializes a new instance of the ApplicationInsightsClient class.
1578
1719
  * @param options The parameter options
1579
1720
  */
1580
1721
  constructor(options) {
1722
+ var _a, _b;
1581
1723
  // Initializing default values for options
1582
1724
  if (!options) {
1583
1725
  options = {};
@@ -1585,34 +1727,33 @@ class ApplicationInsightsClientContext extends coreClient__namespace.ServiceClie
1585
1727
  const defaults = {
1586
1728
  requestContentType: "application/json; charset=utf-8"
1587
1729
  };
1588
- const packageDetails = `azsdk-js-monitor-opentelemetry-exporter/1.0.0-beta.7`;
1730
+ const packageDetails = `azsdk-js-monitor-opentelemetry-exporter/1.0.0-beta.9`;
1589
1731
  const userAgentPrefix = options.userAgentOptions && options.userAgentOptions.userAgentPrefix
1590
1732
  ? `${options.userAgentOptions.userAgentPrefix} ${packageDetails}`
1591
1733
  : `${packageDetails}`;
1592
1734
  const optionsWithDefaults = Object.assign(Object.assign(Object.assign({}, defaults), options), { userAgentOptions: {
1593
1735
  userAgentPrefix
1594
- }, baseUri: options.endpoint || "{Host}/v2.1" });
1736
+ }, baseUri: (_b = (_a = options.endpoint) !== null && _a !== void 0 ? _a : options.baseUri) !== null && _b !== void 0 ? _b : "{Host}/v2.1" });
1595
1737
  super(optionsWithDefaults);
1738
+ if ((options === null || options === void 0 ? void 0 : options.pipeline) && options.pipeline.getOrderedPolicies().length > 0) {
1739
+ const pipelinePolicies = options.pipeline.getOrderedPolicies();
1740
+ const bearerTokenAuthenticationPolicyFound = pipelinePolicies.some((pipelinePolicy) => pipelinePolicy.name ===
1741
+ coreRestPipeline__namespace.bearerTokenAuthenticationPolicyName);
1742
+ if (!bearerTokenAuthenticationPolicyFound) {
1743
+ this.pipeline.removePolicy({
1744
+ name: coreRestPipeline__namespace.bearerTokenAuthenticationPolicyName
1745
+ });
1746
+ this.pipeline.addPolicy(coreRestPipeline__namespace.bearerTokenAuthenticationPolicy({
1747
+ scopes: `${optionsWithDefaults.baseUri}/.default`,
1748
+ challengeCallbacks: {
1749
+ authorizeRequestOnChallenge: coreClient__namespace.authorizeRequestOnClaimChallenge
1750
+ }
1751
+ }));
1752
+ }
1753
+ }
1596
1754
  // Assigning values to Constant parameters
1597
1755
  this.host = options.host || "https://dc.services.visualstudio.com";
1598
1756
  }
1599
- }
1600
-
1601
- /*
1602
- * Copyright (c) Microsoft Corporation.
1603
- * Licensed under the MIT License.
1604
- *
1605
- * Code generated by Microsoft (R) AutoRest Code Generator.
1606
- * Changes may cause incorrect behavior and will be lost if the code is regenerated.
1607
- */
1608
- class ApplicationInsightsClient extends ApplicationInsightsClientContext {
1609
- /**
1610
- * Initializes a new instance of the ApplicationInsightsClient class.
1611
- * @param options The parameter options
1612
- */
1613
- constructor(options) {
1614
- super(options);
1615
- }
1616
1757
  /**
1617
1758
  * This operation sends a sequence of telemetry events that will be monitored by Azure Monitor.
1618
1759
  * @param body The list of telemetry events to track.
@@ -1669,19 +1810,16 @@ const applicationInsightsResource = "https://monitor.azure.com//.default";
1669
1810
  * @internal
1670
1811
  */
1671
1812
  class HttpSender {
1672
- constructor(_exporterOptions) {
1673
- this._exporterOptions = _exporterOptions;
1813
+ constructor(endpointUrl, options) {
1674
1814
  // Build endpoint using provided configuration or default values
1675
- this._appInsightsClientOptions = {
1676
- host: this._exporterOptions.endpointUrl,
1677
- };
1678
- this._appInsightsClient = new ApplicationInsightsClient(Object.assign({}, this._appInsightsClientOptions));
1815
+ this._appInsightsClientOptions = Object.assign({ host: endpointUrl }, options);
1816
+ this._appInsightsClient = new ApplicationInsightsClient(this._appInsightsClientOptions);
1679
1817
  // Handle redirects in HTTP Sender
1680
1818
  this._appInsightsClient.pipeline.removePolicy({ name: coreRestPipeline.redirectPolicyName });
1681
- if (this._exporterOptions.aadTokenCredential) {
1819
+ if (options === null || options === void 0 ? void 0 : options.aadTokenCredential) {
1682
1820
  let scopes = [applicationInsightsResource];
1683
1821
  this._appInsightsClient.pipeline.addPolicy(coreRestPipeline.bearerTokenAuthenticationPolicy({
1684
- credential: this._exporterOptions.aadTokenCredential,
1822
+ credential: options === null || options === void 0 ? void 0 : options.aadTokenCredential,
1685
1823
  scopes: scopes,
1686
1824
  }));
1687
1825
  }
@@ -1725,37 +1863,6 @@ class HttpSender {
1725
1863
  }
1726
1864
  }
1727
1865
 
1728
- // Copyright (c) Microsoft Corporation.
1729
- // Licensed under the MIT license.
1730
- /**
1731
- * AI MS Links.
1732
- * @internal
1733
- */
1734
- const MS_LINKS = "_MS.links";
1735
- /**
1736
- * AI enqueued time attribute.
1737
- * @internal
1738
- */
1739
- const ENQUEUED_TIME = "enqueuedTime";
1740
- /**
1741
- * AI time since enqueued attribute.
1742
- * @internal
1743
- */
1744
- const TIME_SINCE_ENQUEUED = "timeSinceEnqueued";
1745
- /**
1746
- * AzureMonitorTraceExporter version.
1747
- * @internal
1748
- */
1749
- const packageVersion = "1.0.0-beta.8";
1750
- var DependencyTypes;
1751
- (function (DependencyTypes) {
1752
- DependencyTypes["InProc"] = "InProc";
1753
- DependencyTypes["QueueMessage"] = "Queue Message";
1754
- DependencyTypes["Sql"] = "SQL";
1755
- DependencyTypes["Http"] = "Http";
1756
- DependencyTypes["Grpc"] = "GRPC";
1757
- })(DependencyTypes || (DependencyTypes = {}));
1758
-
1759
1866
  // Copyright (c) Microsoft Corporation.
1760
1867
  let instance = null;
1761
1868
  /**
@@ -1806,7 +1913,9 @@ function isRetriable(statusCode) {
1806
1913
  statusCode === 408 || // Timeout
1807
1914
  statusCode === 429 || // Too many requests
1808
1915
  statusCode === 500 || // Server Error
1809
- statusCode === 503 // Server Unavailable
1916
+ statusCode === 502 || // Bad Gateway
1917
+ statusCode === 503 || // Server Unavailable
1918
+ statusCode === 504 // Gateway Timeout
1810
1919
  );
1811
1920
  }
1812
1921
  /**
@@ -1830,39 +1939,39 @@ function msToTimeSpan(ms) {
1830
1939
  }
1831
1940
 
1832
1941
  // Copyright (c) Microsoft Corporation.
1942
+ const DEFAULT_BATCH_SEND_RETRY_INTERVAL_MS = 60000;
1833
1943
  /**
1834
1944
  * Azure Monitor OpenTelemetry Trace Exporter.
1835
1945
  */
1836
1946
  class AzureMonitorBaseExporter {
1837
1947
  /**
1838
1948
  * Initializes a new instance of the AzureMonitorBaseExporter class.
1839
- * @param AzureExporterConfig - Exporter configuration.
1949
+ * @param AzureMonitorExporterOptions - Exporter configuration.
1840
1950
  */
1841
1951
  constructor(options = {}) {
1842
- var _a, _b, _c, _d;
1952
+ var _a;
1953
+ this._batchSendRetryIntervalMs = DEFAULT_BATCH_SEND_RETRY_INTERVAL_MS;
1954
+ this._options = options;
1843
1955
  this._numConsecutiveRedirects = 0;
1844
- const connectionString = options.connectionString || process.env[ENV_CONNECTION_STRING];
1845
- this._options = Object.assign({}, DEFAULT_EXPORTER_CONFIG);
1846
- this._options.apiVersion = (_a = options.apiVersion) !== null && _a !== void 0 ? _a : this._options.apiVersion;
1847
- this._options.aadTokenCredential = options.aadTokenCredential;
1956
+ this._instrumentationKey = "";
1957
+ this._endpointUrl = DEFAULT_BREEZE_ENDPOINT;
1958
+ const connectionString = this._options.connectionString || process.env[ENV_CONNECTION_STRING];
1848
1959
  if (connectionString) {
1849
1960
  const parsedConnectionString = ConnectionStringParser.parse(connectionString);
1850
- this._options.instrumentationKey =
1851
- (_b = parsedConnectionString.instrumentationkey) !== null && _b !== void 0 ? _b : this._options.instrumentationKey;
1852
- this._options.endpointUrl =
1853
- (_d = (_c = parsedConnectionString.ingestionendpoint) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : this._options.endpointUrl;
1961
+ this._instrumentationKey =
1962
+ parsedConnectionString.instrumentationkey || this._instrumentationKey;
1963
+ this._endpointUrl = ((_a = parsedConnectionString.ingestionendpoint) === null || _a === void 0 ? void 0 : _a.trim()) || this._endpointUrl;
1854
1964
  }
1855
1965
  // Instrumentation key is required
1856
- if (!this._options.instrumentationKey) {
1966
+ if (!this._instrumentationKey) {
1857
1967
  const message = "No instrumentation key or connection string was provided to the Azure Monitor Exporter";
1858
1968
  api.diag.error(message);
1859
1969
  throw new Error(message);
1860
1970
  }
1861
- this._instrumentationKey = this._options.instrumentationKey;
1862
- this._sender = new HttpSender(this._options);
1863
- this._persister = new FileSystemPersist(this._options);
1971
+ this._sender = new HttpSender(this._endpointUrl, this._options);
1972
+ this._persister = new FileSystemPersist(this._instrumentationKey, this._options);
1864
1973
  this._retryTimer = null;
1865
- api.diag.debug("AzureMonitorTraceExporter was successfully setup");
1974
+ api.diag.debug("AzureMonitorExporter was successfully setup");
1866
1975
  }
1867
1976
  /**
1868
1977
  * Persist envelopes to disk
@@ -1892,6 +2001,9 @@ class AzureMonitorBaseExporter {
1892
2001
  */
1893
2002
  async _exportEnvelopes(envelopes) {
1894
2003
  api.diag.info(`Exporting ${envelopes.length} envelope(s)`);
2004
+ if (envelopes.length < 1) {
2005
+ return { code: core.ExportResultCode.SUCCESS };
2006
+ }
1895
2007
  try {
1896
2008
  const { result, statusCode } = await this._sender.send(envelopes);
1897
2009
  this._numConsecutiveRedirects = 0;
@@ -1901,7 +2013,7 @@ class AzureMonitorBaseExporter {
1901
2013
  this._retryTimer = setTimeout(() => {
1902
2014
  this._retryTimer = null;
1903
2015
  this._sendFirstPersistedFile();
1904
- }, this._options.batchSendRetryIntervalMs);
2016
+ }, this._batchSendRetryIntervalMs);
1905
2017
  this._retryTimer.unref();
1906
2018
  }
1907
2019
  return { code: core.ExportResultCode.SUCCESS };
@@ -2088,7 +2200,7 @@ const parseEventHubSpan = (span, baseData) => {
2088
2200
  };
2089
2201
 
2090
2202
  // Copyright (c) Microsoft Corporation.
2091
- function createGenericTagsFromSpan(span) {
2203
+ function createTagsFromSpan(span) {
2092
2204
  const tags = createTagsFromResource(span.resource);
2093
2205
  tags[KnownContextTagKeys.AiOperationId] = span.spanContext().traceId;
2094
2206
  if (span.parentSpanId) {
@@ -2099,10 +2211,6 @@ function createGenericTagsFromSpan(span) {
2099
2211
  // TODO: Not exposed in Swagger, need to update def
2100
2212
  tags["ai.user.userAgent"] = String(httpUserAgent);
2101
2213
  }
2102
- return tags;
2103
- }
2104
- function createTagsFromSpan(span) {
2105
- const tags = createGenericTagsFromSpan(span);
2106
2214
  if (span.kind === api.SpanKind.SERVER) {
2107
2215
  const httpMethod = span.attributes[semanticConventions.SemanticAttributes.HTTP_METHOD];
2108
2216
  const httpClientIp = span.attributes[semanticConventions.SemanticAttributes.HTTP_CLIENT_IP];
@@ -2153,7 +2261,8 @@ function createPropertiesFromSpanAttributes(attributes) {
2153
2261
  key.startsWith("exception.") ||
2154
2262
  key.startsWith("thread.") ||
2155
2263
  key.startsWith("faas.") ||
2156
- key.startsWith("code."))) {
2264
+ key.startsWith("code.") ||
2265
+ key.startsWith("_MS."))) {
2157
2266
  properties[key] = attributes[key];
2158
2267
  }
2159
2268
  }
@@ -2380,7 +2489,6 @@ function createRequestData(span) {
2380
2489
  function readableSpanToEnvelope(span, ikey) {
2381
2490
  let name;
2382
2491
  let baseType;
2383
- const sampleRate = 100;
2384
2492
  let baseData;
2385
2493
  const time = new Date(core.hrTimeToMilliseconds(span.startTime));
2386
2494
  const instrumentationKey = ikey;
@@ -2406,6 +2514,10 @@ function readableSpanToEnvelope(span, ikey) {
2406
2514
  api.diag.error(`Unsupported span kind ${span.kind}`);
2407
2515
  throw new Error(`Unsupported span kind ${span.kind}`);
2408
2516
  }
2517
+ let sampleRate = 100;
2518
+ if (span.attributes[AzureMonitorSampleRate]) {
2519
+ sampleRate = Number(span.attributes[AzureMonitorSampleRate]);
2520
+ }
2409
2521
  // Azure SDK
2410
2522
  if (span.attributes[AzNamespace]) {
2411
2523
  if (span.kind === api.SpanKind.INTERNAL) {
@@ -2437,14 +2549,20 @@ function spanEventsToEnvelopes(span, ikey) {
2437
2549
  let envelopes = [];
2438
2550
  if (span.events) {
2439
2551
  span.events.forEach((event) => {
2552
+ var _a;
2440
2553
  let baseType;
2441
- const sampleRate = 100;
2442
2554
  let time = new Date(core.hrTimeToMilliseconds(event.time));
2443
2555
  let name = "";
2444
2556
  let baseData;
2445
2557
  const properties = createPropertiesFromSpanAttributes(event.attributes);
2446
- const tags = createGenericTagsFromSpan(span);
2447
- if (event.name == "exception") {
2558
+ let tags = createTagsFromResource(span.resource);
2559
+ tags[KnownContextTagKeys.AiOperationId] = span.spanContext().traceId;
2560
+ let spanId = (_a = span.spanContext()) === null || _a === void 0 ? void 0 : _a.spanId;
2561
+ if (spanId) {
2562
+ tags[KnownContextTagKeys.AiOperationParentId] = spanId;
2563
+ }
2564
+ // Only generate exception telemetry for incoming requests
2565
+ if (event.name == "exception" && span.kind == api.SpanKind.SERVER) {
2448
2566
  name = "Microsoft.ApplicationInsights.Exception";
2449
2567
  baseType = "ExceptionData";
2450
2568
  let typeName = "";
@@ -2489,6 +2607,10 @@ function spanEventsToEnvelopes(span, ikey) {
2489
2607
  };
2490
2608
  baseData = messageData;
2491
2609
  }
2610
+ let sampleRate = 100;
2611
+ if (span.attributes[AzureMonitorSampleRate]) {
2612
+ sampleRate = Number(span.attributes[AzureMonitorSampleRate]);
2613
+ }
2492
2614
  let env = {
2493
2615
  name: name,
2494
2616
  time: time,
@@ -2518,6 +2640,10 @@ class AzureMonitorTraceExporter extends AzureMonitorBaseExporter {
2518
2640
  */
2519
2641
  constructor(options = {}) {
2520
2642
  super(options);
2643
+ /**
2644
+ * Flag to determine if Exporter is shutdown.
2645
+ */
2646
+ this._isShutdown = false;
2521
2647
  api.diag.debug("AzureMonitorTraceExporter was successfully setup");
2522
2648
  }
2523
2649
  /**
@@ -2526,6 +2652,11 @@ class AzureMonitorTraceExporter extends AzureMonitorBaseExporter {
2526
2652
  * @param resultCallback - Result callback.
2527
2653
  */
2528
2654
  async export(spans, resultCallback) {
2655
+ if (this._isShutdown) {
2656
+ api.diag.info("Exporter shut down. Failed to export spans.");
2657
+ setTimeout(() => resultCallback({ code: core.ExportResultCode.FAILED }), 0);
2658
+ return;
2659
+ }
2529
2660
  api.diag.info(`Exporting ${spans.length} span(s). Converting to envelopes...`);
2530
2661
  let envelopes = [];
2531
2662
  spans.forEach((span) => {
@@ -2541,12 +2672,22 @@ class AzureMonitorTraceExporter extends AzureMonitorBaseExporter {
2541
2672
  * Shutdown AzureMonitorTraceExporter.
2542
2673
  */
2543
2674
  async shutdown() {
2544
- api.diag.info("Azure Monitor Trace Exporter shutting down");
2675
+ this._isShutdown = true;
2676
+ api.diag.info("AzureMonitorTraceExporter shutting down");
2545
2677
  return this._shutdown();
2546
2678
  }
2547
2679
  }
2548
2680
 
2549
2681
  // Copyright (c) Microsoft Corporation.
2682
+ function createPropertiesFromMetricAttributes(attributes) {
2683
+ const properties = {};
2684
+ if (attributes) {
2685
+ for (const key of Object.keys(attributes)) {
2686
+ properties[key] = attributes[key];
2687
+ }
2688
+ }
2689
+ return properties;
2690
+ }
2550
2691
  /**
2551
2692
  * Metric to Azure envelope parsing.
2552
2693
  * @internal
@@ -2557,41 +2698,46 @@ function resourceMetricsToEnvelope(metrics, ikey) {
2557
2698
  const instrumentationKey = ikey;
2558
2699
  const tags = createTagsFromResource(metrics.resource);
2559
2700
  metrics.scopeMetrics.forEach((scopeMetric) => {
2560
- let baseData = {
2561
- metrics: [],
2562
- version: 2,
2563
- };
2564
2701
  scopeMetric.metrics.forEach((metric) => {
2565
2702
  metric.dataPoints.forEach((dataPoint) => {
2703
+ let baseData = {
2704
+ metrics: [],
2705
+ version: 2,
2706
+ properties: {},
2707
+ };
2708
+ baseData.properties = createPropertiesFromMetricAttributes(dataPoint.attributes);
2566
2709
  var metricDataPoint = {
2567
2710
  name: metric.descriptor.name,
2568
2711
  value: 0,
2569
2712
  dataPointType: "Aggregation",
2570
2713
  };
2571
- if (metric.dataPointType == sdkMetricsBase.DataPointType.SINGULAR) {
2714
+ if (metric.dataPointType == sdkMetrics.DataPointType.SUM ||
2715
+ metric.dataPointType == sdkMetrics.DataPointType.GAUGE) {
2572
2716
  metricDataPoint.value = dataPoint.value;
2573
2717
  metricDataPoint.count = 1;
2574
2718
  }
2575
2719
  else {
2576
- metricDataPoint.value = dataPoint.value.sum;
2720
+ metricDataPoint.value = dataPoint.value.sum || 0;
2577
2721
  metricDataPoint.count = dataPoint.value.count;
2722
+ metricDataPoint.max = dataPoint.value.max;
2723
+ metricDataPoint.min = dataPoint.value.min;
2578
2724
  }
2579
2725
  baseData.metrics.push(metricDataPoint);
2726
+ let envelope = {
2727
+ name: "Microsoft.ApplicationInsights.Metric",
2728
+ time: time,
2729
+ sampleRate: 100,
2730
+ instrumentationKey: instrumentationKey,
2731
+ tags: tags,
2732
+ version: 1,
2733
+ data: {
2734
+ baseType: "MetricData",
2735
+ baseData: Object.assign({}, baseData),
2736
+ },
2737
+ };
2738
+ envelopes.push(envelope);
2580
2739
  });
2581
2740
  });
2582
- let envelope = {
2583
- name: "Microsoft.ApplicationInsights.Metric",
2584
- time: time,
2585
- sampleRate: 100,
2586
- instrumentationKey: instrumentationKey,
2587
- tags: tags,
2588
- version: 1,
2589
- data: {
2590
- baseType: "MetricData",
2591
- baseData: Object.assign({}, baseData),
2592
- },
2593
- };
2594
- envelopes.push(envelope);
2595
2741
  });
2596
2742
  return envelopes;
2597
2743
  }
@@ -2607,6 +2753,11 @@ class AzureMonitorMetricExporter extends AzureMonitorBaseExporter {
2607
2753
  */
2608
2754
  constructor(options = {}) {
2609
2755
  super(options);
2756
+ /**
2757
+ * Flag to determine if Exporter is shutdown.
2758
+ */
2759
+ this._isShutdown = false;
2760
+ this._aggregationTemporality = sdkMetrics.AggregationTemporality.CUMULATIVE;
2610
2761
  api.diag.debug("AzureMonitorMetricExporter was successfully setup");
2611
2762
  }
2612
2763
  /**
@@ -2615,32 +2766,41 @@ class AzureMonitorMetricExporter extends AzureMonitorBaseExporter {
2615
2766
  * @param resultCallback - Result callback.
2616
2767
  */
2617
2768
  async export(metrics, resultCallback) {
2769
+ if (this._isShutdown) {
2770
+ api.diag.info("Exporter shut down. Failed to export spans.");
2771
+ setTimeout(() => resultCallback({ code: core.ExportResultCode.FAILED }), 0);
2772
+ return;
2773
+ }
2618
2774
  api.diag.info(`Exporting ${metrics.scopeMetrics.length} metrics(s). Converting to envelopes...`);
2619
2775
  let envelopes = resourceMetricsToEnvelope(metrics, this._instrumentationKey);
2620
- resultCallback(await this._exportEnvelopes(envelopes));
2776
+ // Supress tracing until OpenTelemetry Metrics SDK support it
2777
+ api.context.with(core.suppressTracing(api.context.active()), async () => {
2778
+ resultCallback(await this._exportEnvelopes(envelopes));
2779
+ });
2621
2780
  }
2622
2781
  /**
2623
2782
  * Shutdown AzureMonitorMetricExporter.
2624
2783
  */
2625
2784
  async shutdown() {
2626
- api.diag.info("Azure Monitor Trace Exporter shutting down");
2785
+ this._isShutdown = true;
2786
+ api.diag.info("AzureMonitorMetricExporter shutting down");
2627
2787
  return this._shutdown();
2628
2788
  }
2629
2789
  /**
2630
2790
  * Select aggregation temporality
2631
2791
  */
2632
- selectAggregationTemporality() {
2633
- return sdkMetricsBase.AggregationTemporality.CUMULATIVE;
2792
+ selectAggregationTemporality(_instrumentType) {
2793
+ return this._aggregationTemporality;
2634
2794
  }
2635
2795
  /**
2636
2796
  * Force flush
2637
2797
  */
2638
2798
  async forceFlush() {
2639
- // TODO: https://github.com/open-telemetry/opentelemetry-js/issues/3060
2640
- throw new Error("Method not implemented.");
2799
+ return Promise.resolve();
2641
2800
  }
2642
2801
  }
2643
2802
 
2803
+ exports.ApplicationInsightsSampler = ApplicationInsightsSampler;
2644
2804
  exports.AzureMonitorBaseExporter = AzureMonitorBaseExporter;
2645
2805
  exports.AzureMonitorMetricExporter = AzureMonitorMetricExporter;
2646
2806
  exports.AzureMonitorTraceExporter = AzureMonitorTraceExporter;