@nsshunt/stsappframework 3.0.59 → 3.0.61

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.
@@ -1,6 +1,99 @@
1
1
  /* eslint @typescript-eslint/no-explicit-any: 0, @typescript-eslint/no-unused-vars: 0 */ // --> OFF
2
2
  // https://www.influxdata.com/blog/tldr-influxdb-tech-tips-multiple-aggregations-yield-flux/
3
3
  // https://www.influxdata.com/blog/top-5-hurdles-for-intermediate-flux-users-and-resources-for-optimizing-flux/
4
+
5
+ /*
6
+ option task = {name: "task-sts-agent-stats", every: 1s}
7
+
8
+ data =
9
+ from(bucket: "TestBucket01")
10
+ |> range(start: -5s)
11
+ |> last()
12
+ |> filter(fn: (r) => r["_measurement"] == "agent")
13
+
14
+ r1 =
15
+ data
16
+ |> filter(fn: (r) => r["_field"] == "requestCount"
17
+ or r["_field"] == "errorCount"
18
+ or r["_field"] == "retryCount"
19
+ or r["_field"] == "authenticationCount"
20
+ or r["_field"] == "activeRequestCount"
21
+ or r["_field"] == "coreCount"
22
+ or r["_field"] == "velocity"
23
+ or r["_field"] == "childCount"
24
+ or r["_field"] == "timer")
25
+
26
+ r2 =
27
+ data
28
+ |> filter(fn: (r) => float(v: r["_value"]) > 0.0 and (r["_field"] == "duration"
29
+ or r["_field"] == "latency"))
30
+
31
+ byagentthreadasyncunnersum =
32
+ r1
33
+ |> group(columns: ["agentName", "threadId", "asyncRunnerId", "_field"])
34
+ |> sum()
35
+ |> toFloat()
36
+
37
+ byagentthreadasyncunnermean =
38
+ r2
39
+ |> group(columns: ["agentName", "threadId", "asyncRunnerId", "_field"])
40
+ |> mean()
41
+ |> toFloat()
42
+
43
+ union(tables: [byagentthreadasyncunnersum, byagentthreadasyncunnermean])
44
+ |> map(fn: (r) => ({r with _time: now(), _measurement: "sts-stats-by-agentthreadasyncunner"}))
45
+ |> to(org: "my-org", bucket: "TestBucket01")
46
+
47
+ byagentthreadsum =
48
+ r1
49
+ |> group(columns: ["agentName", "threadId", "_field"])
50
+ |> sum()
51
+ |> toFloat()
52
+
53
+ byagentthreadmean =
54
+ r2
55
+ |> group(columns: ["agentName", "threadId", "_field"])
56
+ |> mean()
57
+ |> toFloat()
58
+
59
+ union(tables: [byagentthreadsum, byagentthreadmean])
60
+ |> map(fn: (r) => ({r with _time: now(), _measurement: "sts-stats-by-agentthread"}))
61
+ |> to(org: "my-org", bucket: "TestBucket01")
62
+
63
+ byagentsum =
64
+ r1
65
+ |> group(columns: ["agentName", "_field"])
66
+ |> sum()
67
+ |> toFloat()
68
+
69
+ byagentmean =
70
+ r2
71
+ |> group(columns: ["agentName", "_field"])
72
+ |> mean()
73
+ |> toFloat()
74
+
75
+ union(tables: [byagentsum, byagentmean])
76
+ |> map(fn: (r) => ({r with _time: now(), _measurement: "sts-stats-by-agent"}))
77
+ |> to(org: "my-org", bucket: "TestBucket01")
78
+
79
+ globalagentsum =
80
+ r1
81
+ |> group(columns: ["_field"])
82
+ |> sum()
83
+ |> toFloat()
84
+
85
+ globalagentmean =
86
+ r2
87
+ |> group(columns: ["_field"])
88
+ |> mean()
89
+ |> toFloat()
90
+
91
+ union(tables: [globalagentsum, globalagentmean])
92
+ |> map(fn: (r) => ({r with _time: now(), _measurement: "sts-stats-globalagent"}))
93
+ |> to(org: "my-org", bucket: "TestBucket01")
94
+
95
+ */
96
+
4
97
  import { Point, WriteApi, QueryApi } from '@influxdata/influxdb-client'
5
98
 
6
99
  import { Gauge, InstrumentVelocity } from '@nsshunt/stsinstrumentation'
@@ -10,6 +103,11 @@ import { InfluxDBManagerBase } from './influxDBManagerBase'
10
103
  import { IInfluxDBManagerOptions, InstrumentPayload } from './../commonTypes'
