|
1 | | -import { EventEmitter } from 'stream'; |
2 | | -import { StaticContext, Unleash, UnleashEvents } from '../unleash'; |
3 | | -import { ImpactMetricRegistry, MetricFlagContext, MetricLabels } from './metric-types'; |
4 | | -import { extractEnvironmentFromCustomHeaders } from './environment-resolver'; |
5 | | -import Client from '../client'; |
6 | | - |
7 | | -export class MetricsAPI extends EventEmitter { |
8 | | - constructor( |
9 | | - private metricRegistry: ImpactMetricRegistry, |
10 | | - private variantResolver: Pick<Client, 'forceGetVariant'>, |
11 | | - private staticContext: StaticContext, |
12 | | - ) { |
13 | | - super(); |
14 | | - } |
15 | | - |
16 | | - defineCounter(name: string, help: string) { |
17 | | - if (!name || !help) { |
18 | | - this.emit(UnleashEvents.Warn, `Counter name or help cannot be empty: ${name}, ${help}.`); |
19 | | - return; |
20 | | - } |
21 | | - const labelNames = ['featureName', 'appName', 'environment']; |
22 | | - this.metricRegistry.counter({ name, help, labelNames }); |
23 | | - } |
24 | | - |
25 | | - defineGauge(name: string, help: string) { |
26 | | - if (!name || !help) { |
27 | | - this.emit(UnleashEvents.Warn, `Gauge name or help cannot be empty: ${name}, ${help}.`); |
28 | | - return; |
29 | | - } |
30 | | - const labelNames = ['featureName', 'appName', 'environment']; |
31 | | - this.metricRegistry.gauge({ name, help, labelNames }); |
32 | | - } |
33 | | - |
34 | | - defineHistogram(name: string, help: string, buckets?: number[]) { |
35 | | - if (!name || !help) { |
36 | | - this.emit(UnleashEvents.Warn, `Histogram name or help cannot be empty: ${name}, ${help}.`); |
37 | | - return; |
38 | | - } |
39 | | - const labelNames = ['featureName', 'appName', 'environment']; |
40 | | - this.metricRegistry.histogram({ name, help, labelNames, buckets: buckets || [] }); |
41 | | - } |
42 | | - |
43 | | - private getFlagLabels(flagContext?: MetricFlagContext): MetricLabels { |
44 | | - let flagLabels: MetricLabels = {}; |
45 | | - if (flagContext) { |
46 | | - for (const flag of flagContext.flagNames) { |
47 | | - const variant = this.variantResolver.forceGetVariant(flag, flagContext.context); |
48 | | - |
49 | | - if (variant.enabled) { |
50 | | - flagLabels[flag] = variant.name; |
51 | | - } else if (variant.feature_enabled) { |
52 | | - flagLabels[flag] = 'enabled'; |
53 | | - } else { |
54 | | - flagLabels[flag] = 'disabled'; |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - return flagLabels; |
59 | | - } |
60 | | - |
61 | | - incrementCounter(name: string, value?: number, flagContext?: MetricFlagContext): void { |
62 | | - const counter = this.metricRegistry.getCounter(name); |
63 | | - if (!counter) { |
64 | | - this.emit( |
65 | | - UnleashEvents.Warn, |
66 | | - `Counter ${name} not defined, this counter will not be incremented.`, |
67 | | - ); |
68 | | - return; |
69 | | - } |
70 | | - |
71 | | - const flagLabels = this.getFlagLabels(flagContext); |
72 | | - |
73 | | - const labels = { |
74 | | - ...flagLabels, |
75 | | - ...this.staticContext, |
76 | | - }; |
77 | | - |
78 | | - counter.inc(value, labels); |
79 | | - } |
80 | | - |
81 | | - updateGauge(name: string, value: number, flagContext?: MetricFlagContext): void { |
82 | | - const gauge = this.metricRegistry.getGauge(name); |
83 | | - if (!gauge) { |
84 | | - this.emit(UnleashEvents.Warn, `Gauge ${name} not defined, this gauge will not be updated.`); |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - const flagLabels = this.getFlagLabels(flagContext); |
89 | | - |
90 | | - const labels = { |
91 | | - ...flagLabels, |
92 | | - ...this.staticContext, |
93 | | - }; |
94 | | - |
95 | | - gauge.set(value, labels); |
96 | | - } |
97 | | - |
98 | | - observeHistogram(name: string, value: number, flagContext?: MetricFlagContext): void { |
99 | | - const histogram = this.metricRegistry.getHistogram(name); |
100 | | - if (!histogram) { |
101 | | - this.emit( |
102 | | - UnleashEvents.Warn, |
103 | | - `Histogram ${name} not defined, this histogram will not be updated.`, |
104 | | - ); |
105 | | - return; |
106 | | - } |
107 | | - |
108 | | - const flagLabels = this.getFlagLabels(flagContext); |
109 | | - |
110 | | - const labels = { |
111 | | - ...flagLabels, |
112 | | - ...this.staticContext, |
113 | | - }; |
114 | | - |
115 | | - histogram.observe(value, labels); |
116 | | - } |
117 | | -} |
| 1 | +import { Unleash } from '../unleash'; |
118 | 2 |
|
119 | 3 | export class UnleashMetricClient extends Unleash { |
120 | | - public impactMetrics: MetricsAPI; |
121 | | - |
122 | 4 | constructor(...args: ConstructorParameters<typeof Unleash>) { |
123 | 5 | super(...args); |
124 | | - |
125 | | - const config = args[0]; |
126 | | - const metricsContext: StaticContext = { ...this.staticContext }; |
127 | | - |
128 | | - if (config && config.customHeaders) { |
129 | | - const environment = extractEnvironmentFromCustomHeaders(config.customHeaders); |
130 | | - if (environment) { |
131 | | - metricsContext.environment = environment; |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - this.impactMetrics = new MetricsAPI(this.metricRegistry, this.client, metricsContext); |
| 6 | + console.warn( |
| 7 | + 'UnleashMetricClient is deprecated. ' + |
| 8 | + 'This functionality now lives in UnleashClient. ' + |
| 9 | + 'This class will be removed in the next major release.', |
| 10 | + ); |
136 | 11 | } |
137 | 12 | } |
0 commit comments