@openplait/adapter-prometheus 0.1.0-alpha.0
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.
- package/LICENSE +201 -0
- package/README.md +8 -0
- package/dist/src/adapter.d.ts +27 -0
- package/dist/src/adapter.d.ts.map +1 -0
- package/dist/src/adapter.js +90 -0
- package/dist/src/adapter.js.map +1 -0
- package/dist/src/compiler.d.ts +7 -0
- package/dist/src/compiler.d.ts.map +1 -0
- package/dist/src/compiler.js +82 -0
- package/dist/src/compiler.js.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +4 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/normalization.d.ts +4 -0
- package/dist/src/normalization.d.ts.map +1 -0
- package/dist/src/normalization.js +29 -0
- package/dist/src/normalization.js.map +1 -0
- package/dist/src/types.d.ts +41 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# `@openplait/adapter-prometheus`
|
|
2
|
+
|
|
3
|
+
Prometheus-compatible metrics adapter with bounded PromQL range execution, semantic label filters, normalized frames, label discovery, and support for Prometheus-compatible services such as Mimir.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
const adapter = new PrometheusAdapter({ url: "http://localhost:9090", allowNativeQueries: true });
|
|
7
|
+
const result = await adapter.execute(nativePromQlQuery, executionContext);
|
|
8
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { NativeQuery, QueryResult, SemanticQuery, SemanticSchema } from "@openplait/core";
|
|
2
|
+
import { type AdapterManifest, type AdapterValidationResult, type CompileContext, type CompiledQuery, type DatasourceAdapter, type DatasourceCapabilities, type DiscoveryContext, type ExecutionContext, type QueryExplanation } from "@openplait/adapter-sdk";
|
|
3
|
+
import type { PrometheusAdapterConfig, PrometheusAdapterOptions, PrometheusCompiledRequest } from "./types.js";
|
|
4
|
+
export declare const PROMETHEUS_ADAPTER_MANIFEST: AdapterManifest;
|
|
5
|
+
export declare const PROMETHEUS_CAPABILITIES: DatasourceCapabilities;
|
|
6
|
+
export declare function validatePrometheusConfig(config: PrometheusAdapterConfig): AdapterValidationResult<PrometheusAdapterConfig>;
|
|
7
|
+
export declare class PrometheusAdapter implements DatasourceAdapter<PrometheusAdapterConfig, PrometheusCompiledRequest> {
|
|
8
|
+
readonly manifest: AdapterManifest;
|
|
9
|
+
private readonly config;
|
|
10
|
+
private readonly fetchImplementation;
|
|
11
|
+
private readonly now;
|
|
12
|
+
private readonly compiled;
|
|
13
|
+
constructor(config: PrometheusAdapterConfig, options?: PrometheusAdapterOptions);
|
|
14
|
+
validateConfig(config: PrometheusAdapterConfig): AdapterValidationResult<PrometheusAdapterConfig>;
|
|
15
|
+
getCapabilities(): Promise<DatasourceCapabilities>;
|
|
16
|
+
compile(query: SemanticQuery, context: CompileContext): Promise<CompiledQuery<PrometheusCompiledRequest>>;
|
|
17
|
+
private native;
|
|
18
|
+
execute(query: CompiledQuery<PrometheusCompiledRequest> | NativeQuery, context: ExecutionContext): Promise<QueryResult>;
|
|
19
|
+
explain(query: SemanticQuery, context: CompileContext): Promise<QueryExplanation<PrometheusCompiledRequest>>;
|
|
20
|
+
discoverSchema(_context: DiscoveryContext): Promise<SemanticSchema>;
|
|
21
|
+
private discoveryTimeParams;
|
|
22
|
+
private discovery;
|
|
23
|
+
labelNames(context: DiscoveryContext): Promise<string[]>;
|
|
24
|
+
labelValues(label: string, context: DiscoveryContext): Promise<string[]>;
|
|
25
|
+
metricNames(context: DiscoveryContext): Promise<string[]>;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC/F,OAAO,EAA0J,KAAK,eAAe,EAA+B,KAAK,uBAAuB,EAAE,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,KAAK,iBAAiB,EAAE,KAAK,sBAAsB,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAGpb,OAAO,KAAK,EAAE,uBAAuB,EAAE,wBAAwB,EAAyB,yBAAyB,EAA2B,MAAM,YAAY,CAAC;AAI/J,eAAO,MAAM,2BAA2B,EAAE,eAA6J,CAAC;AACxM,eAAO,MAAM,uBAAuB,EAAE,sBAAiQ,CAAC;AAExS,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,uBAAuB,GAAG,uBAAuB,CAAC,uBAAuB,CAAC,CAQ1H;AAED,qBAAa,iBAAkB,YAAW,iBAAiB,CAAC,uBAAuB,EAAE,yBAAyB,CAAC;IAC7G,QAAQ,CAAC,QAAQ,kBAA+B;IAChD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IACjD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAe;IACnD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyB;gBACtC,MAAM,EAAE,uBAAuB,EAAE,OAAO,GAAE,wBAA6B;IAOnF,cAAc,CAAC,MAAM,EAAE,uBAAuB;IACxC,eAAe,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAClD,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,yBAAyB,CAAC,CAAC;IAC/G,OAAO,CAAC,MAAM;IAQR,OAAO,CAAC,KAAK,EAAE,aAAa,CAAC,yBAAyB,CAAC,GAAG,WAAW,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC;IAQvH,OAAO,CAAC,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,gBAAgB,CAAC,yBAAyB,CAAC,CAAC;IAC5G,cAAc,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,cAAc,CAAC;IACzE,OAAO,CAAC,mBAAmB;YAOb,SAAS;IACjB,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACxD,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACxE,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAChE"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { AdapterError, datasourceUrl, deepFreeze, httpHeaders, isCompiledQuery, requestJson, requireSafeRequestId, validateHttpAdapterConfig, boundedEpochRange } from "@openplait/adapter-sdk";
|
|
2
|
+
import { compilePrometheusQuery, parsePrometheusStep, PROMETHEUS_DATASET } from "./compiler.js";
|
|
3
|
+
import { normalizePrometheusResponse } from "./normalization.js";
|
|
4
|
+
const VERSION = "0.1.0-alpha.0";
|
|
5
|
+
const DEFAULT_TIMEOUT = 30_000;
|
|
6
|
+
export const PROMETHEUS_ADAPTER_MANIFEST = { name: "prometheus", displayName: "Prometheus", version: VERSION, queryLanguages: ["openplait-semantic", "promql"], signals: ["metrics"] };
|
|
7
|
+
export const PROMETHEUS_CAPABILITIES = { signals: ["metrics"], operations: ["select", "filter", "aggregate", "group", "limit", "rate", "timeBucket"], joins: { local: false, crossDatasource: false }, nativeQueryLanguages: ["promql"], explain: true, schemaDiscovery: true };
|
|
8
|
+
export function validatePrometheusConfig(config) {
|
|
9
|
+
const errors = validateHttpAdapterConfig(config);
|
|
10
|
+
for (const [path, value] of [["/maxResultRows", config.maxResultRows], ["/maxSeries", config.maxSeries], ["/maxTimeRangeMs", config.maxTimeRangeMs], ["/maxLookbackMs", config.maxLookbackMs]]) {
|
|
11
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0))
|
|
12
|
+
errors.push({ code: "CONFIG_INVALID", path, message: "Value must be a positive integer." });
|
|
13
|
+
}
|
|
14
|
+
if (config.defaultStep !== undefined)
|
|
15
|
+
try {
|
|
16
|
+
parsePrometheusStep(config.defaultStep);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
errors.push({ code: "CONFIG_INVALID", path: "/defaultStep", message: "Step must be a duration such as 30s or 5m." });
|
|
20
|
+
}
|
|
21
|
+
for (const [field, label] of Object.entries(config.labelFields ?? {}))
|
|
22
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(label))
|
|
23
|
+
errors.push({ code: "CONFIG_INVALID", path: `/labelFields/${field}`, message: "Prometheus label name is invalid." });
|
|
24
|
+
return errors.length ? { valid: false, errors } : { valid: true, value: config, errors: [] };
|
|
25
|
+
}
|
|
26
|
+
export class PrometheusAdapter {
|
|
27
|
+
manifest = PROMETHEUS_ADAPTER_MANIFEST;
|
|
28
|
+
config;
|
|
29
|
+
fetchImplementation;
|
|
30
|
+
now;
|
|
31
|
+
compiled = new WeakSet();
|
|
32
|
+
constructor(config, options = {}) {
|
|
33
|
+
const validation = validatePrometheusConfig(config);
|
|
34
|
+
if (!validation.valid)
|
|
35
|
+
throw new AdapterError("CONFIG_INVALID", "Prometheus adapter config is invalid.", { details: { errors: validation.errors } });
|
|
36
|
+
this.config = { ...config, url: config.url.replace(/\/+$/, "") };
|
|
37
|
+
this.fetchImplementation = options.fetch ?? fetch;
|
|
38
|
+
this.now = options.now ?? Date.now;
|
|
39
|
+
}
|
|
40
|
+
validateConfig(config) { return validatePrometheusConfig(config); }
|
|
41
|
+
async getCapabilities() { return { ...PROMETHEUS_CAPABILITIES, limits: { ...(this.config.maxResultRows ? { maxResultRows: this.config.maxResultRows } : {}), ...(this.config.maxTimeRangeMs ? { maxTimeRangeMs: this.config.maxTimeRangeMs } : {}), ...(this.config.maxLookbackMs ? { maxLookbackMs: this.config.maxLookbackMs } : {}) } }; }
|
|
42
|
+
async compile(query, context) { const result = deepFreeze(compilePrometheusQuery(query, this.config, context)); this.compiled.add(result); return result; }
|
|
43
|
+
native(query) {
|
|
44
|
+
if (query.spec.native.language.toLowerCase() !== "promql" || !query.spec.native.statement.trim() || /\0/.test(query.spec.native.statement))
|
|
45
|
+
throw new AdapterError("NATIVE_QUERY_REJECTED", "Native query must contain valid PromQL.");
|
|
46
|
+
const extension = query.spec.extensions?.["io.openplait.prometheus"];
|
|
47
|
+
if (!extension?.timeRange)
|
|
48
|
+
throw new AdapterError("TIME_RANGE_REQUIRED", "Native PromQL range queries require io.openplait.prometheus.timeRange.");
|
|
49
|
+
const range = boundedEpochRange(extension.timeRange.from, extension.timeRange.to, this.config.maxTimeRangeMs);
|
|
50
|
+
const step = extension.step ?? this.config.defaultStep ?? "30s";
|
|
51
|
+
parsePrometheusStep(step);
|
|
52
|
+
return { endpoint: "query_range", promql: query.spec.native.statement, start: range.startMs / 1000, end: range.endMs / 1000, step };
|
|
53
|
+
}
|
|
54
|
+
async execute(query, context) {
|
|
55
|
+
let request;
|
|
56
|
+
let frameName;
|
|
57
|
+
if (isCompiledQuery(query)) {
|
|
58
|
+
if (!this.compiled.has(query))
|
|
59
|
+
throw new AdapterError("QUERY_VALIDATION_FAILED", "Compiled query was not produced by this adapter instance.");
|
|
60
|
+
request = query.compiled;
|
|
61
|
+
frameName = query.source.resourceName;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
if (!this.config.allowNativeQueries)
|
|
65
|
+
throw new AdapterError("NATIVE_QUERY_REJECTED", "Native PromQL is disabled for this datasource.");
|
|
66
|
+
request = this.native(query);
|
|
67
|
+
frameName = query.metadata.name;
|
|
68
|
+
}
|
|
69
|
+
const started = this.now();
|
|
70
|
+
const response = await requestJson(datasourceUrl(this.config.url, "api/v1/query_range", { query: request.promql, start: request.start, end: request.end, step: request.step }), { fetch: this.fetchImplementation, datasourceName: "Prometheus", signal: context.abortSignal, timeoutMs: Math.min(context.timeoutMs ?? this.config.queryTimeoutMs ?? DEFAULT_TIMEOUT, this.config.queryTimeoutMs ?? DEFAULT_TIMEOUT), requestId: requireSafeRequestId(context.queryId ?? context.audit?.requestId), headers: httpHeaders(this.config) });
|
|
71
|
+
return normalizePrometheusResponse(response, frameName, VERSION, Math.max(0, this.now() - started));
|
|
72
|
+
}
|
|
73
|
+
async explain(query, context) { const compiledQuery = await this.compile(query, context); return { summary: "Compile the semantic metric query to bounded PromQL.", steps: ["Map canonical fields to labels.", "Compile safe label matchers.", "Apply aggregation and a bounded range step."], compiledQuery, warnings: compiledQuery.warnings }; }
|
|
74
|
+
async discoverSchema(_context) { return { datasets: [{ name: PROMETHEUS_DATASET, signal: "metrics", fields: [{ name: "timestamp", type: "time", signals: ["metrics"] }, { name: "value", type: "number", signals: ["metrics"] }, { name: "metric.name", type: "string", signals: ["metrics"] }, { name: "labels", type: "json", signals: ["metrics"] }], supportedAggregations: ["sum", "avg", "min", "max", "count", "rate"] }] }; }
|
|
75
|
+
discoveryTimeParams(context) {
|
|
76
|
+
if (!context.timeRange)
|
|
77
|
+
return {};
|
|
78
|
+
const startMs = new Date(context.timeRange.from).getTime();
|
|
79
|
+
const endMs = new Date(context.timeRange.to).getTime();
|
|
80
|
+
if (!Number.isFinite(startMs) || !Number.isFinite(endMs))
|
|
81
|
+
return {};
|
|
82
|
+
return { start: Math.floor(startMs / 1000), end: Math.ceil(endMs / 1000) };
|
|
83
|
+
}
|
|
84
|
+
async discovery(path, context) { const response = await requestJson(datasourceUrl(this.config.url, path, this.discoveryTimeParams(context)), { fetch: this.fetchImplementation, datasourceName: "Prometheus", signal: context.abortSignal, timeoutMs: context.timeoutMs ?? this.config.requestTimeoutMs ?? DEFAULT_TIMEOUT, headers: httpHeaders(this.config) }); return Array.isArray(response.data) ? response.data.filter((item) => typeof item === "string") : []; }
|
|
85
|
+
async labelNames(context) { return this.discovery("api/v1/labels", context); }
|
|
86
|
+
async labelValues(label, context) { if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(label))
|
|
87
|
+
throw new AdapterError("INVALID_PARAMETER", "Prometheus label name is invalid."); return this.discovery(`api/v1/label/${encodeURIComponent(label)}/values`, context); }
|
|
88
|
+
async metricNames(context) { return this.labelValues("__name__", context); }
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,WAAW,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,iBAAiB,EAAsP,MAAM,wBAAwB,CAAC;AACpb,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAChG,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAGjE,MAAM,OAAO,GAAG,eAAe,CAAC;AAChC,MAAM,eAAe,GAAG,MAAM,CAAC;AAC/B,MAAM,CAAC,MAAM,2BAA2B,GAAoB,EAAE,IAAI,EAAE,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC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CAAC,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,gBAAgB,kBAAkB,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACrS,KAAK,CAAC,WAAW,CAAC,OAAyB,IAAuB,OAAO,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;CAClH"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SemanticQuery } from "@openplait/core";
|
|
2
|
+
import { type CompileContext, type CompiledQuery } from "@openplait/adapter-sdk";
|
|
3
|
+
import type { PrometheusAdapterConfig, PrometheusCompiledRequest } from "./types.js";
|
|
4
|
+
export declare const PROMETHEUS_DATASET = "prometheus.samples";
|
|
5
|
+
export declare function compilePrometheusQuery(query: SemanticQuery, config: PrometheusAdapterConfig, context?: CompileContext): CompiledQuery<PrometheusCompiledRequest>;
|
|
6
|
+
export declare function parsePrometheusStep(step: string): number;
|
|
7
|
+
//# sourceMappingURL=compiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../../src/compiler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2C,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE9F,OAAO,EAAmC,KAAK,cAAc,EAAsB,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACtI,OAAO,KAAK,EAAE,uBAAuB,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAErF,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAoCvD,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,uBAAuB,EAAE,OAAO,GAAE,cAAmB,GAAG,aAAa,CAAC,yBAAyB,CAAC,CA4BpK;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAA8B"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { validateQuery } from "@openplait/core";
|
|
2
|
+
import { AdapterError, boundedEpochRange } from "@openplait/adapter-sdk";
|
|
3
|
+
export const PROMETHEUS_DATASET = "prometheus.samples";
|
|
4
|
+
const BUILTIN_LABELS = { "metric.name": "__name__", "service.name": "service_name", job: "job", instance: "instance" };
|
|
5
|
+
function fail(code, message, path) {
|
|
6
|
+
throw new AdapterError(code, message, { path });
|
|
7
|
+
}
|
|
8
|
+
function escape(value) { return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/[\r\n\0]/g, " ")}"`; }
|
|
9
|
+
function labelName(field, config) {
|
|
10
|
+
const name = config.labelFields?.[field] ?? BUILTIN_LABELS[field] ?? field;
|
|
11
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name))
|
|
12
|
+
fail("FIELD_NOT_FOUND", `Field '${field}' is not mapped to a Prometheus label.`, "/spec/where");
|
|
13
|
+
return name;
|
|
14
|
+
}
|
|
15
|
+
function comparison(expression, config) {
|
|
16
|
+
if (expression.operator === "isNull" || expression.operator === "isNotNull") {
|
|
17
|
+
return `${labelName(expression.field, config)}${expression.operator === "isNull" ? "=" : "!="}\"\"`;
|
|
18
|
+
}
|
|
19
|
+
if (!("value" in expression) || Array.isArray(expression.value))
|
|
20
|
+
fail("UNSUPPORTED_OPERATION", "Prometheus label filters require one scalar value.", "/spec/where");
|
|
21
|
+
const value = String(expression.value ?? "");
|
|
22
|
+
const operators = { equals: "=", notEquals: "!=", matches: "=~", contains: "=~", notContains: "!~", startsWith: "=~", endsWith: "=~" };
|
|
23
|
+
const op = operators[expression.operator];
|
|
24
|
+
if (!op)
|
|
25
|
+
fail("UNSUPPORTED_OPERATION", `Prometheus does not support '${expression.operator}' for labels.`, "/spec/where");
|
|
26
|
+
const pattern = expression.operator === "contains" || expression.operator === "notContains" ? `.*${value}.*` : expression.operator === "startsWith" ? `${value}.*` : expression.operator === "endsWith" ? `.*${value}` : value;
|
|
27
|
+
return `${labelName(expression.field, config)}${op}${escape(pattern)}`;
|
|
28
|
+
}
|
|
29
|
+
function matchers(expression, config) {
|
|
30
|
+
if (!expression)
|
|
31
|
+
return [];
|
|
32
|
+
if ("and" in expression)
|
|
33
|
+
return expression.and.flatMap((item) => matchers(item, config));
|
|
34
|
+
if ("or" in expression || "not" in expression)
|
|
35
|
+
fail("UNSUPPORTED_OPERATION", "Prometheus semantic selectors currently support conjunctions only.", "/spec/where");
|
|
36
|
+
return [comparison(expression, config)];
|
|
37
|
+
}
|
|
38
|
+
function stepSeconds(step) {
|
|
39
|
+
const match = step.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);
|
|
40
|
+
if (!match)
|
|
41
|
+
fail("INVALID_PARAMETER", "Prometheus step must be a positive duration such as 30s or 5m.", "/step");
|
|
42
|
+
const multiplier = { ms: .001, s: 1, m: 60, h: 3600, d: 86400 }[match[2]];
|
|
43
|
+
return Number(match[1]) * multiplier;
|
|
44
|
+
}
|
|
45
|
+
export function compilePrometheusQuery(query, config, context = {}) {
|
|
46
|
+
const validation = validateQuery(query);
|
|
47
|
+
if (!validation.valid)
|
|
48
|
+
fail("QUERY_VALIDATION_FAILED", "The semantic query is not valid.", "/spec");
|
|
49
|
+
if (query.spec.input.signal !== "metrics" || query.spec.input.entity !== PROMETHEUS_DATASET)
|
|
50
|
+
fail("DATASET_NOT_FOUND", `Dataset '${query.spec.input.entity}' is not registered.`, "/spec/input/entity");
|
|
51
|
+
if (!query.spec.timeRange)
|
|
52
|
+
fail("TIME_RANGE_REQUIRED", "Prometheus range queries require a bounded time range.", "/spec/timeRange");
|
|
53
|
+
if (query.spec.joins?.length)
|
|
54
|
+
fail("UNSUPPORTED_OPERATION", "Prometheus does not support joins.", "/spec/joins");
|
|
55
|
+
const { startMs, endMs } = boundedEpochRange(query.spec.timeRange.from, query.spec.timeRange.to, config.maxTimeRangeMs);
|
|
56
|
+
const step = config.defaultStep ?? "30s";
|
|
57
|
+
stepSeconds(step);
|
|
58
|
+
const selector = `{${matchers(query.spec.where, config).join(",")}}`;
|
|
59
|
+
const aggregate = query.spec.select.find((item) => "aggregate" in item);
|
|
60
|
+
let promql = selector;
|
|
61
|
+
if (aggregate && "aggregate" in aggregate) {
|
|
62
|
+
const fn = aggregate.aggregate.function;
|
|
63
|
+
const groups = (query.spec.groupBy ?? []).flatMap((item) => "field" in item ? [labelName(item.field, config)] : []);
|
|
64
|
+
if (fn === "rate")
|
|
65
|
+
promql = `rate(${selector}[${step}])`;
|
|
66
|
+
else if (["sum", "avg", "min", "max", "count"].includes(fn))
|
|
67
|
+
promql = `${fn}${groups.length ? ` by (${groups.join(",")})` : ""} (${selector})`;
|
|
68
|
+
else
|
|
69
|
+
fail("UNSUPPORTED_OPERATION", `Prometheus aggregation '${fn}' is not supported by the semantic compiler.`, "/spec/select");
|
|
70
|
+
}
|
|
71
|
+
const fields = [
|
|
72
|
+
{ name: "timestamp", type: "time", source: "timestamp" },
|
|
73
|
+
{ name: "value", type: "number", source: "value" },
|
|
74
|
+
{ name: "labels", type: "json", source: "labels" },
|
|
75
|
+
];
|
|
76
|
+
return {
|
|
77
|
+
kind: "CompiledQuery", language: "promql", compiled: { endpoint: "query_range", promql, start: startMs / 1000, end: endMs / 1000, step },
|
|
78
|
+
parameters: {}, fields, source: { resourceName: query.metadata.name, dataset: PROMETHEUS_DATASET }, warnings: [],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function parsePrometheusStep(step) { return stepSeconds(step); }
|
|
82
|
+
//# sourceMappingURL=compiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compiler.js","sourceRoot":"","sources":["../../src/compiler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAA+D,MAAM,wBAAwB,CAAC;AAGtI,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AACvD,MAAM,cAAc,GA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fig } from "./adapter.js";
|
|
2
|
+
export { compilePrometheusQuery, PROMETHEUS_DATASET } from "./compiler.js";
|
|
3
|
+
export { normalizePrometheusResponse } from "./normalization.js";
|
|
4
|
+
export type * from "./types.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACjI,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,mBAAmB,YAAY,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { PrometheusAdapter, PROMETHEUS_ADAPTER_MANIFEST, PROMETHEUS_CAPABILITIES, validatePrometheusConfig } from "./adapter.js";
|
|
2
|
+
export { compilePrometheusQuery, PROMETHEUS_DATASET } from "./compiler.js";
|
|
3
|
+
export { normalizePrometheusResponse } from "./normalization.js";
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACjI,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { QueryResult } from "@openplait/core";
|
|
2
|
+
import type { PrometheusApiResponse } from "./types.js";
|
|
3
|
+
export declare function normalizePrometheusResponse(response: PrometheusApiResponse, frameName: string, adapterVersion: string, executionTimeMs: number): QueryResult;
|
|
4
|
+
//# sourceMappingURL=normalization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalization.d.ts","sourceRoot":"","sources":["../../src/normalization.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAExD,wBAAgB,2BAA2B,CAAC,QAAQ,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,WAAW,CAuB5J"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AdapterError } from "@openplait/adapter-sdk";
|
|
2
|
+
export function normalizePrometheusResponse(response, frameName, adapterVersion, executionTimeMs) {
|
|
3
|
+
if (response.status !== "success")
|
|
4
|
+
throw new AdapterError("EXECUTION_FAILED", response.error || "Prometheus query failed.", { details: { errorType: response.errorType } });
|
|
5
|
+
const result = response.data?.result;
|
|
6
|
+
const rows = [];
|
|
7
|
+
if (Array.isArray(result)) {
|
|
8
|
+
if (response.data?.resultType === "scalar" || response.data?.resultType === "string") {
|
|
9
|
+
const scalar = result;
|
|
10
|
+
rows.push({ timestamp: new Date(Number(scalar[0]) * 1000).toISOString(), value: Number(scalar[1]), labels: {} });
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
for (const series of result) {
|
|
14
|
+
const samples = series.values ?? (series.value ? [series.value] : []);
|
|
15
|
+
for (const sample of samples)
|
|
16
|
+
rows.push({ timestamp: new Date(Number(sample[0]) * 1000).toISOString(), value: Number(sample[1]), labels: series.metric ?? {} });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
frames: [{ name: frameName, length: rows.length, fields: [
|
|
22
|
+
{ name: "timestamp", type: "time", values: rows.map((row) => row.timestamp) },
|
|
23
|
+
{ name: "value", type: "number", values: rows.map((row) => row.value) },
|
|
24
|
+
{ name: "labels", type: "json", values: rows.map((row) => row.labels) },
|
|
25
|
+
] }],
|
|
26
|
+
metadata: { adapterVersion, executionTimeMs, rowsReturned: rows.length, ...(response.warnings?.length ? { warnings: response.warnings } : {}) },
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=normalization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalization.js","sourceRoot":"","sources":["../../src/normalization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAItD,MAAM,UAAU,2BAA2B,CAAC,QAA+B,EAAE,SAAiB,EAAE,cAAsB,EAAE,eAAuB;IAC7I,IAAI,QAAQ,CAAC,MAAM,KAAK,SAAS;QAAE,MAAM,IAAI,YAAY,CAAC,kBAAkB,EAAE,QAAQ,CAAC,KAAK,IAAI,0BAA0B,EAAE,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAC5K,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACrC,MAAM,IAAI,GAAgF,EAAE,CAAC;IAC7F,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,IAAI,QAAQ,CAAC,IAAI,EAAE,UAAU,KAAK,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE,UAAU,KAAK,QAAQ,EAAE,CAAC;YACrF,MAAM,MAAM,GAAG,MAAuD,CAAC;YACvE,IAAI,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,M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AAE,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE;KAChJ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { TimeRange } from "@openplait/core";
|
|
2
|
+
import type { HttpAdapterConfig } from "@openplait/adapter-sdk";
|
|
3
|
+
export interface PrometheusAdapterConfig extends HttpAdapterConfig {
|
|
4
|
+
allowNativeQueries?: boolean;
|
|
5
|
+
maxResultRows?: number;
|
|
6
|
+
maxSeries?: number;
|
|
7
|
+
maxTimeRangeMs?: number;
|
|
8
|
+
maxLookbackMs?: number;
|
|
9
|
+
defaultStep?: string;
|
|
10
|
+
labelFields?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export interface PrometheusAdapterOptions {
|
|
13
|
+
fetch?: typeof fetch;
|
|
14
|
+
now?: () => number;
|
|
15
|
+
}
|
|
16
|
+
export interface PrometheusCompiledRequest {
|
|
17
|
+
endpoint: "query_range";
|
|
18
|
+
promql: string;
|
|
19
|
+
start: number;
|
|
20
|
+
end: number;
|
|
21
|
+
step: string;
|
|
22
|
+
}
|
|
23
|
+
export interface PrometheusNativeOptions {
|
|
24
|
+
timeRange: Omit<TimeRange, "field">;
|
|
25
|
+
step?: string;
|
|
26
|
+
}
|
|
27
|
+
export interface PrometheusApiResponse {
|
|
28
|
+
status: "success" | "error";
|
|
29
|
+
errorType?: string;
|
|
30
|
+
error?: string;
|
|
31
|
+
warnings?: string[];
|
|
32
|
+
data?: {
|
|
33
|
+
resultType?: "matrix" | "vector" | "scalar" | "string";
|
|
34
|
+
result?: Array<{
|
|
35
|
+
metric?: Record<string, string>;
|
|
36
|
+
values?: Array<[number | string, number | string]>;
|
|
37
|
+
value?: [number | string, number | string];
|
|
38
|
+
}> | [number | string, number | string];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAEhE,MAAM,WAAW,uBAAwB,SAAQ,iBAAiB;IAChE,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC;AAED,MAAM,WAAW,wBAAwB;IAAG,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;CAAE;AACtF,MAAM,WAAW,yBAAyB;IAAG,QAAQ,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AAChI,MAAM,WAAW,uBAAuB;IAAG,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE;AAC/F,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,IAAI,CAAC,EAAE;QACL,UAAU,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;QACvD,MAAM,CAAC,EAAE,KAAK,CAAC;YAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAAC,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;YAAC,KAAK,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;SAAE,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;KAC1L,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openplait/adapter-prometheus",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "Prometheus-compatible metrics datasource adapter for OpenPlait.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/src",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/src/index.d.ts",
|
|
15
|
+
"import": "./dist/src/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.build.json",
|
|
20
|
+
"prebuild": "npm run build --workspace @openplait/adapter-sdk",
|
|
21
|
+
"prepack": "npm run build",
|
|
22
|
+
"pretest": "npm run build --workspace @openplait/adapter-sdk",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"pretypecheck": "npm run build --workspace @openplait/adapter-sdk",
|
|
25
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20.19"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@openplait/adapter-sdk": "0.1.0-alpha.0",
|
|
32
|
+
"@openplait/core": "0.1.0-alpha.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^5.9.0",
|
|
36
|
+
"vitest": "^3.2.0"
|
|
37
|
+
},
|
|
38
|
+
"license": "Apache-2.0",
|
|
39
|
+
"author": "OpenLIT",
|
|
40
|
+
"homepage": "https://github.com/openlit/openplait#readme",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/openlit/openplait/issues"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/openlit/openplait.git",
|
|
47
|
+
"directory": "typescript/packages/adapter-prometheus"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"registry": "https://registry.npmjs.org/"
|
|
52
|
+
},
|
|
53
|
+
"keywords": [
|
|
54
|
+
"openplait",
|
|
55
|
+
"openlit",
|
|
56
|
+
"opentelemetry",
|
|
57
|
+
"observability",
|
|
58
|
+
"datasource",
|
|
59
|
+
"adapter",
|
|
60
|
+
"promql",
|
|
61
|
+
"traceql",
|
|
62
|
+
"logql",
|
|
63
|
+
"adapter prometheus"
|
|
64
|
+
]
|
|
65
|
+
}
|