@azure/monitor-opentelemetry-exporter 1.0.0-beta.31 → 1.0.0-beta.33
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.
- package/README.md +4 -4
- package/dist/commonjs/Declarations/Constants.d.ts +5 -0
- package/dist/commonjs/Declarations/Constants.d.ts.map +1 -1
- package/dist/commonjs/Declarations/Constants.js +6 -1
- package/dist/commonjs/Declarations/Constants.js.map +1 -1
- package/dist/commonjs/export/base.js +25 -14
- package/dist/commonjs/export/base.js.map +1 -1
- package/dist/commonjs/export/log.js +5 -4
- package/dist/commonjs/export/log.js.map +1 -1
- package/dist/commonjs/export/metric.js +5 -4
- package/dist/commonjs/export/metric.js.map +1 -1
- package/dist/commonjs/export/statsbeat/customerStatsbeat.d.ts +125 -0
- package/dist/commonjs/export/statsbeat/customerStatsbeat.d.ts.map +1 -0
- package/dist/commonjs/export/statsbeat/customerStatsbeat.js +480 -0
- package/dist/commonjs/export/statsbeat/customerStatsbeat.js.map +1 -0
- package/dist/commonjs/export/statsbeat/longIntervalStatsbeatMetrics.d.ts +7 -7
- package/dist/commonjs/export/statsbeat/longIntervalStatsbeatMetrics.d.ts.map +1 -1
- package/dist/commonjs/export/statsbeat/longIntervalStatsbeatMetrics.js +69 -30
- package/dist/commonjs/export/statsbeat/longIntervalStatsbeatMetrics.js.map +1 -1
- package/dist/commonjs/export/statsbeat/networkStatsbeatMetrics.d.ts +6 -1
- package/dist/commonjs/export/statsbeat/networkStatsbeatMetrics.d.ts.map +1 -1
- package/dist/commonjs/export/statsbeat/networkStatsbeatMetrics.js +115 -43
- package/dist/commonjs/export/statsbeat/networkStatsbeatMetrics.js.map +1 -1
- package/dist/commonjs/export/statsbeat/statsbeatExporter.d.ts +7 -0
- package/dist/commonjs/export/statsbeat/statsbeatExporter.d.ts.map +1 -1
- package/dist/commonjs/export/statsbeat/statsbeatExporter.js +25 -5
- package/dist/commonjs/export/statsbeat/statsbeatExporter.js.map +1 -1
- package/dist/commonjs/export/statsbeat/statsbeatMetrics.js +6 -8
- package/dist/commonjs/export/statsbeat/statsbeatMetrics.js.map +1 -1
- package/dist/commonjs/export/statsbeat/types.d.ts +52 -0
- package/dist/commonjs/export/statsbeat/types.d.ts.map +1 -1
- package/dist/commonjs/export/statsbeat/types.js +75 -1
- package/dist/commonjs/export/statsbeat/types.js.map +1 -1
- package/dist/commonjs/export/trace.js +6 -5
- package/dist/commonjs/export/trace.js.map +1 -1
- package/dist/commonjs/generated/applicationInsightsClient.js +9 -4
- package/dist/commonjs/generated/applicationInsightsClient.js.map +1 -1
- package/dist/commonjs/generated/models/mappers.js +145 -68
- package/dist/commonjs/generated/models/mappers.js.map +1 -1
- package/dist/commonjs/index.d.ts +2 -1
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +5 -3
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/platform/nodejs/baseSender.d.ts +1 -0
- package/dist/commonjs/platform/nodejs/baseSender.d.ts.map +1 -1
- package/dist/commonjs/platform/nodejs/baseSender.js +101 -34
- package/dist/commonjs/platform/nodejs/baseSender.js.map +1 -1
- package/dist/commonjs/platform/nodejs/context/context.d.ts +0 -1
- package/dist/commonjs/platform/nodejs/context/context.d.ts.map +1 -1
- package/dist/commonjs/platform/nodejs/context/context.js +4 -8
- package/dist/commonjs/platform/nodejs/context/context.js.map +1 -1
- package/dist/commonjs/platform/nodejs/httpSender.js +11 -4
- package/dist/commonjs/platform/nodejs/httpSender.js.map +1 -1
- package/dist/commonjs/platform/nodejs/index.d.ts +1 -0
- package/dist/commonjs/platform/nodejs/index.d.ts.map +1 -1
- package/dist/commonjs/platform/nodejs/index.js +1 -0
- package/dist/commonjs/platform/nodejs/index.js.map +1 -1
- package/dist/commonjs/platform/nodejs/persist/fileAccessControl.js +7 -7
- package/dist/commonjs/platform/nodejs/persist/fileAccessControl.js.map +1 -1
- package/dist/commonjs/platform/nodejs/persist/fileSystemPersist.d.ts +9 -1
- package/dist/commonjs/platform/nodejs/persist/fileSystemPersist.d.ts.map +1 -1
- package/dist/commonjs/platform/nodejs/persist/fileSystemPersist.js +37 -16
- package/dist/commonjs/platform/nodejs/persist/fileSystemPersist.js.map +1 -1
- package/dist/commonjs/{sampling.d.ts → sampling/percentageSampler.d.ts} +2 -3
- package/dist/commonjs/sampling/percentageSampler.d.ts.map +1 -0
- package/dist/commonjs/{sampling.js → sampling/percentageSampler.js} +6 -39
- package/dist/commonjs/sampling/percentageSampler.js.map +1 -0
- package/dist/commonjs/sampling/rateLimitedSampler.d.ts +58 -0
- package/dist/commonjs/sampling/rateLimitedSampler.d.ts.map +1 -0
- package/dist/commonjs/sampling/rateLimitedSampler.js +118 -0
- package/dist/commonjs/sampling/rateLimitedSampler.js.map +1 -0
- package/dist/commonjs/sampling/samplingUtils.d.ts +17 -0
- package/dist/commonjs/sampling/samplingUtils.d.ts.map +1 -0
- package/dist/commonjs/sampling/samplingUtils.js +94 -0
- package/dist/commonjs/sampling/samplingUtils.js.map +1 -0
- package/dist/commonjs/tsdoc-metadata.json +11 -11
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +1 -0
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/utils/common.js +2 -3
- package/dist/commonjs/utils/common.js.map +1 -1
- package/dist/commonjs/utils/connectionStringParser.js +3 -3
- package/dist/commonjs/utils/connectionStringParser.js.map +1 -1
- package/dist/commonjs/utils/constants/applicationinsights.d.ts +1 -1
- package/dist/commonjs/utils/constants/applicationinsights.js +1 -1
- package/dist/commonjs/utils/constants/applicationinsights.js.map +1 -1
- package/dist/commonjs/utils/eventhub.js +5 -2
- package/dist/commonjs/utils/eventhub.js.map +1 -1
- package/dist/commonjs/utils/logUtils.js +10 -9
- package/dist/commonjs/utils/logUtils.js.map +1 -1
- package/dist/commonjs/utils/metricUtils.d.ts.map +1 -1
- package/dist/commonjs/utils/metricUtils.js +6 -6
- package/dist/commonjs/utils/metricUtils.js.map +1 -1
- package/dist/commonjs/utils/spanUtils.d.ts.map +1 -1
- package/dist/commonjs/utils/spanUtils.js +16 -14
- package/dist/commonjs/utils/spanUtils.js.map +1 -1
- package/dist/esm/Declarations/Constants.d.ts +5 -0
- package/dist/esm/Declarations/Constants.d.ts.map +1 -1
- package/dist/esm/Declarations/Constants.js +5 -0
- package/dist/esm/Declarations/Constants.js.map +1 -1
- package/dist/esm/export/base.js +25 -14
- package/dist/esm/export/base.js.map +1 -1
- package/dist/esm/export/log.js +5 -4
- package/dist/esm/export/log.js.map +1 -1
- package/dist/esm/export/metric.js +5 -4
- package/dist/esm/export/metric.js.map +1 -1
- package/dist/esm/export/statsbeat/customerStatsbeat.d.ts +125 -0
- package/dist/esm/export/statsbeat/customerStatsbeat.d.ts.map +1 -0
- package/dist/esm/export/statsbeat/customerStatsbeat.js +475 -0
- package/dist/esm/export/statsbeat/customerStatsbeat.js.map +1 -0
- package/dist/esm/export/statsbeat/longIntervalStatsbeatMetrics.d.ts +7 -7
- package/dist/esm/export/statsbeat/longIntervalStatsbeatMetrics.d.ts.map +1 -1
- package/dist/esm/export/statsbeat/longIntervalStatsbeatMetrics.js +68 -30
- package/dist/esm/export/statsbeat/longIntervalStatsbeatMetrics.js.map +1 -1
- package/dist/esm/export/statsbeat/networkStatsbeatMetrics.d.ts +6 -1
- package/dist/esm/export/statsbeat/networkStatsbeatMetrics.d.ts.map +1 -1
- package/dist/esm/export/statsbeat/networkStatsbeatMetrics.js +115 -43
- package/dist/esm/export/statsbeat/networkStatsbeatMetrics.js.map +1 -1
- package/dist/esm/export/statsbeat/statsbeatExporter.d.ts +7 -0
- package/dist/esm/export/statsbeat/statsbeatExporter.d.ts.map +1 -1
- package/dist/esm/export/statsbeat/statsbeatExporter.js +25 -5
- package/dist/esm/export/statsbeat/statsbeatExporter.js.map +1 -1
- package/dist/esm/export/statsbeat/statsbeatMetrics.js +6 -8
- package/dist/esm/export/statsbeat/statsbeatMetrics.js.map +1 -1
- package/dist/esm/export/statsbeat/types.d.ts +52 -0
- package/dist/esm/export/statsbeat/types.d.ts.map +1 -1
- package/dist/esm/export/statsbeat/types.js +73 -0
- package/dist/esm/export/statsbeat/types.js.map +1 -1
- package/dist/esm/export/trace.js +6 -5
- package/dist/esm/export/trace.js.map +1 -1
- package/dist/esm/generated/applicationInsightsClient.js +9 -4
- package/dist/esm/generated/applicationInsightsClient.js.map +1 -1
- package/dist/esm/generated/models/mappers.js +145 -68
- package/dist/esm/generated/models/mappers.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/platform/nodejs/baseSender.d.ts +1 -0
- package/dist/esm/platform/nodejs/baseSender.d.ts.map +1 -1
- package/dist/esm/platform/nodejs/baseSender.js +104 -37
- package/dist/esm/platform/nodejs/baseSender.js.map +1 -1
- package/dist/esm/platform/nodejs/context/context.d.ts +0 -1
- package/dist/esm/platform/nodejs/context/context.d.ts.map +1 -1
- package/dist/esm/platform/nodejs/context/context.js +4 -8
- package/dist/esm/platform/nodejs/context/context.js.map +1 -1
- package/dist/esm/platform/nodejs/httpSender.js +11 -4
- package/dist/esm/platform/nodejs/httpSender.js.map +1 -1
- package/dist/esm/platform/nodejs/index.d.ts +1 -0
- package/dist/esm/platform/nodejs/index.d.ts.map +1 -1
- package/dist/esm/platform/nodejs/index.js +1 -0
- package/dist/esm/platform/nodejs/index.js.map +1 -1
- package/dist/esm/platform/nodejs/persist/fileAccessControl.js +7 -7
- package/dist/esm/platform/nodejs/persist/fileAccessControl.js.map +1 -1
- package/dist/esm/platform/nodejs/persist/fileSystemPersist.d.ts +9 -1
- package/dist/esm/platform/nodejs/persist/fileSystemPersist.d.ts.map +1 -1
- package/dist/esm/platform/nodejs/persist/fileSystemPersist.js +37 -16
- package/dist/esm/platform/nodejs/persist/fileSystemPersist.js.map +1 -1
- package/dist/esm/{sampling.d.ts → sampling/percentageSampler.d.ts} +2 -3
- package/dist/esm/sampling/percentageSampler.d.ts.map +1 -0
- package/dist/esm/{sampling.js → sampling/percentageSampler.js} +6 -39
- package/dist/esm/sampling/percentageSampler.js.map +1 -0
- package/dist/esm/sampling/rateLimitedSampler.d.ts +58 -0
- package/dist/esm/sampling/rateLimitedSampler.d.ts.map +1 -0
- package/dist/esm/sampling/rateLimitedSampler.js +114 -0
- package/dist/esm/sampling/rateLimitedSampler.js.map +1 -0
- package/dist/esm/sampling/samplingUtils.d.ts +17 -0
- package/dist/esm/sampling/samplingUtils.d.ts.map +1 -0
- package/dist/esm/sampling/samplingUtils.js +89 -0
- package/dist/esm/sampling/samplingUtils.js.map +1 -0
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +2 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/common.js +2 -3
- package/dist/esm/utils/common.js.map +1 -1
- package/dist/esm/utils/connectionStringParser.js +3 -3
- package/dist/esm/utils/connectionStringParser.js.map +1 -1
- package/dist/esm/utils/constants/applicationinsights.d.ts +1 -1
- package/dist/esm/utils/constants/applicationinsights.js +1 -1
- package/dist/esm/utils/constants/applicationinsights.js.map +1 -1
- package/dist/esm/utils/eventhub.js +5 -2
- package/dist/esm/utils/eventhub.js.map +1 -1
- package/dist/esm/utils/logUtils.js +10 -9
- package/dist/esm/utils/logUtils.js.map +1 -1
- package/dist/esm/utils/metricUtils.d.ts.map +1 -1
- package/dist/esm/utils/metricUtils.js +6 -6
- package/dist/esm/utils/metricUtils.js.map +1 -1
- package/dist/esm/utils/spanUtils.d.ts.map +1 -1
- package/dist/esm/utils/spanUtils.js +16 -14
- package/dist/esm/utils/spanUtils.js.map +1 -1
- package/package.json +21 -26
- package/dist/commonjs/sampling.d.ts.map +0 -1
- package/dist/commonjs/sampling.js.map +0 -1
- package/dist/esm/sampling.d.ts.map +0 -1
- package/dist/esm/sampling.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"percentageSampler.d.ts","sourceRoot":"","sources":["../../../src/sampling/percentageSampler.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAI7E;;;;;GAKG;AACH,qBAAa,0BAA2B,YAAW,OAAO;IACxD,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC;;;OAGG;gBACS,aAAa,GAAE,MAAU;IAQrC;;;;;;;;;;;;;OAaG;IACI,YAAY,CACjB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EAEf,QAAQ,EAAE,MAAM,EAEhB,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,UAAU,EAEtB,KAAK,EAAE,IAAI,EAAE,GACZ,cAAc;IAMjB;;OAEG;IACI,QAAQ,IAAI,MAAM;CAG1B"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApplicationInsightsSampler = void 0;
|
|
4
4
|
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
5
|
-
const
|
|
5
|
+
const samplingUtils_js_1 = require("./samplingUtils.js");
|
|
6
6
|
/**
|
|
7
7
|
* ApplicationInsightsSampler is responsible for the following:
|
|
8
8
|
* Implements same trace id hashing algorithm so that traces are sampled the same across multiple nodes
|
|
@@ -10,6 +10,8 @@ const applicationinsights_js_1 = require("./utils/constants/applicationinsights.
|
|
|
10
10
|
* @param samplingRatio - 0 to 1 value.
|
|
11
11
|
*/
|
|
12
12
|
class ApplicationInsightsSampler {
|
|
13
|
+
_sampleRate;
|
|
14
|
+
samplingRatio;
|
|
13
15
|
/**
|
|
14
16
|
* Initializes a new instance of the ApplicationInsightsSampler class.
|
|
15
17
|
* @param samplingRatio - Value in the range [0,1], 1 meaning all data will sampled and 0 all Tracing data will be sampled out.
|
|
@@ -35,32 +37,14 @@ class ApplicationInsightsSampler {
|
|
|
35
37
|
* be created. Typically useful for batch operations.
|
|
36
38
|
* @returns a {@link SamplingResult}.
|
|
37
39
|
*/
|
|
38
|
-
shouldSample(
|
|
39
|
-
// @ts-expect-error unused argument
|
|
40
|
-
context, traceId,
|
|
40
|
+
shouldSample(context, traceId,
|
|
41
41
|
// @ts-expect-error unused argument
|
|
42
42
|
spanName,
|
|
43
43
|
// @ts-expect-error unused argument
|
|
44
44
|
spanKind, attributes,
|
|
45
45
|
// @ts-expect-error unused argument
|
|
46
46
|
links) {
|
|
47
|
-
|
|
48
|
-
if (this._sampleRate === 100) {
|
|
49
|
-
isSampledIn = true;
|
|
50
|
-
}
|
|
51
|
-
else if (this._sampleRate === 0) {
|
|
52
|
-
isSampledIn = false;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
isSampledIn = this._getSamplingHashCode(traceId) < this._sampleRate;
|
|
56
|
-
}
|
|
57
|
-
// Add sample rate as span attribute
|
|
58
|
-
attributes = attributes || {};
|
|
59
|
-
// Only send the sample rate if it's not 100
|
|
60
|
-
if (this._sampleRate !== 100) {
|
|
61
|
-
attributes[applicationinsights_js_1.AzureMonitorSampleRate] = this._sampleRate;
|
|
62
|
-
}
|
|
63
|
-
return isSampledIn
|
|
47
|
+
return (0, samplingUtils_js_1.shouldSample)(this._sampleRate, context, traceId, attributes)
|
|
64
48
|
? { decision: sdk_trace_base_1.SamplingDecision.RECORD_AND_SAMPLED, attributes: attributes }
|
|
65
49
|
: { decision: sdk_trace_base_1.SamplingDecision.NOT_RECORD, attributes: attributes };
|
|
66
50
|
}
|
|
@@ -70,23 +54,6 @@ class ApplicationInsightsSampler {
|
|
|
70
54
|
toString() {
|
|
71
55
|
return `ApplicationInsightsSampler{${this.samplingRatio}}`;
|
|
72
56
|
}
|
|
73
|
-
_getSamplingHashCode(input) {
|
|
74
|
-
const csharpMin = -2147483648;
|
|
75
|
-
const csharpMax = 2147483647;
|
|
76
|
-
let hash = 5381;
|
|
77
|
-
if (!input) {
|
|
78
|
-
return 0;
|
|
79
|
-
}
|
|
80
|
-
while (input.length < 8) {
|
|
81
|
-
input = input + input;
|
|
82
|
-
}
|
|
83
|
-
for (let i = 0; i < input.length; i++) {
|
|
84
|
-
// JS doesn't respond to integer overflow by wrapping around. Simulate it with bitwise operators ( | 0)
|
|
85
|
-
hash = ((((hash << 5) + hash) | 0) + input.charCodeAt(i)) | 0;
|
|
86
|
-
}
|
|
87
|
-
hash = hash <= csharpMin ? csharpMax : Math.abs(hash);
|
|
88
|
-
return (hash / csharpMax) * 100;
|
|
89
|
-
}
|
|
90
57
|
}
|
|
91
58
|
exports.ApplicationInsightsSampler = ApplicationInsightsSampler;
|
|
92
|
-
//# sourceMappingURL=
|
|
59
|
+
//# sourceMappingURL=percentageSampler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"percentageSampler.js","sourceRoot":"","sources":["../../../src/sampling/percentageSampler.ts"],"names":[],"mappings":";;;AAIA,kEAAiE;AACjE,yDAAkD;AAElD;;;;;GAKG;AACH,MAAa,0BAA0B;IAC7B,WAAW,CAAS;IACX,aAAa,CAAS;IAEvC;;;OAGG;IACH,YAAY,gBAAwB,CAAC;QACnC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,IAAI,CAAC,aAAa,GAAG,CAAC,IAAI,IAAI,CAAC,aAAa,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;YAC7F,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;QACvE,CAAC;QACD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,GAAG,GAAG,CAAC,CAAC;IAC1D,CAAC;IAED;;;;;;;;;;;;;OAaG;IACI,YAAY,CACjB,OAAgB,EAChB,OAAe;IACf,mCAAmC;IACnC,QAAgB;IAChB,mCAAmC;IACnC,QAAkB,EAClB,UAAsB;IACtB,mCAAmC;IACnC,KAAa;QAEb,OAAO,IAAA,+BAAY,EAAC,IAAI,CAAC,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC;YACjE,CAAC,CAAC,EAAE,QAAQ,EAAE,iCAAgB,CAAC,kBAAkB,EAAE,UAAU,EAAE,UAAU,EAAE;YAC3E,CAAC,CAAC,EAAE,QAAQ,EAAE,iCAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;IACxE,CAAC;IAED;;OAEG;IACI,QAAQ;QACb,OAAO,8BAA8B,IAAI,CAAC,aAAa,GAAG,CAAC;IAC7D,CAAC;CACF;AApDD,gEAoDC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport type { Link, Attributes, SpanKind, Context } from \"@opentelemetry/api\";\nimport type { Sampler, SamplingResult } from \"@opentelemetry/sdk-trace-base\";\nimport { SamplingDecision } from \"@opentelemetry/sdk-trace-base\";\nimport { shouldSample } from \"./samplingUtils.js\";\n\n/**\n * ApplicationInsightsSampler is responsible for the following:\n * Implements same trace id hashing algorithm so that traces are sampled the same across multiple nodes\n * Adds item count to span attribute if span is sampled (needed for ingestion service)\n * @param samplingRatio - 0 to 1 value.\n */\nexport class ApplicationInsightsSampler implements Sampler {\n private _sampleRate: number;\n private readonly samplingRatio: number;\n\n /**\n * Initializes a new instance of the ApplicationInsightsSampler class.\n * @param samplingRatio - Value in the range [0,1], 1 meaning all data will sampled and 0 all Tracing data will be sampled out.\n */\n constructor(samplingRatio: number = 1) {\n this.samplingRatio = samplingRatio;\n if (this.samplingRatio > 1 || this.samplingRatio < 0 || !Number.isFinite(this.samplingRatio)) {\n throw new Error(\"Wrong sampling rate, data will not be sampled out\");\n }\n this._sampleRate = Math.round(this.samplingRatio * 100);\n }\n\n /**\n * Checks whether span needs to be created and tracked.\n *\n * @param context - Parent Context which may contain a span.\n * @param traceId - traceif of the span to be created. It can be different from the\n * traceId in the {@link SpanContext}. Typically in situations when the\n * span to be created starts a new trace.\n * @param spanName - Name of the span to be created.\n * @param spanKind - Kind of the span to be created.\n * @param attributes - Initial set of SpanAttributes for the Span being constructed.\n * @param links - Collection of links that will be associated with the Span to\n * be created. Typically useful for batch operations.\n * @returns a {@link SamplingResult}.\n */\n public shouldSample(\n context: Context,\n traceId: string,\n // @ts-expect-error unused argument\n spanName: string,\n // @ts-expect-error unused argument\n spanKind: SpanKind,\n attributes: Attributes,\n // @ts-expect-error unused argument\n links: Link[],\n ): SamplingResult {\n return shouldSample(this._sampleRate, context, traceId, attributes)\n ? { decision: SamplingDecision.RECORD_AND_SAMPLED, attributes: attributes }\n : { decision: SamplingDecision.NOT_RECORD, attributes: attributes };\n }\n\n /**\n * Return Sampler description\n */\n public toString(): string {\n return `ApplicationInsightsSampler{${this.samplingRatio}}`;\n }\n}\n"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Link, Attributes, SpanKind, Context } from "@opentelemetry/api";
|
|
2
|
+
import type { Sampler, SamplingResult } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
/**
|
|
4
|
+
* RateLimitedSampler is responsible for the following:
|
|
5
|
+
* - Implements a rate-limiting sampling strategy based on a specified number of requests per second.
|
|
6
|
+
* - Dynamically adjusts the sampling rate based on the time elapsed since the last sample.
|
|
7
|
+
* - Provides a sampling rate that can be used to determine whether a span should be recorded.
|
|
8
|
+
* @param requestsPerSecond -
|
|
9
|
+
*/
|
|
10
|
+
export declare class RateLimitedSampler implements Sampler {
|
|
11
|
+
private readonly nanoTimeSupplier;
|
|
12
|
+
private readonly inverseAdaptationTimeNanos;
|
|
13
|
+
private readonly targetSpansPerNanosecondLimit;
|
|
14
|
+
private state;
|
|
15
|
+
private readonly roundToNearest;
|
|
16
|
+
private readonly tracesPerSecond;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the RateLimitedSampler class.
|
|
19
|
+
* @param tracesPerSecond - The maximum number of traces to sample per second.
|
|
20
|
+
* @throws Error if tracesPerSecond is negative.
|
|
21
|
+
*/
|
|
22
|
+
constructor(tracesPerSecond: number);
|
|
23
|
+
/**
|
|
24
|
+
* Updates the state of the sampler based on the current time.
|
|
25
|
+
* This method calculates the effective window count and nanos based on the time elapsed since the last sample.
|
|
26
|
+
* @param oldState - The previous state of the sampler.
|
|
27
|
+
* @param currentNanoTime - The current time in nanoseconds.
|
|
28
|
+
* @returns The updated state of the sampler.
|
|
29
|
+
*/
|
|
30
|
+
private updateState;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the current sample rate based on the effective window count and nanos.
|
|
33
|
+
* This method calculates the sampling probability and returns it as a percentage.
|
|
34
|
+
* If `roundToNearest` is true, it rounds down the sampling percentage to the nearest whole number.
|
|
35
|
+
* @returns The current sample rate as a percentage.
|
|
36
|
+
*/
|
|
37
|
+
getSampleRate(): number;
|
|
38
|
+
/**
|
|
39
|
+
* Checks whether span needs to be created and tracked.
|
|
40
|
+
*
|
|
41
|
+
* @param context - Parent Context which may contain a span.
|
|
42
|
+
* @param traceId - traceId of the span to be created. It can be different from the
|
|
43
|
+
* traceId in the {@link SpanContext}. Typically in situations when the
|
|
44
|
+
* span to be created starts a new trace.
|
|
45
|
+
* @param spanName - Name of the span to be created.
|
|
46
|
+
* @param spanKind - Kind of the span to be created.
|
|
47
|
+
* @param attributes - Initial set of SpanAttributes for the Span being constructed.
|
|
48
|
+
* @param links - Collection of links that will be associated with the Span to
|
|
49
|
+
* be created. Typically useful for batch operations.
|
|
50
|
+
* @returns a {@link SamplingResult}.
|
|
51
|
+
*/
|
|
52
|
+
shouldSample(context: Context, traceId: string, spanName: string, spanKind: SpanKind, attributes: Attributes, links: Link[]): SamplingResult;
|
|
53
|
+
/**
|
|
54
|
+
* Return Sampler description
|
|
55
|
+
*/
|
|
56
|
+
toString(): string;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=rateLimitedSampler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rateLimitedSampler.d.ts","sourceRoot":"","sources":["../../../src/sampling/rateLimitedSampler.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAU7E;;;;;;GAMG;AACH,qBAAa,kBAAmB,YAAW,OAAO;IAChD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAe;IAChD,OAAO,CAAC,QAAQ,CAAC,0BAA0B,CAAS;IACpD,OAAO,CAAC,QAAQ,CAAC,6BAA6B,CAAS;IACvD,OAAO,CAAC,KAAK,CAA0B;IACvC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAU;IACzC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IAEzC;;;;OAIG;gBACS,eAAe,EAAE,MAAM;IAkBnC;;;;;;OAMG;IACH,OAAO,CAAC,WAAW;IAsBnB;;;;;OAKG;IACI,aAAa,IAAI,MAAM;IAe9B;;;;;;;;;;;;;OAaG;IACI,YAAY,CACjB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EAEf,QAAQ,EAAE,MAAM,EAEhB,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,UAAU,EAEtB,KAAK,EAAE,IAAI,EAAE,GACZ,cAAc;IAOjB;;OAEG;IACI,QAAQ,IAAI,MAAM;CAG1B"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RateLimitedSampler = void 0;
|
|
4
|
+
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
5
|
+
const samplingUtils_js_1 = require("./samplingUtils.js");
|
|
6
|
+
/**
|
|
7
|
+
* RateLimitedSampler is responsible for the following:
|
|
8
|
+
* - Implements a rate-limiting sampling strategy based on a specified number of requests per second.
|
|
9
|
+
* - Dynamically adjusts the sampling rate based on the time elapsed since the last sample.
|
|
10
|
+
* - Provides a sampling rate that can be used to determine whether a span should be recorded.
|
|
11
|
+
* @param requestsPerSecond -
|
|
12
|
+
*/
|
|
13
|
+
class RateLimitedSampler {
|
|
14
|
+
nanoTimeSupplier;
|
|
15
|
+
inverseAdaptationTimeNanos;
|
|
16
|
+
targetSpansPerNanosecondLimit;
|
|
17
|
+
state;
|
|
18
|
+
roundToNearest;
|
|
19
|
+
tracesPerSecond;
|
|
20
|
+
/**
|
|
21
|
+
* Initializes a new instance of the RateLimitedSampler class.
|
|
22
|
+
* @param tracesPerSecond - The maximum number of traces to sample per second.
|
|
23
|
+
* @throws Error if tracesPerSecond is negative.
|
|
24
|
+
*/
|
|
25
|
+
constructor(tracesPerSecond) {
|
|
26
|
+
this.tracesPerSecond = tracesPerSecond;
|
|
27
|
+
if (this.tracesPerSecond < 0.0) {
|
|
28
|
+
throw new Error("Limit for sampled traces per second must be nonnegative");
|
|
29
|
+
}
|
|
30
|
+
const adaptationTimeSeconds = 0.1;
|
|
31
|
+
this.nanoTimeSupplier = () => Number(process.hrtime.bigint());
|
|
32
|
+
this.inverseAdaptationTimeNanos = 1e-9 / adaptationTimeSeconds;
|
|
33
|
+
this.targetSpansPerNanosecondLimit = 1e-9 * this.tracesPerSecond;
|
|
34
|
+
const now = this.nanoTimeSupplier();
|
|
35
|
+
this.state = {
|
|
36
|
+
effectiveWindowCount: 0,
|
|
37
|
+
effectiveWindowNanos: 0,
|
|
38
|
+
lastNanoTime: now,
|
|
39
|
+
};
|
|
40
|
+
this.roundToNearest = true;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Updates the state of the sampler based on the current time.
|
|
44
|
+
* This method calculates the effective window count and nanos based on the time elapsed since the last sample.
|
|
45
|
+
* @param oldState - The previous state of the sampler.
|
|
46
|
+
* @param currentNanoTime - The current time in nanoseconds.
|
|
47
|
+
* @returns The updated state of the sampler.
|
|
48
|
+
*/
|
|
49
|
+
updateState(oldState, currentNanoTime) {
|
|
50
|
+
if (currentNanoTime <= oldState.lastNanoTime) {
|
|
51
|
+
return {
|
|
52
|
+
effectiveWindowCount: oldState.effectiveWindowCount + 1,
|
|
53
|
+
effectiveWindowNanos: oldState.effectiveWindowNanos,
|
|
54
|
+
lastNanoTime: oldState.lastNanoTime,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const nanoTimeDelta = currentNanoTime - oldState.lastNanoTime;
|
|
58
|
+
const decayFactor = Math.exp(-nanoTimeDelta * this.inverseAdaptationTimeNanos);
|
|
59
|
+
const currentEffectiveWindowCount = oldState.effectiveWindowCount * decayFactor + 1;
|
|
60
|
+
const currentEffectiveWindowNanos = oldState.effectiveWindowNanos * decayFactor + nanoTimeDelta;
|
|
61
|
+
return {
|
|
62
|
+
effectiveWindowCount: currentEffectiveWindowCount,
|
|
63
|
+
effectiveWindowNanos: currentEffectiveWindowNanos,
|
|
64
|
+
lastNanoTime: currentNanoTime,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the current sample rate based on the effective window count and nanos.
|
|
69
|
+
* This method calculates the sampling probability and returns it as a percentage.
|
|
70
|
+
* If `roundToNearest` is true, it rounds down the sampling percentage to the nearest whole number.
|
|
71
|
+
* @returns The current sample rate as a percentage.
|
|
72
|
+
*/
|
|
73
|
+
getSampleRate() {
|
|
74
|
+
const currentNanoTime = this.nanoTimeSupplier();
|
|
75
|
+
this.state = this.updateState(this.state, currentNanoTime);
|
|
76
|
+
const samplingProbability = (this.state.effectiveWindowNanos * this.targetSpansPerNanosecondLimit) /
|
|
77
|
+
this.state.effectiveWindowCount;
|
|
78
|
+
let samplingPercentage = 100 * Math.min(samplingProbability, 1);
|
|
79
|
+
if (this.roundToNearest) {
|
|
80
|
+
samplingPercentage = (0, samplingUtils_js_1.roundDownToNearest)(samplingPercentage);
|
|
81
|
+
}
|
|
82
|
+
return samplingPercentage;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks whether span needs to be created and tracked.
|
|
86
|
+
*
|
|
87
|
+
* @param context - Parent Context which may contain a span.
|
|
88
|
+
* @param traceId - traceId of the span to be created. It can be different from the
|
|
89
|
+
* traceId in the {@link SpanContext}. Typically in situations when the
|
|
90
|
+
* span to be created starts a new trace.
|
|
91
|
+
* @param spanName - Name of the span to be created.
|
|
92
|
+
* @param spanKind - Kind of the span to be created.
|
|
93
|
+
* @param attributes - Initial set of SpanAttributes for the Span being constructed.
|
|
94
|
+
* @param links - Collection of links that will be associated with the Span to
|
|
95
|
+
* be created. Typically useful for batch operations.
|
|
96
|
+
* @returns a {@link SamplingResult}.
|
|
97
|
+
*/
|
|
98
|
+
shouldSample(context, traceId,
|
|
99
|
+
// @ts-expect-error unused argument
|
|
100
|
+
spanName,
|
|
101
|
+
// @ts-expect-error unused argument
|
|
102
|
+
spanKind, attributes,
|
|
103
|
+
// @ts-expect-error unused argument
|
|
104
|
+
links) {
|
|
105
|
+
const sampleRate = this.getSampleRate();
|
|
106
|
+
return (0, samplingUtils_js_1.shouldSample)(sampleRate, context, traceId, attributes)
|
|
107
|
+
? { decision: sdk_trace_base_1.SamplingDecision.RECORD_AND_SAMPLED, attributes: attributes }
|
|
108
|
+
: { decision: sdk_trace_base_1.SamplingDecision.NOT_RECORD, attributes: attributes };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Return Sampler description
|
|
112
|
+
*/
|
|
113
|
+
toString() {
|
|
114
|
+
return `RateLimitedSampler{${this.tracesPerSecond}}`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.RateLimitedSampler = RateLimitedSampler;
|
|
118
|
+
//# sourceMappingURL=rateLimitedSampler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rateLimitedSampler.js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IAAA,+BAAY,EAAC,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC;YAC3D,CAAC,CAAC,EAAE,QAAQ,EAAE,iCAAgB,CAAC,kBAAkB,EAAE,UAAU,EAAE,UAAU,EAAE;YAC3E,CAAC,CAAC,EAAE,QAAQ,EAAE,iCAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;IACxE,CAAC;IAED;;OAEG;IACI,QAAQ;QACb,OAAO,sBAAsB,IAAI,CAAC,eAAe,GAAG,CAAC;IACvD,CAAC;CACF;AAtHD,gDAsHC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\nimport type { Link, Attributes, SpanKind, Context } from \"@opentelemetry/api\";\nimport type { Sampler, SamplingResult } from \"@opentelemetry/sdk-trace-base\";\nimport { SamplingDecision } from \"@opentelemetry/sdk-trace-base\";\nimport { roundDownToNearest, shouldSample } from \"./samplingUtils.js\";\n\ntype RateLimitedSamplerState = {\n effectiveWindowCount: number;\n effectiveWindowNanos: number;\n lastNanoTime: number;\n};\n\n/**\n * RateLimitedSampler is responsible for the following:\n * - Implements a rate-limiting sampling strategy based on a specified number of requests per second.\n * - Dynamically adjusts the sampling rate based on the time elapsed since the last sample.\n * - Provides a sampling rate that can be used to determine whether a span should be recorded.\n * @param requestsPerSecond -\n */\nexport class RateLimitedSampler implements Sampler {\n private readonly nanoTimeSupplier: () => number;\n private readonly inverseAdaptationTimeNanos: number;\n private readonly targetSpansPerNanosecondLimit: number;\n private state: RateLimitedSamplerState;\n private readonly roundToNearest: boolean;\n private readonly tracesPerSecond: number;\n\n /**\n * Initializes a new instance of the RateLimitedSampler class.\n * @param tracesPerSecond - The maximum number of traces to sample per second.\n * @throws Error if tracesPerSecond is negative.\n */\n constructor(tracesPerSecond: number) {\n this.tracesPerSecond = tracesPerSecond;\n if (this.tracesPerSecond < 0.0) {\n throw new Error(\"Limit for sampled traces per second must be nonnegative\");\n }\n const adaptationTimeSeconds = 0.1;\n this.nanoTimeSupplier = () => Number(process.hrtime.bigint());\n this.inverseAdaptationTimeNanos = 1e-9 / adaptationTimeSeconds;\n this.targetSpansPerNanosecondLimit = 1e-9 * this.tracesPerSecond;\n const now = this.nanoTimeSupplier();\n this.state = {\n effectiveWindowCount: 0,\n effectiveWindowNanos: 0,\n lastNanoTime: now,\n };\n this.roundToNearest = true;\n }\n\n /**\n * Updates the state of the sampler based on the current time.\n * This method calculates the effective window count and nanos based on the time elapsed since the last sample.\n * @param oldState - The previous state of the sampler.\n * @param currentNanoTime - The current time in nanoseconds.\n * @returns The updated state of the sampler.\n */\n private updateState(\n oldState: RateLimitedSamplerState,\n currentNanoTime: number,\n ): RateLimitedSamplerState {\n if (currentNanoTime <= oldState.lastNanoTime) {\n return {\n effectiveWindowCount: oldState.effectiveWindowCount + 1,\n effectiveWindowNanos: oldState.effectiveWindowNanos,\n lastNanoTime: oldState.lastNanoTime,\n };\n }\n const nanoTimeDelta = currentNanoTime - oldState.lastNanoTime;\n const decayFactor = Math.exp(-nanoTimeDelta * this.inverseAdaptationTimeNanos);\n const currentEffectiveWindowCount = oldState.effectiveWindowCount * decayFactor + 1;\n const currentEffectiveWindowNanos = oldState.effectiveWindowNanos * decayFactor + nanoTimeDelta;\n return {\n effectiveWindowCount: currentEffectiveWindowCount,\n effectiveWindowNanos: currentEffectiveWindowNanos,\n lastNanoTime: currentNanoTime,\n };\n }\n\n /**\n * Gets the current sample rate based on the effective window count and nanos.\n * This method calculates the sampling probability and returns it as a percentage.\n * If `roundToNearest` is true, it rounds down the sampling percentage to the nearest whole number.\n * @returns The current sample rate as a percentage.\n */\n public getSampleRate(): number {\n const currentNanoTime = this.nanoTimeSupplier();\n this.state = this.updateState(this.state, currentNanoTime);\n\n const samplingProbability =\n (this.state.effectiveWindowNanos * this.targetSpansPerNanosecondLimit) /\n this.state.effectiveWindowCount;\n let samplingPercentage = 100 * Math.min(samplingProbability, 1);\n\n if (this.roundToNearest) {\n samplingPercentage = roundDownToNearest(samplingPercentage);\n }\n return samplingPercentage;\n }\n\n /**\n * Checks whether span needs to be created and tracked.\n *\n * @param context - Parent Context which may contain a span.\n * @param traceId - traceId of the span to be created. It can be different from the\n * traceId in the {@link SpanContext}. Typically in situations when the\n * span to be created starts a new trace.\n * @param spanName - Name of the span to be created.\n * @param spanKind - Kind of the span to be created.\n * @param attributes - Initial set of SpanAttributes for the Span being constructed.\n * @param links - Collection of links that will be associated with the Span to\n * be created. Typically useful for batch operations.\n * @returns a {@link SamplingResult}.\n */\n public shouldSample(\n context: Context,\n traceId: string,\n // @ts-expect-error unused argument\n spanName: string,\n // @ts-expect-error unused argument\n spanKind: SpanKind,\n attributes: Attributes,\n // @ts-expect-error unused argument\n links: Link[],\n ): SamplingResult {\n const sampleRate = this.getSampleRate();\n return shouldSample(sampleRate, context, traceId, attributes)\n ? { decision: SamplingDecision.RECORD_AND_SAMPLED, attributes: attributes }\n : { decision: SamplingDecision.NOT_RECORD, attributes: attributes };\n }\n\n /**\n * Return Sampler description\n */\n public toString(): string {\n return `RateLimitedSampler{${this.tracesPerSecond}}`;\n }\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Attributes, Context } from "@opentelemetry/api";
|
|
2
|
+
/**
|
|
3
|
+
* Computes a deterministic hash code from a string input (typically a trace ID)
|
|
4
|
+
* and returns a value between 0 and 100 for sampling decisions.
|
|
5
|
+
*
|
|
6
|
+
* This function replicates the C# hash algorithm used in Application Insights
|
|
7
|
+
* to ensure consistent sampling decisions across different SDKs and languages.
|
|
8
|
+
* The same trace ID will always produce the same hash value, enabling
|
|
9
|
+
* distributed sampling where all spans in a trace are sampled consistently.
|
|
10
|
+
*
|
|
11
|
+
* @param input - The input string to hash (usually a trace ID)
|
|
12
|
+
* @returns A number between 0 and 100 representing the hash-based sampling score
|
|
13
|
+
*/
|
|
14
|
+
export declare function getSamplingHashCode(input: string): number;
|
|
15
|
+
export declare function roundDownToNearest(samplingPercentage: number): number;
|
|
16
|
+
export declare function shouldSample(samplePercentage: number, context: Context, traceId: string, attributes: Attributes): boolean;
|
|
17
|
+
//# sourceMappingURL=samplingUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"samplingUtils.d.ts","sourceRoot":"","sources":["../../../src/sampling/samplingUtils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAI9D;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAyBzD;AAED,wBAAgB,kBAAkB,CAAC,kBAAkB,EAAE,MAAM,GAAG,MAAM,CAMrE;AAED,wBAAgB,YAAY,CAC1B,gBAAgB,EAAE,MAAM,EACxB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,UAAU,GACrB,OAAO,CA2CT"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation.
|
|
3
|
+
// Licensed under the MIT License.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.getSamplingHashCode = getSamplingHashCode;
|
|
6
|
+
exports.roundDownToNearest = roundDownToNearest;
|
|
7
|
+
exports.shouldSample = shouldSample;
|
|
8
|
+
const api_1 = require("@opentelemetry/api");
|
|
9
|
+
const applicationinsights_js_1 = require("../utils/constants/applicationinsights.js");
|
|
10
|
+
/**
|
|
11
|
+
* Computes a deterministic hash code from a string input (typically a trace ID)
|
|
12
|
+
* and returns a value between 0 and 100 for sampling decisions.
|
|
13
|
+
*
|
|
14
|
+
* This function replicates the C# hash algorithm used in Application Insights
|
|
15
|
+
* to ensure consistent sampling decisions across different SDKs and languages.
|
|
16
|
+
* The same trace ID will always produce the same hash value, enabling
|
|
17
|
+
* distributed sampling where all spans in a trace are sampled consistently.
|
|
18
|
+
*
|
|
19
|
+
* @param input - The input string to hash (usually a trace ID)
|
|
20
|
+
* @returns A number between 0 and 100 representing the hash-based sampling score
|
|
21
|
+
*/
|
|
22
|
+
function getSamplingHashCode(input) {
|
|
23
|
+
const csharpMin = -2147483648;
|
|
24
|
+
const csharpMax = 2147483647;
|
|
25
|
+
let hash = 5381;
|
|
26
|
+
if (!input) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
// Ensure input is at least 8 characters long by repeating it
|
|
30
|
+
let processedInput = input;
|
|
31
|
+
while (processedInput.length < 8) {
|
|
32
|
+
processedInput = processedInput + processedInput;
|
|
33
|
+
}
|
|
34
|
+
// Compute hash using a variation of djb2 algorithm with C# integer overflow simulation
|
|
35
|
+
// This uses hash * 33 + c (where hash << 5 + hash equals hash * 33)
|
|
36
|
+
for (let i = 0; i < processedInput.length; i++) {
|
|
37
|
+
// JS doesn't respond to integer overflow by wrapping around. Simulate it with bitwise operators ( | 0)
|
|
38
|
+
hash = ((((hash << 5) + hash) | 0) + processedInput.charCodeAt(i)) | 0;
|
|
39
|
+
}
|
|
40
|
+
// Normalize hash to positive value and convert to 0-100 range
|
|
41
|
+
hash = hash <= csharpMin ? csharpMax : Math.abs(hash);
|
|
42
|
+
return (hash / csharpMax) * 100;
|
|
43
|
+
}
|
|
44
|
+
function roundDownToNearest(samplingPercentage) {
|
|
45
|
+
if (samplingPercentage === 0) {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
const itemCount = 100 / samplingPercentage;
|
|
49
|
+
return 100.0 / Math.ceil(itemCount);
|
|
50
|
+
}
|
|
51
|
+
function shouldSample(samplePercentage, context, traceId, attributes) {
|
|
52
|
+
let sampleRate = samplePercentage;
|
|
53
|
+
let isSampled = undefined;
|
|
54
|
+
if (sampleRate === 100) {
|
|
55
|
+
isSampled = true;
|
|
56
|
+
}
|
|
57
|
+
else if (sampleRate === 0) {
|
|
58
|
+
isSampled = false;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Try to get the parent sampling result first
|
|
62
|
+
const parentSpan = api_1.trace.getSpan(context);
|
|
63
|
+
const parentSpanContext = parentSpan?.spanContext();
|
|
64
|
+
if (parentSpanContext &&
|
|
65
|
+
api_1.trace.isSpanContextValid(parentSpanContext) &&
|
|
66
|
+
!parentSpanContext.isRemote) {
|
|
67
|
+
if ((parentSpanContext.traceFlags & api_1.TraceFlags.SAMPLED) === api_1.TraceFlags.SAMPLED) {
|
|
68
|
+
isSampled = true;
|
|
69
|
+
}
|
|
70
|
+
else if ((parentSpanContext.traceFlags & api_1.TraceFlags.NONE) === api_1.TraceFlags.NONE) {
|
|
71
|
+
isSampled = false;
|
|
72
|
+
}
|
|
73
|
+
// If the parent span is valid and not remote, we can use its sample rate
|
|
74
|
+
const parentSampleRate = Number(parentSpan.attributes?.[applicationinsights_js_1.AzureMonitorSampleRate]);
|
|
75
|
+
if (!isNaN(parentSampleRate)) {
|
|
76
|
+
sampleRate = Number(parentSampleRate);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Only add sample rate attribute if it's not 100
|
|
81
|
+
if (sampleRate !== 100) {
|
|
82
|
+
// Add sample rate as span attribute
|
|
83
|
+
attributes = attributes || {};
|
|
84
|
+
attributes[applicationinsights_js_1.AzureMonitorSampleRate] = sampleRate;
|
|
85
|
+
}
|
|
86
|
+
if (isSampled === undefined) {
|
|
87
|
+
const samplingHashCode = getSamplingHashCode(traceId);
|
|
88
|
+
return samplingHashCode < sampleRate;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return isSampled;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=samplingUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"samplingUtils.js","sourceRoot":"","sources":["../../../src/sampling/samplingUtils.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAkBlC,kDAyBC;AAED,gDAMC;AAED,oCAgDC;AAlG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9B,UAAU,CAAC,+CAAsB,CAAC,GAAG,UAAU,CAAC;IAClD,CAAC;IAED,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;QACtD,OAAO,gBAAgB,GAAG,UAAU,CAAC;IACvC,CAAC;SAAM,CAAC;QACN,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { Attributes, Context } from \"@opentelemetry/api\";\nimport { trace, TraceFlags } from \"@opentelemetry/api\";\nimport { AzureMonitorSampleRate } from \"../utils/constants/applicationinsights.js\";\n\n/**\n * Computes a deterministic hash code from a string input (typically a trace ID)\n * and returns a value between 0 and 100 for sampling decisions.\n *\n * This function replicates the C# hash algorithm used in Application Insights\n * to ensure consistent sampling decisions across different SDKs and languages.\n * The same trace ID will always produce the same hash value, enabling\n * distributed sampling where all spans in a trace are sampled consistently.\n *\n * @param input - The input string to hash (usually a trace ID)\n * @returns A number between 0 and 100 representing the hash-based sampling score\n */\nexport function getSamplingHashCode(input: string): number {\n const csharpMin = -2147483648;\n const csharpMax = 2147483647;\n let hash = 5381;\n\n if (!input) {\n return 0;\n }\n\n // Ensure input is at least 8 characters long by repeating it\n let processedInput = input;\n while (processedInput.length < 8) {\n processedInput = processedInput + processedInput;\n }\n\n // Compute hash using a variation of djb2 algorithm with C# integer overflow simulation\n // This uses hash * 33 + c (where hash << 5 + hash equals hash * 33)\n for (let i = 0; i < processedInput.length; i++) {\n // JS doesn't respond to integer overflow by wrapping around. Simulate it with bitwise operators ( | 0)\n hash = ((((hash << 5) + hash) | 0) + processedInput.charCodeAt(i)) | 0;\n }\n\n // Normalize hash to positive value and convert to 0-100 range\n hash = hash <= csharpMin ? csharpMax : Math.abs(hash);\n return (hash / csharpMax) * 100;\n}\n\nexport function roundDownToNearest(samplingPercentage: number): number {\n if (samplingPercentage === 0) {\n return 0;\n }\n const itemCount = 100 / samplingPercentage;\n return 100.0 / Math.ceil(itemCount);\n}\n\nexport function shouldSample(\n samplePercentage: number,\n context: Context,\n traceId: string,\n attributes: Attributes,\n): boolean {\n let sampleRate = samplePercentage;\n let isSampled = undefined;\n\n if (sampleRate === 100) {\n isSampled = true;\n } else if (sampleRate === 0) {\n isSampled = false;\n } else {\n // Try to get the parent sampling result first\n const parentSpan = trace.getSpan(context);\n const parentSpanContext = parentSpan?.spanContext();\n if (\n parentSpanContext &&\n trace.isSpanContextValid(parentSpanContext) &&\n !parentSpanContext.isRemote\n ) {\n if ((parentSpanContext.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED) {\n isSampled = true;\n } else if ((parentSpanContext.traceFlags & TraceFlags.NONE) === TraceFlags.NONE) {\n isSampled = false;\n }\n // If the parent span is valid and not remote, we can use its sample rate\n const parentSampleRate = Number((parentSpan as any).attributes?.[AzureMonitorSampleRate]);\n if (!isNaN(parentSampleRate)) {\n sampleRate = Number(parentSampleRate);\n }\n }\n }\n\n // Only add sample rate attribute if it's not 100\n if (sampleRate !== 100) {\n // Add sample rate as span attribute\n attributes = attributes || {};\n attributes[AzureMonitorSampleRate] = sampleRate;\n }\n\n if (isSampled === undefined) {\n const samplingHashCode = getSamplingHashCode(traceId);\n return samplingHashCode < sampleRate;\n } else {\n return isSampled;\n }\n}\n"]}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
-
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
-
{
|
|
4
|
-
"tsdocVersion": "0.12",
|
|
5
|
-
"toolPackages": [
|
|
6
|
-
{
|
|
7
|
-
"packageName": "@microsoft/api-extractor",
|
|
8
|
-
"packageVersion": "7.52.
|
|
9
|
-
}
|
|
10
|
-
]
|
|
11
|
-
}
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.52.10"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAuDA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D;;;GAGG;AACH,MAAM,MAAM,IAAI,GAAG;KAAG,GAAG,IAAI,cAAc,GAAG,MAAM;CAAE,CAAC;AACvD;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;AACpF;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,YAAY,CAAA;CAAE,CAAC;AACtE;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,EAAE,EAAE,MAAM,CAAC;CACZ;AACD;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC;AACrD;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IAAE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9E;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C;AAED;;;GAGG;AACH,oBAAY,2BAA2B;IACrC,aAAa,kBAAkB;IAC/B,eAAe,oBAAoB;IACnC,cAAc,mBAAmB;IACjC,qBAAqB,0BAA0B;IAC/C,YAAY,iBAAiB;IAC7B,gBAAgB,2BAA2B;IAC3C,uBAAuB,4BAA4B;IACnD,cAAc,mBAAmB;CAClC;AAED;;;GAGG;AACH,oBAAY,6BAA6B;IACvC,aAAa,iDAAiD;IAC9D,eAAe,8BAA8B;IAC7C,cAAc,0CAA0C;IACxD,qBAAqB,oDAAoD;IACzE,YAAY,6DAA6D;IACzE,gBAAgB,uEAAuE;IACvF,uBAAuB,+DAA+D;IACtF,cAAc,sEAAsE;CACrF;AAED;;;GAGG;AACH,oBAAY,kBAAkB;IAC5B,QAAQ,MAAM;IACd,OAAO,OAAO;IACd,YAAY,OAAO;IACnB,WAAW,QAAQ;CACpB;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,UA8BhC,CAAC;AAEF;;;GAGG;AACH,oBAAY,+BAA+B;IACzC,cAAc,8BAA8B;CAC7C;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,8eAwB9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,2BAA2B,UAAsC,CAAC"}
|
package/dist/commonjs/types.js
CHANGED
|
@@ -77,6 +77,7 @@ exports.legacySemanticValues = [
|
|
|
77
77
|
semantic_conventions_1.SEMATTRS_NET_HOST_PORT,
|
|
78
78
|
semantic_conventions_1.SEMATTRS_NET_PEER_PORT,
|
|
79
79
|
semantic_conventions_1.SEMATTRS_HTTP_CLIENT_IP,
|
|
80
|
+
semantic_conventions_1.SEMATTRS_ENDUSER_ID,
|
|
80
81
|
"http.status_text",
|
|
81
82
|
];
|
|
82
83
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,8EAmD6C;AA8C7C;;;GAGG;AACH,IAAY,2BASX;AATD,WAAY,2BAA2B;IACrC,8DAA+B,CAAA;IAC/B,kEAAmC,CAAA;IACnC,gEAAiC,CAAA;IACjC,8EAA+C,CAAA;IAC/C,4DAA6B,CAAA;IAC7B,0EAA2C,CAAA;IAC3C,kFAAmD,CAAA;IACnD,gEAAiC,CAAA;AACnC,CAAC,EATW,2BAA2B,2CAA3B,2BAA2B,QAStC;AAED;;;GAGG;AACH,IAAY,6BASX;AATD,WAAY,6BAA6B;IACvC,+FAA8D,CAAA;IAC9D,8EAA6C,CAAA;IAC7C,yFAAwD,CAAA;IACxD,0GAAyE,CAAA;IACzE,0GAAyE,CAAA;IACzE,wHAAuF,CAAA;IACvF,uHAAsF,CAAA;IACtF,qHAAoF,CAAA;AACtF,CAAC,EATW,6BAA6B,6CAA7B,6BAA6B,QASxC;AAED;;;GAGG;AACH,IAAY,kBAKX;AALD,WAAY,kBAAkB;IAC5B,qEAAc,CAAA;IACd,oEAAc,CAAA;IACd,8EAAmB,CAAA;IACnB,6EAAmB,CAAA;AACrB,CAAC,EALW,kBAAkB,kCAAlB,kBAAkB,QAK7B;AAED;;;GAGG;AACU,QAAA,oBAAoB,GAAG;IAClC,2CAAoB;IACpB,6CAAsB;IACtB,2CAAoB;IACpB,4CAAqB;IACrB,+CAAwB;IACxB,2CAAoB;IACpB,wCAAiB;IACjB,gDAAyB;IACzB,0CAAmB;IACnB,yCAAkB;IAClB,yCAAkB;IAClB,4CAAqB;IACrB,4CAAqB;IACrB,uCAAgB;IAChB,0CAAmB;IACnB,oDAA6B;IAC7B,8CAAuB;IACvB,iDAA0B;IAC1B,oDAA6B;IAC7B,2CAAoB;IACpB,2CAAoB;IACpB,2CAAoB;IACpB,6CAAsB;IACtB,6CAAsB;IACtB,6CAAsB;IACtB,6CAAsB;IACtB,8CAAuB;IACvB,0CAAmB;IACnB,kBAAkB;CACnB,CAAC;AAEF;;;GAGG;AACH,IAAY,+BAEX;AAFD,WAAY,+BAA+B;IACzC,+EAA4C,CAAA;AAC9C,CAAC,EAFW,+BAA+B,+CAA/B,+BAA+B,QAE1C;AAED;;;GAGG;AACU,QAAA,kBAAkB,GAAG;IAChC,0CAAmB;IACnB,uCAAgB;IAChB,0CAAmB;IACnB,uCAAgB;IAChB,oCAAa;IACb,oCAAa;IACb,qCAAc;IACd,sCAAe;IACf,sCAAe;IACf,iDAA0B;IAC1B,8CAAuB;IACvB,iDAA0B;IAC1B,gDAAyB;IACzB,6CAAsB;IACtB,oDAA6B;IAC7B,6CAAsB;IACtB,+CAAwB;IACxB,+CAAwB;IACxB,qDAA8B;IAC9B,0CAAmB;IACnB,6CAAsB;IACtB,gDAAyB;IACzB,+BAA+B,CAAC,cAAc;CAC/C,CAAC;AAEF;;;GAGG;AACU,QAAA,2BAA2B,GAAG,CAAC,iCAAiC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport {\n ATTR_CLIENT_ADDRESS,\n ATTR_CLIENT_PORT,\n ATTR_ERROR_TYPE,\n ATTR_EXCEPTION_MESSAGE,\n ATTR_EXCEPTION_STACKTRACE,\n ATTR_EXCEPTION_TYPE,\n ATTR_HTTP_REQUEST_METHOD,\n ATTR_HTTP_RESPONSE_STATUS_CODE,\n ATTR_NETWORK_LOCAL_ADDRESS,\n ATTR_NETWORK_LOCAL_PORT,\n ATTR_NETWORK_PEER_ADDRESS,\n ATTR_NETWORK_PEER_PORT,\n ATTR_NETWORK_PROTOCOL_NAME,\n ATTR_NETWORK_PROTOCOL_VERSION,\n ATTR_NETWORK_TRANSPORT,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT,\n ATTR_URL_FULL,\n ATTR_URL_PATH,\n ATTR_URL_QUERY,\n ATTR_URL_SCHEME,\n ATTR_USER_AGENT_ORIGINAL,\n SEMATTRS_DB_NAME,\n SEMATTRS_DB_OPERATION,\n SEMATTRS_DB_STATEMENT,\n SEMATTRS_DB_SYSTEM,\n SEMATTRS_ENDUSER_ID,\n SEMATTRS_EXCEPTION_MESSAGE,\n SEMATTRS_EXCEPTION_STACKTRACE,\n SEMATTRS_EXCEPTION_TYPE,\n SEMATTRS_HTTP_CLIENT_IP,\n SEMATTRS_HTTP_FLAVOR,\n SEMATTRS_HTTP_HOST,\n SEMATTRS_HTTP_METHOD,\n SEMATTRS_HTTP_ROUTE,\n SEMATTRS_HTTP_SCHEME,\n SEMATTRS_HTTP_STATUS_CODE,\n SEMATTRS_HTTP_TARGET,\n SEMATTRS_HTTP_URL,\n SEMATTRS_HTTP_USER_AGENT,\n SEMATTRS_NET_HOST_IP,\n SEMATTRS_NET_HOST_NAME,\n SEMATTRS_NET_HOST_PORT,\n SEMATTRS_NET_PEER_IP,\n SEMATTRS_NET_PEER_NAME,\n SEMATTRS_NET_PEER_PORT,\n SEMATTRS_NET_TRANSPORT,\n SEMATTRS_PEER_SERVICE,\n SEMATTRS_RPC_GRPC_STATUS_CODE,\n SEMATTRS_RPC_SYSTEM,\n} from \"@opentelemetry/semantic-conventions\";\nimport type { ContextTagKeys } from \"./generated/index.js\";\n\n/**\n * Azure Monitor envelope tags.\n * @internal\n */\nexport type Tags = { [key in ContextTagKeys]: string };\n/**\n * Azure Monitor envelope property type.\n * @internal\n */\nexport type PropertyType = string | number | boolean | object | Array<PropertyType>;\n/**\n * Azure Monitor envelope properties.\n * @internal\n */\nexport type Properties = { [key: string]: Properties | PropertyType };\n/**\n * Azure Monitor envelope links.\n * @internal\n */\nexport interface MSLink {\n operation_Id: string;\n id: string;\n}\n/**\n * Azure Monitor envelope measurements.\n * @internal\n */\nexport type Measurements = { [key: string]: number };\n/**\n * Exporter sender result.\n * @internal\n */\nexport type SenderResult = { statusCode: number | undefined; result: string };\n\n/**\n * Exporter persistent storage.\n * @internal\n */\nexport interface PersistentStorage {\n shift(): Promise<unknown>;\n push(value: unknown[]): Promise<boolean>;\n}\n\n/**\n * Performance Counter OpenTelemetry compliant names.\n * @internal\n */\nexport enum OTelPerformanceCounterNames {\n PRIVATE_BYTES = \"Private_Bytes\",\n AVAILABLE_BYTES = \"Available_Bytes\",\n PROCESSOR_TIME = \"Processor_Time\",\n PROCESS_TIME_STANDARD = \"Process_Time_Standard\",\n REQUEST_RATE = \"Request_Rate\",\n REQUEST_DURATION = \"Request_Execution_Time\",\n PROCESS_TIME_NORMALIZED = \"Process_Time_Normalized\",\n EXCEPTION_RATE = \"Exception_Rate\",\n}\n\n/**\n * Breeze Performance Counter names.\n * @internal\n */\nexport enum BreezePerformanceCounterNames {\n PRIVATE_BYTES = \"\\\\Process(??APP_WIN32_PROC??)\\\\Private Bytes\",\n AVAILABLE_BYTES = \"\\\\Memory\\\\Available Bytes\",\n PROCESSOR_TIME = \"\\\\Processor(_Total)\\\\% Processor Time\",\n PROCESS_TIME_STANDARD = \"\\\\Process(??APP_WIN32_PROC??)\\\\% Processor Time\",\n REQUEST_RATE = \"\\\\ASP.NET Applications(??APP_W3SVC_PROC??)\\\\Requests/Sec\",\n REQUEST_DURATION = \"\\\\ASP.NET Applications(??APP_W3SVC_PROC??)\\\\Request Execution Time\",\n PROCESS_TIME_NORMALIZED = \"\\\\Process(??APP_WIN32_PROC??)\\\\% Processor Time Normalized\",\n EXCEPTION_RATE = \"\\\\.NET CLR Exceptions(??APP_CLR_PROC??)\\\\# of Exceps Thrown / sec\",\n}\n\n/**\n * Property Max Lengths\n * @internal\n */\nexport enum MaxPropertyLengths {\n NINE_BIT = 512,\n TEN_BIT = 1024,\n THIRTEEN_BIT = 8192,\n FIFTEEN_BIT = 32768,\n}\n\n/**\n * Legacy HTTP semantic convention values\n * @internal\n */\nexport const legacySemanticValues = [\n SEMATTRS_NET_PEER_IP,\n SEMATTRS_NET_PEER_NAME,\n SEMATTRS_NET_HOST_IP,\n SEMATTRS_PEER_SERVICE,\n SEMATTRS_HTTP_USER_AGENT,\n SEMATTRS_HTTP_METHOD,\n SEMATTRS_HTTP_URL,\n SEMATTRS_HTTP_STATUS_CODE,\n SEMATTRS_HTTP_ROUTE,\n SEMATTRS_HTTP_HOST,\n SEMATTRS_DB_SYSTEM,\n SEMATTRS_DB_STATEMENT,\n SEMATTRS_DB_OPERATION,\n SEMATTRS_DB_NAME,\n SEMATTRS_RPC_SYSTEM,\n SEMATTRS_RPC_GRPC_STATUS_CODE,\n SEMATTRS_EXCEPTION_TYPE,\n SEMATTRS_EXCEPTION_MESSAGE,\n SEMATTRS_EXCEPTION_STACKTRACE,\n SEMATTRS_HTTP_SCHEME,\n SEMATTRS_HTTP_TARGET,\n SEMATTRS_HTTP_FLAVOR,\n SEMATTRS_NET_TRANSPORT,\n SEMATTRS_NET_HOST_NAME,\n SEMATTRS_NET_HOST_PORT,\n SEMATTRS_NET_PEER_PORT,\n SEMATTRS_HTTP_CLIENT_IP,\n SEMATTRS_ENDUSER_ID,\n \"http.status_text\",\n];\n\n/**\n * Experimental OpenTelemetry semantic convention values\n * @internal\n */\nexport enum experimentalOpenTelemetryValues {\n SYNTHETIC_TYPE = \"user_agent.synthetic.type\",\n}\n\n/**\n * HTTP semantic convention values\n * @internal\n */\nexport const httpSemanticValues = [\n ATTR_CLIENT_ADDRESS,\n ATTR_CLIENT_PORT,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT,\n ATTR_URL_FULL,\n ATTR_URL_PATH,\n ATTR_URL_QUERY,\n ATTR_URL_SCHEME,\n ATTR_ERROR_TYPE,\n ATTR_NETWORK_LOCAL_ADDRESS,\n ATTR_NETWORK_LOCAL_PORT,\n ATTR_NETWORK_PROTOCOL_NAME,\n ATTR_NETWORK_PEER_ADDRESS,\n ATTR_NETWORK_PEER_PORT,\n ATTR_NETWORK_PROTOCOL_VERSION,\n ATTR_NETWORK_TRANSPORT,\n ATTR_USER_AGENT_ORIGINAL,\n ATTR_HTTP_REQUEST_METHOD,\n ATTR_HTTP_RESPONSE_STATUS_CODE,\n ATTR_EXCEPTION_TYPE,\n ATTR_EXCEPTION_MESSAGE,\n ATTR_EXCEPTION_STACKTRACE,\n experimentalOpenTelemetryValues.SYNTHETIC_TYPE,\n];\n\n/**\n * Internal Microsoft attributes\n * @internal\n */\nexport const internalMicrosoftAttributes = [\"_MS.ProcessedByMetricExtractors\"];\n"]}
|
|
@@ -25,7 +25,7 @@ function hrTimeToDate(hrTime) {
|
|
|
25
25
|
}
|
|
26
26
|
function createTagsFromResource(resource) {
|
|
27
27
|
const context = (0, index_js_1.getInstance)();
|
|
28
|
-
const tags =
|
|
28
|
+
const tags = { ...context.tags };
|
|
29
29
|
if (resource && resource.attributes) {
|
|
30
30
|
tags[index_js_2.KnownContextTagKeys.AiCloudRole] = getCloudRole(resource);
|
|
31
31
|
tags[index_js_2.KnownContextTagKeys.AiCloudRoleInstance] = getCloudRoleInstance(resource);
|
|
@@ -247,8 +247,7 @@ function serializeAttribute(value) {
|
|
|
247
247
|
return String(value);
|
|
248
248
|
}
|
|
249
249
|
function shouldCreateResourceMetric() {
|
|
250
|
-
|
|
251
|
-
return !(((_a = process.env[Constants_js_1.ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "true");
|
|
250
|
+
return !(process.env[Constants_js_1.ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED]?.toLowerCase() === "true");
|
|
252
251
|
}
|
|
253
252
|
function isSyntheticSource(attributes) {
|
|
254
253
|
return !!attributes[types_js_1.experimentalOpenTelemetryValues.SYNTHETIC_TYPE];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/utils/common.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAmDlC,oCAEC;AAED,wDAqBC;AAmED,0BAYC;AAED,wBAkCC;AAED,kDAqBC;AAED,oEA2CC;AAED,g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E,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC;YAClE,CAAC;YAAC,OAAO,GAAY,EAAE,CAAC;gBACtB,0CAA0C;gBAC1C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;aAAM,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;YACvC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;YAAC,OAAO,GAAY,EAAE,CAAC;gBACtB,0CAA0C;gBAC1C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;IACH,CAAC;IACD,qCAAqC;IACrC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AAED,SAAgB,0BAA0B;;IACxC,OAAO,CAAC,CAAC,CAAA,MAAA,OAAO,CAAC,GAAG,CAAC,yDAA0C,CAAC,0CAAE,WAAW,EAAE,MAAK,MAAM,CAAC,CAAC;AAC9F,CAAC;AAED,SAAgB,iBAAiB,CAAC,UAAsB;IACtD,OAAO,CAAC,CAAC,UAAU,CAAC,0CAA+B,CAAC,cAAc,CAAC,CAAC;AACtE,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport os from \"node:os\";\nimport {\n SEMRESATTRS_DEVICE_ID,\n SEMRESATTRS_DEVICE_MODEL_NAME,\n SEMRESATTRS_SERVICE_VERSION,\n SEMRESATTRS_K8S_POD_NAME,\n SEMRESATTRS_SERVICE_INSTANCE_ID,\n DBSYSTEMVALUES_DB2,\n DBSYSTEMVALUES_DERBY,\n DBSYSTEMVALUES_MARIADB,\n DBSYSTEMVALUES_MSSQL,\n DBSYSTEMVALUES_ORACLE,\n DBSYSTEMVALUES_SQLITE,\n DBSYSTEMVALUES_OTHER_SQL,\n DBSYSTEMVALUES_HSQLDB,\n SEMATTRS_PEER_SERVICE,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_K8S_DEPLOYMENT_NAME,\n SEMRESATTRS_K8S_REPLICASET_NAME,\n SEMRESATTRS_K8S_STATEFULSET_NAME,\n SEMRESATTRS_K8S_JOB_NAME,\n SEMRESATTRS_K8S_CRONJOB_NAME,\n SEMRESATTRS_K8S_DAEMONSET_NAME,\n ATTR_TELEMETRY_SDK_VERSION,\n ATTR_TELEMETRY_SDK_LANGUAGE,\n ATTR_TELEMETRY_SDK_NAME,\n DBSYSTEMVALUES_H2,\n} from \"@opentelemetry/semantic-conventions\";\nimport { experimentalOpenTelemetryValues, type Tags } from \"../types.js\";\nimport { getInstance } from \"../platform/index.js\";\nimport type { TelemetryItem as Envelope, MetricsData } from \"../generated/index.js\";\nimport { KnownContextTagKeys } from \"../generated/index.js\";\nimport type { Resource } from \"@opentelemetry/resources\";\nimport type { Attributes, HrTime } from \"@opentelemetry/api\";\nimport { hrTimeToNanoseconds } from \"@opentelemetry/core\";\nimport type { AnyValue } from \"@opentelemetry/api-logs\";\nimport { ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED } from \"../Declarations/Constants.js\";\nimport {\n getHttpHost,\n getHttpMethod,\n getHttpScheme,\n getHttpTarget,\n getHttpUrl,\n getNetPeerName,\n getNetPeerPort,\n getPeerIp,\n} from \"./spanUtils.js\";\n\nexport function hrTimeToDate(hrTime: HrTime): Date {\n return new Date(hrTimeToNanoseconds(hrTime) / 1000000);\n}\n\nexport function createTagsFromResource(resource: Resource): Tags {\n const context = getInstance();\n const tags: Tags = { ...context.tags };\n if (resource && resource.attributes) {\n tags[KnownContextTagKeys.AiCloudRole] = getCloudRole(resource);\n tags[KnownContextTagKeys.AiCloudRoleInstance] = getCloudRoleInstance(resource);\n if (resource.attributes[SEMRESATTRS_DEVICE_ID]) {\n tags[KnownContextTagKeys.AiDeviceId] = String(resource.attributes[SEMRESATTRS_DEVICE_ID]);\n }\n if (resource.attributes[SEMRESATTRS_DEVICE_MODEL_NAME]) {\n tags[KnownContextTagKeys.AiDeviceModel] = String(\n resource.attributes[SEMRESATTRS_DEVICE_MODEL_NAME],\n );\n }\n if (resource.attributes[SEMRESATTRS_SERVICE_VERSION]) {\n tags[KnownContextTagKeys.AiApplicationVer] = String(\n resource.attributes[SEMRESATTRS_SERVICE_VERSION],\n );\n }\n }\n return tags;\n}\n\nfunction getCloudRole(resource: Resource): string {\n let cloudRole = \"\";\n // Service attributes\n const serviceName = resource.attributes[SEMRESATTRS_SERVICE_NAME];\n const serviceNamespace = resource.attributes[SEMRESATTRS_SERVICE_NAMESPACE];\n if (serviceName) {\n // Custom Service name provided by customer is highest precedence\n if (!String(serviceName).startsWith(\"unknown_service\")) {\n if (serviceNamespace) {\n return `${serviceNamespace}.${serviceName}`;\n } else {\n return String(serviceName);\n }\n } else {\n // Service attributes will be only used if K8S attributes are not present\n if (serviceNamespace) {\n cloudRole = `${serviceNamespace}.${serviceName}`;\n } else {\n cloudRole = String(serviceName);\n }\n }\n }\n // Kubernetes attributes should take precedence\n const kubernetesDeploymentName = resource.attributes[SEMRESATTRS_K8S_DEPLOYMENT_NAME];\n if (kubernetesDeploymentName) {\n return String(kubernetesDeploymentName);\n }\n const kuberneteReplicasetName = resource.attributes[SEMRESATTRS_K8S_REPLICASET_NAME];\n if (kuberneteReplicasetName) {\n return String(kuberneteReplicasetName);\n }\n const kubernetesStatefulSetName = resource.attributes[SEMRESATTRS_K8S_STATEFULSET_NAME];\n if (kubernetesStatefulSetName) {\n return String(kubernetesStatefulSetName);\n }\n const kubernetesJobName = resource.attributes[SEMRESATTRS_K8S_JOB_NAME];\n if (kubernetesJobName) {\n return String(kubernetesJobName);\n }\n const kubernetesCronjobName = resource.attributes[SEMRESATTRS_K8S_CRONJOB_NAME];\n if (kubernetesCronjobName) {\n return String(kubernetesCronjobName);\n }\n const kubernetesDaemonsetName = resource.attributes[SEMRESATTRS_K8S_DAEMONSET_NAME];\n if (kubernetesDaemonsetName) {\n return String(kubernetesDaemonsetName);\n }\n return cloudRole;\n}\n\nfunction getCloudRoleInstance(resource: Resource): string {\n // Kubernetes attributes should take precedence\n const kubernetesPodName = resource.attributes[SEMRESATTRS_K8S_POD_NAME];\n if (kubernetesPodName) {\n return String(kubernetesPodName);\n }\n // Service attributes\n const serviceInstanceId = resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID];\n if (serviceInstanceId) {\n return String(serviceInstanceId);\n }\n // Default\n return os && os.hostname();\n}\n\nexport function isSqlDB(dbSystem: string): boolean {\n return (\n dbSystem === DBSYSTEMVALUES_DB2 ||\n dbSystem === DBSYSTEMVALUES_DERBY ||\n dbSystem === DBSYSTEMVALUES_MARIADB ||\n dbSystem === DBSYSTEMVALUES_MSSQL ||\n dbSystem === DBSYSTEMVALUES_ORACLE ||\n dbSystem === DBSYSTEMVALUES_SQLITE ||\n dbSystem === DBSYSTEMVALUES_OTHER_SQL ||\n dbSystem === DBSYSTEMVALUES_HSQLDB ||\n dbSystem === DBSYSTEMVALUES_H2\n );\n}\n\nexport function getUrl(attributes: Attributes): string {\n if (!attributes) {\n return \"\";\n }\n const httpMethod = getHttpMethod(attributes);\n if (httpMethod) {\n const httpUrl = getHttpUrl(attributes);\n if (httpUrl) {\n return String(httpUrl);\n } else {\n const httpScheme = getHttpScheme(attributes);\n const httpTarget = getHttpTarget(attributes);\n if (httpScheme && httpTarget) {\n const httpHost = getHttpHost(attributes);\n if (httpHost) {\n return `${httpScheme}://${httpHost}${httpTarget}`;\n } else {\n const netPeerPort = getNetPeerPort(attributes);\n if (netPeerPort) {\n const netPeerName = getNetPeerName(attributes);\n if (netPeerName) {\n return `${httpScheme}://${netPeerName}:${netPeerPort}${httpTarget}`;\n } else {\n const netPeerIp = getPeerIp(attributes);\n if (netPeerIp) {\n return `${httpScheme}://${netPeerIp}:${netPeerPort}${httpTarget}`;\n }\n }\n }\n }\n }\n }\n }\n return \"\";\n}\n\nexport function getDependencyTarget(attributes: Attributes): string {\n if (!attributes) {\n return \"\";\n }\n const peerService = attributes[SEMATTRS_PEER_SERVICE];\n const httpHost = getHttpHost(attributes);\n const httpUrl = getHttpUrl(attributes);\n const netPeerName = getNetPeerName(attributes);\n const netPeerIp = getPeerIp(attributes);\n if (peerService) {\n return String(peerService);\n } else if (httpHost) {\n return String(httpHost);\n } else if (httpUrl) {\n return String(httpUrl);\n } else if (netPeerName) {\n return String(netPeerName);\n } else if (netPeerIp) {\n return String(netPeerIp);\n }\n return \"\";\n}\n\nexport function createResourceMetricEnvelope(\n resource: Resource,\n instrumentationKey: string,\n): Envelope | undefined {\n if (resource && resource.attributes) {\n const tags = createTagsFromResource(resource);\n const resourceAttributes: { [propertyName: string]: string } = {};\n for (const key of Object.keys(resource.attributes)) {\n // Avoid duplication ignoring fields already mapped.\n if (\n !(\n key.startsWith(\"_MS.\") ||\n key === ATTR_TELEMETRY_SDK_VERSION ||\n key === ATTR_TELEMETRY_SDK_LANGUAGE ||\n key === ATTR_TELEMETRY_SDK_NAME\n )\n ) {\n resourceAttributes[key] = resource.attributes[key] as string;\n }\n }\n // Only send event when resource attributes are available\n if (Object.keys(resourceAttributes).length > 0) {\n const baseData: MetricsData = {\n version: 2,\n metrics: [{ name: \"_OTELRESOURCE_\", value: 1 }],\n properties: resourceAttributes,\n };\n const envelope: Envelope = {\n name: \"Microsoft.ApplicationInsights.Metric\",\n time: new Date(),\n sampleRate: 100, // Metrics are never sampled\n instrumentationKey: instrumentationKey,\n version: 1,\n data: {\n baseType: \"MetricData\",\n baseData: baseData,\n },\n tags: tags,\n };\n return envelope;\n }\n }\n return;\n}\n\nexport function serializeAttribute(value: AnyValue): string {\n if (typeof value === \"object\") {\n if (value instanceof Error) {\n try {\n return JSON.stringify(value, Object.getOwnPropertyNames(value));\n } catch (err: unknown) {\n // Failed to serialize, return string cast\n return String(value);\n }\n } else if (value instanceof Uint8Array) {\n return String(value);\n } else {\n try {\n return JSON.stringify(value);\n } catch (err: unknown) {\n // Failed to serialize, return string cast\n return String(value);\n }\n }\n }\n // Return scalar and undefined values\n return String(value);\n}\n\nexport function shouldCreateResourceMetric(): boolean {\n return !(process.env[ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED]?.toLowerCase() === \"true\");\n}\n\nexport function isSyntheticSource(attributes: Attributes): boolean {\n return !!attributes[experimentalOpenTelemetryValues.SYNTHETIC_TYPE];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/utils/common.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAmDlC,oCAEC;AAED,wDAqBC;AAmED,0BAYC;AAED,wBAkCC;AAED,kDAqBC;AAED,oEA2CC;AAED,gDAsBC;AAED,gEAEC;AAED,8CAEC;;AAjSD,8DAAyB;AACzB,8EA2B6C;AAC7C,0CAAyE;AACzE,mDAAmD;AAEnD,oDAA4D;AAG5D,8CAA0D;AAE1D,+DAA0F;AAC1F,iDASwB;AAExB,SAAgB,YAAY,CAAC,MAAc;IACzC,OAAO,IAAI,IAAI,CAAC,IAAA,0BAAmB,EAAC,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC;AACzD,CAAC;AAED,SAAgB,sBAAsB,CAAC,QAAkB;IACvD,MAAM,OAAO,GAAG,IAAA,sBAAW,GAAE,CAAC;IAC9B,MAAM,IAAI,GAAS,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IACvC,IAAI,QAAQ,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;QACpC,IAAI,CAAC,8BAAmB,CAAC,WAAW,CAAC,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC/D,IAAI,CAAC,8BAAmB,CAAC,mBAAmB,CAAC,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QAC/E,IAAI,QAAQ,CAAC,UAAU,CAAC,4CAAqB,CAAC,EAAE,CAAC;YAC/C,IAAI,CAAC,8BA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,OAAO,CAAC,CAAC,UAAU,CAAC,0CAA+B,CAAC,cAAc,CAAC,CAAC;AACtE,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport os from \"node:os\";\nimport {\n SEMRESATTRS_DEVICE_ID,\n SEMRESATTRS_DEVICE_MODEL_NAME,\n SEMRESATTRS_SERVICE_VERSION,\n SEMRESATTRS_K8S_POD_NAME,\n SEMRESATTRS_SERVICE_INSTANCE_ID,\n DBSYSTEMVALUES_DB2,\n DBSYSTEMVALUES_DERBY,\n DBSYSTEMVALUES_MARIADB,\n DBSYSTEMVALUES_MSSQL,\n DBSYSTEMVALUES_ORACLE,\n DBSYSTEMVALUES_SQLITE,\n DBSYSTEMVALUES_OTHER_SQL,\n DBSYSTEMVALUES_HSQLDB,\n SEMATTRS_PEER_SERVICE,\n SEMRESATTRS_SERVICE_NAME,\n SEMRESATTRS_SERVICE_NAMESPACE,\n SEMRESATTRS_K8S_DEPLOYMENT_NAME,\n SEMRESATTRS_K8S_REPLICASET_NAME,\n SEMRESATTRS_K8S_STATEFULSET_NAME,\n SEMRESATTRS_K8S_JOB_NAME,\n SEMRESATTRS_K8S_CRONJOB_NAME,\n SEMRESATTRS_K8S_DAEMONSET_NAME,\n ATTR_TELEMETRY_SDK_VERSION,\n ATTR_TELEMETRY_SDK_LANGUAGE,\n ATTR_TELEMETRY_SDK_NAME,\n DBSYSTEMVALUES_H2,\n} from \"@opentelemetry/semantic-conventions\";\nimport { experimentalOpenTelemetryValues, type Tags } from \"../types.js\";\nimport { getInstance } from \"../platform/index.js\";\nimport type { TelemetryItem as Envelope, MetricsData } from \"../generated/index.js\";\nimport { KnownContextTagKeys } from \"../generated/index.js\";\nimport type { Resource } from \"@opentelemetry/resources\";\nimport type { Attributes, HrTime } from \"@opentelemetry/api\";\nimport { hrTimeToNanoseconds } from \"@opentelemetry/core\";\nimport type { AnyValue } from \"@opentelemetry/api-logs\";\nimport { ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED } from \"../Declarations/Constants.js\";\nimport {\n getHttpHost,\n getHttpMethod,\n getHttpScheme,\n getHttpTarget,\n getHttpUrl,\n getNetPeerName,\n getNetPeerPort,\n getPeerIp,\n} from \"./spanUtils.js\";\n\nexport function hrTimeToDate(hrTime: HrTime): Date {\n return new Date(hrTimeToNanoseconds(hrTime) / 1000000);\n}\n\nexport function createTagsFromResource(resource: Resource): Tags {\n const context = getInstance();\n const tags: Tags = { ...context.tags };\n if (resource && resource.attributes) {\n tags[KnownContextTagKeys.AiCloudRole] = getCloudRole(resource);\n tags[KnownContextTagKeys.AiCloudRoleInstance] = getCloudRoleInstance(resource);\n if (resource.attributes[SEMRESATTRS_DEVICE_ID]) {\n tags[KnownContextTagKeys.AiDeviceId] = String(resource.attributes[SEMRESATTRS_DEVICE_ID]);\n }\n if (resource.attributes[SEMRESATTRS_DEVICE_MODEL_NAME]) {\n tags[KnownContextTagKeys.AiDeviceModel] = String(\n resource.attributes[SEMRESATTRS_DEVICE_MODEL_NAME],\n );\n }\n if (resource.attributes[SEMRESATTRS_SERVICE_VERSION]) {\n tags[KnownContextTagKeys.AiApplicationVer] = String(\n resource.attributes[SEMRESATTRS_SERVICE_VERSION],\n );\n }\n }\n return tags;\n}\n\nfunction getCloudRole(resource: Resource): string {\n let cloudRole = \"\";\n // Service attributes\n const serviceName = resource.attributes[SEMRESATTRS_SERVICE_NAME];\n const serviceNamespace = resource.attributes[SEMRESATTRS_SERVICE_NAMESPACE];\n if (serviceName) {\n // Custom Service name provided by customer is highest precedence\n if (!String(serviceName).startsWith(\"unknown_service\")) {\n if (serviceNamespace) {\n return `${serviceNamespace}.${serviceName}`;\n } else {\n return String(serviceName);\n }\n } else {\n // Service attributes will be only used if K8S attributes are not present\n if (serviceNamespace) {\n cloudRole = `${serviceNamespace}.${serviceName}`;\n } else {\n cloudRole = String(serviceName);\n }\n }\n }\n // Kubernetes attributes should take precedence\n const kubernetesDeploymentName = resource.attributes[SEMRESATTRS_K8S_DEPLOYMENT_NAME];\n if (kubernetesDeploymentName) {\n return String(kubernetesDeploymentName);\n }\n const kuberneteReplicasetName = resource.attributes[SEMRESATTRS_K8S_REPLICASET_NAME];\n if (kuberneteReplicasetName) {\n return String(kuberneteReplicasetName);\n }\n const kubernetesStatefulSetName = resource.attributes[SEMRESATTRS_K8S_STATEFULSET_NAME];\n if (kubernetesStatefulSetName) {\n return String(kubernetesStatefulSetName);\n }\n const kubernetesJobName = resource.attributes[SEMRESATTRS_K8S_JOB_NAME];\n if (kubernetesJobName) {\n return String(kubernetesJobName);\n }\n const kubernetesCronjobName = resource.attributes[SEMRESATTRS_K8S_CRONJOB_NAME];\n if (kubernetesCronjobName) {\n return String(kubernetesCronjobName);\n }\n const kubernetesDaemonsetName = resource.attributes[SEMRESATTRS_K8S_DAEMONSET_NAME];\n if (kubernetesDaemonsetName) {\n return String(kubernetesDaemonsetName);\n }\n return cloudRole;\n}\n\nfunction getCloudRoleInstance(resource: Resource): string {\n // Kubernetes attributes should take precedence\n const kubernetesPodName = resource.attributes[SEMRESATTRS_K8S_POD_NAME];\n if (kubernetesPodName) {\n return String(kubernetesPodName);\n }\n // Service attributes\n const serviceInstanceId = resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID];\n if (serviceInstanceId) {\n return String(serviceInstanceId);\n }\n // Default\n return os && os.hostname();\n}\n\nexport function isSqlDB(dbSystem: string): boolean {\n return (\n dbSystem === DBSYSTEMVALUES_DB2 ||\n dbSystem === DBSYSTEMVALUES_DERBY ||\n dbSystem === DBSYSTEMVALUES_MARIADB ||\n dbSystem === DBSYSTEMVALUES_MSSQL ||\n dbSystem === DBSYSTEMVALUES_ORACLE ||\n dbSystem === DBSYSTEMVALUES_SQLITE ||\n dbSystem === DBSYSTEMVALUES_OTHER_SQL ||\n dbSystem === DBSYSTEMVALUES_HSQLDB ||\n dbSystem === DBSYSTEMVALUES_H2\n );\n}\n\nexport function getUrl(attributes: Attributes): string {\n if (!attributes) {\n return \"\";\n }\n const httpMethod = getHttpMethod(attributes);\n if (httpMethod) {\n const httpUrl = getHttpUrl(attributes);\n if (httpUrl) {\n return String(httpUrl);\n } else {\n const httpScheme = getHttpScheme(attributes);\n const httpTarget = getHttpTarget(attributes);\n if (httpScheme && httpTarget) {\n const httpHost = getHttpHost(attributes);\n if (httpHost) {\n return `${httpScheme}://${httpHost}${httpTarget}`;\n } else {\n const netPeerPort = getNetPeerPort(attributes);\n if (netPeerPort) {\n const netPeerName = getNetPeerName(attributes);\n if (netPeerName) {\n return `${httpScheme}://${netPeerName}:${netPeerPort}${httpTarget}`;\n } else {\n const netPeerIp = getPeerIp(attributes);\n if (netPeerIp) {\n return `${httpScheme}://${netPeerIp}:${netPeerPort}${httpTarget}`;\n }\n }\n }\n }\n }\n }\n }\n return \"\";\n}\n\nexport function getDependencyTarget(attributes: Attributes): string {\n if (!attributes) {\n return \"\";\n }\n const peerService = attributes[SEMATTRS_PEER_SERVICE];\n const httpHost = getHttpHost(attributes);\n const httpUrl = getHttpUrl(attributes);\n const netPeerName = getNetPeerName(attributes);\n const netPeerIp = getPeerIp(attributes);\n if (peerService) {\n return String(peerService);\n } else if (httpHost) {\n return String(httpHost);\n } else if (httpUrl) {\n return String(httpUrl);\n } else if (netPeerName) {\n return String(netPeerName);\n } else if (netPeerIp) {\n return String(netPeerIp);\n }\n return \"\";\n}\n\nexport function createResourceMetricEnvelope(\n resource: Resource,\n instrumentationKey: string,\n): Envelope | undefined {\n if (resource && resource.attributes) {\n const tags = createTagsFromResource(resource);\n const resourceAttributes: { [propertyName: string]: string } = {};\n for (const key of Object.keys(resource.attributes)) {\n // Avoid duplication ignoring fields already mapped.\n if (\n !(\n key.startsWith(\"_MS.\") ||\n key === ATTR_TELEMETRY_SDK_VERSION ||\n key === ATTR_TELEMETRY_SDK_LANGUAGE ||\n key === ATTR_TELEMETRY_SDK_NAME\n )\n ) {\n resourceAttributes[key] = resource.attributes[key] as string;\n }\n }\n // Only send event when resource attributes are available\n if (Object.keys(resourceAttributes).length > 0) {\n const baseData: MetricsData = {\n version: 2,\n metrics: [{ name: \"_OTELRESOURCE_\", value: 1 }],\n properties: resourceAttributes,\n };\n const envelope: Envelope = {\n name: \"Microsoft.ApplicationInsights.Metric\",\n time: new Date(),\n sampleRate: 100, // Metrics are never sampled\n instrumentationKey: instrumentationKey,\n version: 1,\n data: {\n baseType: \"MetricData\",\n baseData: baseData,\n },\n tags: tags,\n };\n return envelope;\n }\n }\n return;\n}\n\nexport function serializeAttribute(value: AnyValue): string {\n if (typeof value === \"object\") {\n if (value instanceof Error) {\n try {\n return JSON.stringify(value, Object.getOwnPropertyNames(value));\n } catch (err: unknown) {\n // Failed to serialize, return string cast\n return String(value);\n }\n } else if (value instanceof Uint8Array) {\n return String(value);\n } else {\n try {\n return JSON.stringify(value);\n } catch (err: unknown) {\n // Failed to serialize, return string cast\n return String(value);\n }\n }\n }\n // Return scalar and undefined values\n return String(value);\n}\n\nexport function shouldCreateResourceMetric(): boolean {\n return !(process.env[ENV_OPENTELEMETRY_RESOURCE_METRIC_DISABLED]?.toLowerCase() === \"true\");\n}\n\nexport function isSyntheticSource(attributes: Attributes): boolean {\n return !!attributes[experimentalOpenTelemetryValues.SYNTHETIC_TYPE];\n}\n"]}
|