11
104
  import { ISubscriptionPayload, ISubscriptionKey } from '@nsshunt/stssocketio-client'
12
105
 
106
+ const AGENT_STATS_BY_AGENT_THREAD_ASYNCRUNNER = "sts-stats-by-agentthreadasyncunner";
107
+ const AGENT_STATS_BY_AGENT_THREAD = "sts-stats-by-agentthread";
108
+ const AGENT_STATS_BY_AGENT = "sts-stats-by-agent";
109
+ const AGENT_STATS_GLOBALAGENT = "sts-stats-globalagent";
110
+
13
111
  const _logPrefix = 'InfluxDBManagerService:'
14
112
 
15
113
  export class InfluxDBManagerAgent extends InfluxDBManagerBase
@@ -25,60 +123,12 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
25
123
  // Queries --------------------------------------------------------------------------------------------------------
26
124
 
27
125
  // Counter metrics
28
- #GetSTSCountGenericAgent = async (filterClause: string, groupClause: string, showOutput: boolean = false) => {
126
+ #GetSTSCountGenericAgent = async (measurement: string, filterClause: string, showOutput: boolean = false) => {
29
127
  try {
30
128
  const query = `from(bucket: "${this.options.bucket}")
31
129
  |> range(start: -5s)
32
130
  |> last()
33
- |> filter(fn: (r) => r["_measurement"] == "agent" and (r["_field"] == "errorCount"
34
- or r["_field"] == "retryCount"
35
- or r["_field"] == "authenticationCount"
36
- or r["_field"] == "velocity"
37
- or r["_field"] == "coreCount"
38
- or r["_field"] == "timer"
39
- or r["_field"] == "duration"
40
- or r["_field"] == "latency"
41
- or r["_field"] == "activeRequestCount"
42
- or r["_field"] == "childCount") ${filterClause})
43
- |> ${groupClause}
44
- |> sum()`;
45
-
46
- if (showOutput) {
47
- console.log(query);
48
- }
49
-
50
- return this.queryApi.collectRows(query)
51
- } catch (error) {
52
- console.error(`${_logPrefix}#GetSTSCountGeneric: Error: [${error}]`.red);
53
- }
54
- }
55
-
56
- #GetSTSCountGenericAgentOld = async (filterClause: string, groupClause: string, showOutput: boolean = false) => {
57
- try {
58
- const query = `dostscountex = (q, d) =>
59
- from(bucket: "${this.options.bucket}")
60
- |> range(start: d)
61
- |> filter(fn: (r) => r["_measurement"] == "agent" ${filterClause})
62
- |> filter(fn: (r) => r["_field"] == q)
63
- |> last()
64
- |> ${groupClause}
65
- |> sum()
66
-
67
- dostscount = (d) =>
68
- union(tables: [
69
- dostscountex(q: "requestCount", d: d),
70
- dostscountex(q: "errorCount", d: d),
71
- dostscountex(q: "retryCount", d: d),
72
- dostscountex(q: "authenticationCount", d: d),
73
- dostscountex(q: "velocity", d: d),
74
- dostscountex(q: "coreCount", d: d),
75
- dostscountex(q: "timer", d: d),
76
- dostscountex(q: "duration", d: d),
77
- dostscountex(q: "latency", d: d),
78
- dostscountex(q: "activeRequestCount", d: d),
79
- dostscountex(q: "childCount", d: d)
80
- ])
81
- dostscount(d: -5s)`;
131
+ |> filter(fn: (r) => r["_measurement"] == "${measurement}" ${filterClause})`;
82
132
 
83
133
  if (showOutput) {
84
134
  console.log(query);
@@ -91,14 +141,13 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
91
141
  }
92
142
 
93
143
  // Histo metrics
94
- #GetSTSHistoGenericService = async (filterClause: string, groupClause: string): Promise<any> => {
144
+ #GetSTSHistoGenericService = async (measurement: string, filterClause: string): Promise<any> => {
95
145
  try {
96
146
  const query = `import "math"
97
147
  from(bucket: "${this.options.bucket}")
98
148
  |> range(start: -10m)
99
- |> filter(fn: (r) => r["_measurement"] == "agent" and (r["_field"] =="latency"
149
+ |> filter(fn: (r) => r["_measurement"] == "${measurement}" and (r["_field"] =="latency"
100
150
  or r["_field"] =="duration") ${filterClause})
101
- |> ${groupClause}
102
151
  |> histogram(bins: [0.0, 10.0, 20.0, 50.0, 100.0, 1000.0, 50000.0, math.mInf(sign: 1) ])
103
152
  |> difference()`;
104
153
  return this.queryApi.collectRows(query)
@@ -107,40 +156,13 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
107
156
  }
108
157
  }
109
158
 
110
- #GetSTSHistoGenericServiceOld = async (filterClause: string, groupClause: string): Promise<any> => {
111
- try {
112
- const query = `import "math"
113
-
114
- dostshistoex = (q, d) =>
115
- from(bucket: "${this.options.bucket}")
116
- |> range(start: d)
117
- |> filter(fn: (r) => r["_measurement"] == "agent")
118
- |> filter(fn: (r) => r["_field"] == q ${filterClause})
119
- |> ${groupClause}
120
- |> histogram(bins: [0.0, 10.0, 20.0, 50.0, 100.0, 1000.0, 50000.0, math.mInf(sign: 1) ])
121
- |> difference()
122
-
123
- dostshisto = (d) =>
124
- union(tables: [
125
- dostshistoex(q: "latency", d: d),
126
- dostshistoex(q: "duration", d: d)
127
- ])
128
-
129
- dostshisto(d: -10m)`;
130
- return this.queryApi.collectRows(query)
131
- } catch (error) {
132
- console.error(`${_logPrefix}#GetSTSHistoGeneric: Error: [${error}]`.red);
133
- }
134
- }
135
-
136
159
  // Quantile metrics
137
- #GetSTSQuantileGenericAgent = async (filterClause: string, groupClause: string) => {
160
+ #GetSTSQuantileGenericAgent = async (measurement: string, filterClause: string) => {
138
161
  try {
139
162
  const query = `data = from(bucket: "${this.options.bucket}")
140
163
  |> range(start: -10m)
141
- |> filter(fn: (r) => r["_measurement"] == "agent" and (r["_field"] == "latency"
164
+ |> filter(fn: (r) => r["_measurement"] == "${measurement}" and (r["_field"] == "latency"
142
165
  or r["_field"] == "duration") ${filterClause})
143
- |> ${groupClause}
144
166
  |> aggregateWindow(every: 5s, fn: max, createEmpty: false)
145
167
 
146
168
  dostsquantileex = (q) =>
@@ -163,48 +185,15 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
163
185
  }
164
186
  }
165
187
 
166
- #GetSTSQuantileGenericAgentOld = async (filterClause: string, groupClause: string) => {
167
- try {
168
- const query = `dostsquantileex = (q, d, i, f) =>
169
- from(bucket: "${this.options.bucket}")
170
- |> range(start: d)
171
- |> filter(fn: (r) => r["_measurement"] == "agent")
172
- |> filter(fn: (r) => r["_field"] == f ${filterClause})
173
- |> ${groupClause}
174
- |> aggregateWindow(every: i, fn: max, createEmpty: false)
175
- |> quantile(q: q, method: "estimate_tdigest", compression: 1000.0)
176
- |> set(key: "quantile", value: string(v:q))
177
- |> group(columns: ["LatencyType","quantile"])
178
-
179
- dostsquantile = (d, i, f) =>
180
- union(tables: [
181
- dostsquantileex(q: 0.5, d: d, i: i, f: f),
182
- dostsquantileex(q: 0.8, d: d, i: i, f: f),
183
- dostsquantileex(q: 0.9, d: d, i: i, f: f),
184
- dostsquantileex(q: 0.95, d: d, i: i, f: f),
185
- dostsquantileex(q: 0.99, d: d, i: i, f: f)
186
- ])
187
-
188
- union(tables: [
189
- dostsquantile(d: -10m, i: 5s, f: "latency"),
190
- dostsquantile(d: -10m, i: 5s, f: "duration")
191
- ])`;
192
-
193
- return this.queryApi.collectRows(query)
194
- } catch (error) {
195
- console.error(`${_logPrefix}#GetSTSQuantileGeneric: Error: [${error}]`.red);
196
- }
197
- }
198
-
199
188
  // Metric queries -------------------------------------------------------------------------------------------------
