@codefresh-io/cf-telemetry 2.3.0-alpha.3 → 2.3.0-alpha.4

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,8 +1,74 @@
1
- import { Attributes, Meter } from '@opentelemetry/api';
1
+ import { Attributes as A, Meter } from '@opentelemetry/api';
2
2
  type Name<Prefix extends string = 'codefresh'> = Prefix extends '' ? `${string}_info` : `${Prefix}_${string}_info`;
3
- export declare class InfoGauge<A extends Attributes, Prefix extends string = 'codefresh'> {
3
+ /**
4
+ * InfoGauge is a specialized gauge for recording information metrics.
5
+ * It records a value of 1 for the current state and 0 for the previous state,
6
+ * allowing for tracking changes in state over time.
7
+ *
8
+ * First type argument `Attributes` defines the attributes that will be recorded with the gauge.
9
+ * The second type argument `Prefix` allows customization of the gauge name prefix.
10
+ *
11
+ * ⚠️ This class stores the last recorded attributes for each unique combination of immutable attributes.
12
+ * Do not use it in a long-running process without proper cleanup, as it may lead to memory leaks.
13
+ *
14
+ * @example
15
+ * import * as otel from '@codefresh-io/cf-telemetry/otel';
16
+ *
17
+ * type ClassicBuildInfo = {
18
+ * build_id: string;
19
+ * build_status?: string;
20
+ * };
21
+ * const meter = otel.cf.getMeter();
22
+ * const buildInfoGauge = new otel.cf.InfoGauge<ClassicBuildInfo>(
23
+ * meter,
24
+ * 'codefresh_classic_build_info',
25
+ * 'Codefresh classic build info',
26
+ * ['build_id'],
27
+ * );
28
+ *
29
+ * buildInfoGauge.set({
30
+ * build_id: '111',
31
+ * build_status: 'running',
32
+ * });
33
+ * // Results in the following state:
34
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 1
35
+ *
36
+ * buildInfoGauge.set({
37
+ * build_id: '111',
38
+ * build_status: 'success',
39
+ * });
40
+ * // Results in the following state:
41
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 0
42
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'success' } = 1
43
+ *
44
+ * buildInfoGauge.set({
45
+ * build_id: '222',
46
+ * build_status: 'running',
47
+ * });
48
+ * // Results in the following state:
49
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 0
50
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'success' } = 1
51
+ * // codefresh_classic_build_info{ build_id: '222', build_status: 'running' } = 1
52
+ */
53
+ export declare class InfoGauge<Attributes extends A, Prefix extends string = 'codefresh'> {
4
54
  #private;
5
- constructor(meter: Meter, name: Name<Prefix>, description: string);
6
- set(attributes: Partial<A>): void;
55
+ /**
56
+ * @param meter {@link Meter|OpenTelemetry Meter} instance
57
+ * @param name the name of the gauge, default format is `codefresh_<name>_info`.
58
+ * Prefix can be customized via the `Prefix` type argument.
59
+ * @param description the description of the gauge, used for documentation purposes.
60
+ * @param immutableAttributes an array of attribute keys that should be treated as immutable
61
+ * and will be used to identify unique series.
62
+ * For example, `codefresh_classic_build_info{ build_id, build_status }` will have
63
+ * immutable attribute `build_id`: this attribute is immutable and accurately characterizes
64
+ * a unique series for which the value of `build_status` can change over time.
65
+ */
66
+ constructor(meter: Meter, name: Name<Prefix>, description: string, immutableAttributes: (keyof Attributes)[]);
67
+ /**
68
+ * @param attributes Attributes to set for the gauge.
69
+ * This method records the current state of the gauge with a value of 1,
70
+ * and resets the previous state (if any) to 0.
71
+ */
72
+ set(attributes: Attributes): void;
7
73
  }
8
74
  export {};
@@ -2,23 +2,101 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfoGauge = void 0;
4
4
  const api_1 = require("@opentelemetry/api");
5
+ /**
6
+ * InfoGauge is a specialized gauge for recording information metrics.
7
+ * It records a value of 1 for the current state and 0 for the previous state,
8
+ * allowing for tracking changes in state over time.
9
+ *
10
+ * First type argument `Attributes` defines the attributes that will be recorded with the gauge.
11
+ * The second type argument `Prefix` allows customization of the gauge name prefix.
12
+ *
13
+ * ⚠️ This class stores the last recorded attributes for each unique combination of immutable attributes.
14
+ * Do not use it in a long-running process without proper cleanup, as it may lead to memory leaks.
15
+ *
16
+ * @example
17
+ * import * as otel from '@codefresh-io/cf-telemetry/otel';
18
+ *
19
+ * type ClassicBuildInfo = {
20
+ * build_id: string;
21
+ * build_status?: string;
22
+ * };
23
+ * const meter = otel.cf.getMeter();
24
+ * const buildInfoGauge = new otel.cf.InfoGauge<ClassicBuildInfo>(
25
+ * meter,
26
+ * 'codefresh_classic_build_info',
27
+ * 'Codefresh classic build info',
28
+ * ['build_id'],
29
+ * );
30
+ *
31
+ * buildInfoGauge.set({
32
+ * build_id: '111',
33
+ * build_status: 'running',
34
+ * });
35
+ * // Results in the following state:
36
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 1
37
+ *
38
+ * buildInfoGauge.set({
39
+ * build_id: '111',
40
+ * build_status: 'success',
41
+ * });
42
+ * // Results in the following state:
43
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 0
44
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'success' } = 1
45
+ *
46
+ * buildInfoGauge.set({
47
+ * build_id: '222',
48
+ * build_status: 'running',
49
+ * });
50
+ * // Results in the following state:
51
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'running' } = 0
52
+ * // codefresh_classic_build_info{ build_id: '111', build_status: 'success' } = 1
53
+ * // codefresh_classic_build_info{ build_id: '222', build_status: 'running' } = 1
54
+ */
5
55
  class InfoGauge {
6
56
  #gauge;
7
- #lastAttributes = null;
8
- constructor(meter, name, description) {
57
+ #immutableAttributes;
58
+ #lastValuesHashMap;
59
+ /**
60
+ * @param meter {@link Meter|OpenTelemetry Meter} instance
61
+ * @param name the name of the gauge, default format is `codefresh_<name>_info`.
62
+ * Prefix can be customized via the `Prefix` type argument.
63
+ * @param description the description of the gauge, used for documentation purposes.
64
+ * @param immutableAttributes an array of attribute keys that should be treated as immutable
65
+ * and will be used to identify unique series.
66
+ * For example, `codefresh_classic_build_info{ build_id, build_status }` will have
67
+ * immutable attribute `build_id`: this attribute is immutable and accurately characterizes
68
+ * a unique series for which the value of `build_status` can change over time.
69
+ */
70
+ constructor(meter, name, description, immutableAttributes) {
71
+ this.#immutableAttributes = immutableAttributes;
72
+ this.#lastValuesHashMap = new Map();
9
73
  this.#gauge = meter.createGauge(name, {
10
74
  description,
11
75
  valueType: api_1.ValueType.INT,
12
76
  unit: '', // No unit for info gauges
13
77
  });
14
78
  }
79
+ #getImmutableAttributes(attributes) {
80
+ return Object.fromEntries(Object.entries(attributes).filter(([key]) => this.#immutableAttributes.includes(key)));
81
+ }
82
+ #getImmutableHash(attributes) {
83
+ return JSON.stringify(attributes);
84
+ }
85
+ /**
86
+ * @param attributes Attributes to set for the gauge.
87
+ * This method records the current state of the gauge with a value of 1,
88
+ * and resets the previous state (if any) to 0.
89
+ */
15
90
  set(attributes) {
16
- if (this.#lastAttributes) {
17
- this.#gauge.record(0, this.#lastAttributes);
91
+ const immutable = this.#getImmutableAttributes(attributes);
92
+ const seriesHash = this.#getImmutableHash(immutable);
93
+ const lastAttributes = this.#lastValuesHashMap.get(seriesHash);
94
+ if (lastAttributes) {
95
+ this.#gauge.record(0, lastAttributes);
18
96
  }
19
- const mergedAttributes = { ...this.#lastAttributes, ...attributes };
97
+ const mergedAttributes = { ...lastAttributes, ...attributes };
20
98
  this.#gauge.record(1, mergedAttributes);
21
- this.#lastAttributes = mergedAttributes;
99
+ this.#lastValuesHashMap.set(seriesHash, mergedAttributes);
22
100
  }
23
101
  }
24
102
  exports.InfoGauge = InfoGauge;
@@ -1 +1 @@
1
- {"version":3,"file":"info-gauge.js","sourceRoot":"","sources":["../../../src/otel/cf/info-gauge.ts"],"names":[],"mappings":";;;AAAA,4CAAyE;AAMzE,MAAa,SAAS;IACpB,MAAM,CAAW;IACjB,eAAe,GAAa,IAAI,CAAC;IAEjC,YAAY,KAAY,EAAE,IAAkB,EAAE,WAAmB;QAC/D,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE;YACpC,WAAW;YACX,SAAS,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CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,oBAAoB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CACxE,CAAC;IAClB,CAAC;IAED,iBAAiB,CAAC,UAAsB;QACtC,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,UAAsB;QACxB,MAAM,SAAS,GAAG,IAAI,CAAC,uBAAuB,CAAC,UAAU,CAAC,CAAC;QAC3D,MAAM,UAAU,GAAG,IAAI,CAAC,iBAAiB,CAAC,SAAuB,CAAC,CAAC;QACnE,MAAM,cAAc,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC/D,IAAI,cAAc,EAAE,CAAC;YACnB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC;QACxC,CAAC;QACD,MAAM,gBAAgB,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,UAAU,EAAE,CAAC;QAC9D,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC;QACxC,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAC5D,CAAC;CACF;AApDD,8BAoDC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codefresh-io/cf-telemetry",
3
- "version": "2.3.0-alpha.3",
3
+ "version": "2.3.0-alpha.4",
4
4
  "exports": {
5
5
  "./init": {
6
6
  "import": "./dist/init.mjs",