-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathdropped-spans-stats.js
More file actions
98 lines (85 loc) · 2.49 KB
/
dropped-spans-stats.js
File metadata and controls
98 lines (85 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict'
const MAX_DROPPED_SPANS_STATS = 128
class DroppedSpansStats {
constructor () {
this.statsMap = new Map()
}
/**
* Record this span in dropped spans stats.
*
* @param {Span} span
* @returns {boolean} True iff this span was added to stats. This return value
* is only used for testing.
*/
captureDroppedSpan (span) {
if (!span) {
return false
}
const serviceTargetType = span._serviceTarget && span._serviceTarget.type
const serviceTargetName = span._serviceTarget && span._serviceTarget.name
const resource = span._destination && span._destination.service && span._destination.service.resource
if (!span._exitSpan || !(serviceTargetType || serviceTargetName) || !resource) {
return false
}
const stats = this._getOrCreateStats(serviceTargetType, serviceTargetName, resource, span.outcome)
if (!stats) {
return false
}
stats.duration.count++
stats.duration.sum.us += (span._duration * 1000)
return true
}
_getOrCreateStats (serviceTargetType, serviceTargetName, resource, outcome) {
const key = [serviceTargetType, serviceTargetName, resource, outcome].join('')
let stats = this.statsMap.get(key)
if (stats) {
return stats
}
if (this.statsMap.size >= MAX_DROPPED_SPANS_STATS) {
return
}
stats = {
duration: {
count: 0,
sum: {
us: 0
}
},
destination_service_resource: resource,
outcome
}
if (serviceTargetType) {
stats.service_target_type = serviceTargetType
}
if (serviceTargetName) {
stats.service_target_name = serviceTargetName
}
this.statsMap.set(key, stats)
return stats
}
encode () {
// `duration.sum.us` is an integer in the intake API, but is stored as
// a float. We assume this `.encode()` is typically only called when the
// transaction is ended, so the in-place loss of the fractional value is
// acceptable.
const result = []
for (const stats of this.statsMap.values()) {
stats.duration.sum.us = Math.round(stats.duration.sum.us)
result.push(stats)
}
return result
}
size () {
return this.statsMap.size
}
}
module.exports = {
DroppedSpansStats,
// Exported for testing-only.
MAX_DROPPED_SPANS_STATS
}