metric-summary.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const utils = require('@sentry/utils');
  3. require('../debug-build.js');
  4. require('../tracing/errors.js');
  5. require('../tracing/spanstatus.js');
  6. const trace = require('../tracing/trace.js');
  7. /**
  8. * key: bucketKey
  9. * value: [exportKey, MetricSummary]
  10. */
  11. let SPAN_METRIC_SUMMARY;
  12. function getMetricStorageForSpan(span) {
  13. return SPAN_METRIC_SUMMARY ? SPAN_METRIC_SUMMARY.get(span) : undefined;
  14. }
  15. /**
  16. * Fetches the metric summary if it exists for the passed span
  17. */
  18. function getMetricSummaryJsonForSpan(span) {
  19. const storage = getMetricStorageForSpan(span);
  20. if (!storage) {
  21. return undefined;
  22. }
  23. const output = {};
  24. for (const [, [exportKey, summary]] of storage) {
  25. if (!output[exportKey]) {
  26. output[exportKey] = [];
  27. }
  28. output[exportKey].push(utils.dropUndefinedKeys(summary));
  29. }
  30. return output;
  31. }
  32. /**
  33. * Updates the metric summary on the currently active span
  34. */
  35. function updateMetricSummaryOnActiveSpan(
  36. metricType,
  37. sanitizedName,
  38. value,
  39. unit,
  40. tags,
  41. bucketKey,
  42. ) {
  43. const span = trace.getActiveSpan();
  44. if (span) {
  45. const storage = getMetricStorageForSpan(span) || new Map();
  46. const exportKey = `${metricType}:${sanitizedName}@${unit}`;
  47. const bucketItem = storage.get(bucketKey);
  48. if (bucketItem) {
  49. const [, summary] = bucketItem;
  50. storage.set(bucketKey, [
  51. exportKey,
  52. {
  53. min: Math.min(summary.min, value),
  54. max: Math.max(summary.max, value),
  55. count: (summary.count += 1),
  56. sum: (summary.sum += value),
  57. tags: summary.tags,
  58. },
  59. ]);
  60. } else {
  61. storage.set(bucketKey, [
  62. exportKey,
  63. {
  64. min: value,
  65. max: value,
  66. count: 1,
  67. sum: value,
  68. tags,
  69. },
  70. ]);
  71. }
  72. if (!SPAN_METRIC_SUMMARY) {
  73. SPAN_METRIC_SUMMARY = new WeakMap();
  74. }
  75. SPAN_METRIC_SUMMARY.set(span, storage);
  76. }
  77. }
  78. exports.getMetricSummaryJsonForSpan = getMetricSummaryJsonForSpan;
  79. exports.updateMetricSummaryOnActiveSpan = updateMetricSummaryOnActiveSpan;
  80. //# sourceMappingURL=metric-summary.js.map