200
189
  // Root level metrics
201
190
  async GetInfluxDBResultsRootAgent(subscriptionKey: ISubscriptionKey): Promise<ISubscriptionPayload> { // ISubscriptionPayload
202
191
  let retVal = null;
203
192
  try {
204
193
  retVal = await this.ProcessInfluxDBResults([
205
- this.#GetSTSCountGenericAgent('', 'group(columns: ["_field"])'),
206
- this.#GetSTSQuantileGenericAgent('', 'group(columns: ["_field"])'),
207
- this.#GetSTSHistoGenericService('', 'group(columns: ["_field"])')],
194
+ this.#GetSTSCountGenericAgent(AGENT_STATS_GLOBALAGENT, ''),
195
+ this.#GetSTSQuantileGenericAgent(AGENT_STATS_GLOBALAGENT, ''),
196
+ this.#GetSTSHistoGenericService(AGENT_STATS_GLOBALAGENT, '')],
208
197
  [ ])
209
198
  } catch (error) {
210
199
  console.error(`${_logPrefix}GetInfluxDBResultsRootAgent: Error: [${error}]`.red);
@@ -220,9 +209,9 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
220
209
  let retVal = null;
221
210
  try {
222
211
  retVal = await this.ProcessInfluxDBResults([
223
- this.#GetSTSCountGenericAgent('', 'group(columns: ["agentName", "_field"])'),
224
- this.#GetSTSQuantileGenericAgent('', 'group(columns: ["agentName", "_field"])'),
225
- this.#GetSTSHistoGenericService('', 'group(columns: ["agentName", "_field"])')],
212
+ this.#GetSTSCountGenericAgent(AGENT_STATS_BY_AGENT, ''),
213
+ this.#GetSTSQuantileGenericAgent(AGENT_STATS_BY_AGENT, ''),
214
+ this.#GetSTSHistoGenericService(AGENT_STATS_BY_AGENT, '')],
226
215
  ['agentName'])
227
216
  } catch (error) {
228
217
  console.error(`${_logPrefix}GetInfluxDBResultsAgent: Error: [${error}]`.red);
@@ -239,9 +228,9 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
239
228
  try {
240
229
  const agentName = subscriptionKey.key as string;
241
230
  retVal = await this.ProcessInfluxDBResults([
242
- this.#GetSTSCountGenericAgent(`and r["agentName"] == "${agentName}"`, `group(columns: ["agentName", "threadId", "_field"])`),
243
- this.#GetSTSQuantileGenericAgent(`and r["agentName"] == "${agentName}"`, 'group(columns: ["agentName", "threadId", "_field"])'),
244
- this.#GetSTSHistoGenericService(`and r["agentName"] == "${agentName}"`, 'group(columns: ["agentName", "threadId", "_field"])')],
231
+ this.#GetSTSCountGenericAgent(AGENT_STATS_BY_AGENT_THREAD, `and r["agentName"] == "${agentName}"`),
232
+ this.#GetSTSQuantileGenericAgent(AGENT_STATS_BY_AGENT_THREAD, `and r["agentName"] == "${agentName}"`),
233
+ this.#GetSTSHistoGenericService(AGENT_STATS_BY_AGENT_THREAD, `and r["agentName"] == "${agentName}"`)],
245
234
  ['agentName', 'threadId'])
246
235
  } catch (error) {
247
236
  console.error(`${_logPrefix}GetInfluxDBResultsAgentThreads: Error: [${error}]`.red);
@@ -259,9 +248,9 @@ export class InfluxDBManagerAgent extends InfluxDBManagerBase
259
248
  const agentName = subscriptionKey.key as string;
260
249
  const threadId = subscriptionKey.subkey as string;
261
250
  retVal = await this.ProcessInfluxDBResults([
262
- this.#GetSTSCountGenericAgent(`and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`, `group(columns: ["agentName", "threadId", "asyncRunnerId", "_field"])`),
263
- this.#GetSTSQuantileGenericAgent(`and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`, 'group(columns: ["agentName", "threadId", "asyncRunnerId", "_field"])'),
264
- this.#GetSTSHistoGenericService(`and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`, 'group(columns: ["agentName", "threadId", "asyncRunnerId", "_field"])')],
251
+ this.#GetSTSCountGenericAgent(AGENT_STATS_BY_AGENT_THREAD_ASYNCRUNNER, `and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`),
252
+ this.#GetSTSQuantileGenericAgent(AGENT_STATS_BY_AGENT_THREAD_ASYNCRUNNER, `and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`),
253
+ this.#GetSTSHistoGenericService(AGENT_STATS_BY_AGENT_THREAD_ASYNCRUNNER, `and r["agentName"] == "${agentName}" and r["threadId"] == "${threadId}"`)],
265
254
  ['agentName', 'threadId', 'asyncRunnerId'])
266
255
  } catch (error) {
267
256
  console.error(`${_logPrefix}GetInfluxDBResultsAgentThread: Error: [${error}]`.red);