Skip to content

Commit

Permalink
DEV: add current statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Nov 4, 2023
1 parent 118ad88 commit eca7c69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,13 @@ This will retrieve a metric value for a given namespace, metric name and set of
The `period` argument selects the best metric span to query. For example: 3600 for one hour. The period will be used by query to find the span that has the same or closest (and greater) period.
The `statistic` can be `avg`, `max`, `min`, `sum`, `count` or a P-value of the form `pNN` where NN is the P-value. For example: p95 would return the P-95 value. To get meaningful P-value statistics you must set the CustomMetrics pResolution parameter to the number of data points to keep for computing P-values. By default this resolution is zero, which means P-values are not computed. To enable, you should set this to at least 100.
The `statistic` can be `avg`, `current`, `max`, `min`, `sum`, `count` or a P-value of the form `pNN` where NN is the P-value.
The `avg`, `max` and `min` statistics compute the average, maximum and minimum value for each span data point. The `current` statistic uses the most recent value for each span data point and is useful when used with the `accumulate` option to return the most recent value of a metric.
The `sum` statistic returns the summation of the values for each span data point and the `count` statistic returns the number of values for a span data point.
For a P-value metric example: p95 would return the P-95 value. To get meaningful P-value statistics you must set the CustomMetrics pResolution parameter to the number of data points to keep for computing P-values. By default this resolution is zero, which means P-values are not computed. To enable, you should set this to at least 100.
The `options` map can modify the query. If `options.accumulate` is true, all points will be aggregated and a single data point will be returned that will represent the desired statistic for the requested period.
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ export class CustomMetrics {
} else if (statistic == 'count') {
value = 0
count = 0
} else if (statistic == 'current') {
value = 0
count = 0
} else if (statistic.match(/^p[0-9]+/)) {
pvalues = []
} /* avg */ else {
Expand All @@ -591,6 +594,8 @@ export class CustomMetrics {
}
} else if (statistic == 'sum') {
value += point.sum
} else if (statistic == 'current') {
value = point.sum / (point.count || 1)
} else if (statistic == 'count') {
value += point.count
} else if (statistic.match(/^p[0-9]+/)) {
Expand Down Expand Up @@ -659,7 +664,7 @@ export class CustomMetrics {
pvalues.sort((a, b) => a - b)
let nth = Math.min(Math.round((pvalues.length * p) / 100 + 1), pvalues.length - 1)
value = pvalues[nth]
} /* avg */ else {
} /* avg | current */ else {
value = point.sum / point.count
}
} else {
Expand Down

0 comments on commit eca7c69

Please sign in to comment.