123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const utils = require('@sentry/utils');
- const constants = require('./constants.js');
- /**
- * Generate bucket key from metric properties.
- */
- function getBucketKey(
- metricType,
- name,
- unit,
- tags,
- ) {
- const stringifiedTags = Object.entries(utils.dropUndefinedKeys(tags)).sort((a, b) => a[0].localeCompare(b[0]));
- return `${metricType}${name}${unit}${stringifiedTags}`;
- }
- /* eslint-disable no-bitwise */
- /**
- * Simple hash function for strings.
- */
- function simpleHash(s) {
- let rv = 0;
- for (let i = 0; i < s.length; i++) {
- const c = s.charCodeAt(i);
- rv = (rv << 5) - rv + c;
- rv &= rv;
- }
- return rv >>> 0;
- }
- /* eslint-enable no-bitwise */
- /**
- * Serialize metrics buckets into a string based on statsd format.
- *
- * Example of format:
- * metric.name@second:1:1.2|d|#a:value,b:anothervalue|T12345677
- * Segments:
- * name: metric.name
- * unit: second
- * value: [1, 1.2]
- * type of metric: d (distribution)
- * tags: { a: value, b: anothervalue }
- * timestamp: 12345677
- */
- function serializeMetricBuckets(metricBucketItems) {
- let out = '';
- for (const item of metricBucketItems) {
- const tagEntries = Object.entries(item.tags);
- const maybeTags = tagEntries.length > 0 ? `|#${tagEntries.map(([key, value]) => `${key}:${value}`).join(',')}` : '';
- out += `${item.name}@${item.unit}:${item.metric}|${item.metricType}${maybeTags}|T${item.timestamp}\n`;
- }
- return out;
- }
- /**
- * Sanitizes tags.
- */
- function sanitizeTags(unsanitizedTags) {
- const tags = {};
- for (const key in unsanitizedTags) {
- if (Object.prototype.hasOwnProperty.call(unsanitizedTags, key)) {
- const sanitizedKey = key.replace(constants.NAME_AND_TAG_KEY_NORMALIZATION_REGEX, '_');
- tags[sanitizedKey] = String(unsanitizedTags[key]).replace(constants.TAG_VALUE_NORMALIZATION_REGEX, '');
- }
- }
- return tags;
- }
- exports.getBucketKey = getBucketKey;
- exports.sanitizeTags = sanitizeTags;
- exports.serializeMetricBuckets = serializeMetricBuckets;
- exports.simpleHash = simpleHash;
- //# sourceMappingURL=utils.js.map
|