@kopai/collector 0.0.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 +190 -0
- package/dist/index.cjs +214 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +215 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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 the 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
|
+
Copyright 2025 Kopai Authors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
let fastify_type_provider_zod = require("fastify-type-provider-zod");
|
|
2
|
+
let zod_v4 = require("zod/v4");
|
|
3
|
+
let _kopai_core = require("@kopai/core");
|
|
4
|
+
require("fastify");
|
|
5
|
+
|
|
6
|
+
//#region src/routes/metrics.ts
|
|
7
|
+
const exportMetricsServiceResponseSchema = zod_v4.z.object({ partialSuccess: zod_v4.z.object({
|
|
8
|
+
rejectedDataPoints: zod_v4.z.string().optional(),
|
|
9
|
+
errorMessage: zod_v4.z.string().optional()
|
|
10
|
+
}).optional() });
|
|
11
|
+
const metricsRoute = async function(fastify, opts) {
|
|
12
|
+
fastify.route({
|
|
13
|
+
method: "POST",
|
|
14
|
+
url: "/v1/metrics",
|
|
15
|
+
schema: {
|
|
16
|
+
body: _kopai_core.otlpMetricsZod.metricsDataSchema,
|
|
17
|
+
response: { 200: exportMetricsServiceResponseSchema }
|
|
18
|
+
},
|
|
19
|
+
handler: async (req, res) => {
|
|
20
|
+
const { rejectedDataPoints, errorMessage } = await opts.writeMetricsDatasource.writeMetrics(req.body);
|
|
21
|
+
res.send({ partialSuccess: {
|
|
22
|
+
rejectedDataPoints,
|
|
23
|
+
errorMessage
|
|
24
|
+
} });
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/routes/traces.ts
|
|
31
|
+
const exportTracesServiceResponseSchema = zod_v4.z.object({ partialSuccess: zod_v4.z.object({
|
|
32
|
+
rejectedSpans: zod_v4.z.string().optional(),
|
|
33
|
+
errorMessage: zod_v4.z.string().optional()
|
|
34
|
+
}).optional() });
|
|
35
|
+
const tracesRoute = async function(fastify, opts) {
|
|
36
|
+
fastify.route({
|
|
37
|
+
method: "POST",
|
|
38
|
+
url: "/v1/traces",
|
|
39
|
+
schema: {
|
|
40
|
+
body: _kopai_core.otlpZod.tracesDataSchema,
|
|
41
|
+
response: { 200: exportTracesServiceResponseSchema }
|
|
42
|
+
},
|
|
43
|
+
handler: async (req, res) => {
|
|
44
|
+
const { rejectedSpans, errorMessage } = await opts.writeTracesDatasource.writeTraces(req.body);
|
|
45
|
+
res.send({ partialSuccess: {
|
|
46
|
+
rejectedSpans,
|
|
47
|
+
errorMessage
|
|
48
|
+
} });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/routes/logs.ts
|
|
55
|
+
const exportLogsServiceResponseSchema = zod_v4.z.object({ partialSuccess: zod_v4.z.object({
|
|
56
|
+
rejectedLogRecords: zod_v4.z.string().optional(),
|
|
57
|
+
errorMessage: zod_v4.z.string().optional()
|
|
58
|
+
}).optional() });
|
|
59
|
+
const logsRoute = async function(fastify, opts) {
|
|
60
|
+
fastify.route({
|
|
61
|
+
method: "POST",
|
|
62
|
+
url: "/v1/logs",
|
|
63
|
+
schema: {
|
|
64
|
+
body: _kopai_core.otlpZod.logsDataSchema,
|
|
65
|
+
response: { 200: exportLogsServiceResponseSchema }
|
|
66
|
+
},
|
|
67
|
+
handler: async (req, res) => {
|
|
68
|
+
const { rejectedLogRecords, errorMessage } = await opts.writeLogsDatasource.writeLogs(req.body);
|
|
69
|
+
res.send({ partialSuccess: {
|
|
70
|
+
rejectedLogRecords,
|
|
71
|
+
errorMessage
|
|
72
|
+
} });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/routes/otlp-schemas.ts
|
|
79
|
+
const grpcStatusCode = {
|
|
80
|
+
OK: 0,
|
|
81
|
+
CANCELLED: 1,
|
|
82
|
+
UNKNOWN: 2,
|
|
83
|
+
INVALID_ARGUMENT: 3,
|
|
84
|
+
DEADLINE_EXCEEDED: 4,
|
|
85
|
+
NOT_FOUND: 5,
|
|
86
|
+
ALREADY_EXISTS: 6,
|
|
87
|
+
PERMISSION_DENIED: 7,
|
|
88
|
+
RESOURCE_EXHAUSTED: 8,
|
|
89
|
+
FAILED_PRECONDITION: 9,
|
|
90
|
+
ABORTED: 10,
|
|
91
|
+
OUT_OF_RANGE: 11,
|
|
92
|
+
UNIMPLEMENTED: 12,
|
|
93
|
+
INTERNAL: 13,
|
|
94
|
+
UNAVAILABLE: 14,
|
|
95
|
+
DATA_LOSS: 15,
|
|
96
|
+
UNAUTHENTICATED: 16
|
|
97
|
+
};
|
|
98
|
+
const grpcStatusSchema = zod_v4.z.object({
|
|
99
|
+
message: zod_v4.z.string(),
|
|
100
|
+
details: zod_v4.z.array(zod_v4.z.unknown()).optional()
|
|
101
|
+
});
|
|
102
|
+
const grpcStatusCodeSchema = zod_v4.z.number().int().min(0).max(16);
|
|
103
|
+
const otlpErrorResponseSchema = zod_v4.z.object({
|
|
104
|
+
code: grpcStatusCodeSchema,
|
|
105
|
+
message: zod_v4.z.string(),
|
|
106
|
+
details: zod_v4.z.array(zod_v4.z.unknown()).optional()
|
|
107
|
+
});
|
|
108
|
+
const fieldViolationSchema = zod_v4.z.object({
|
|
109
|
+
field: zod_v4.z.string(),
|
|
110
|
+
description: zod_v4.z.string(),
|
|
111
|
+
reason: zod_v4.z.string().optional()
|
|
112
|
+
});
|
|
113
|
+
const badRequestSchema = zod_v4.z.object({
|
|
114
|
+
"@type": zod_v4.z.literal("type.googleapis.com/google.rpc.BadRequest").optional(),
|
|
115
|
+
fieldViolations: zod_v4.z.array(fieldViolationSchema)
|
|
116
|
+
});
|
|
117
|
+
const otlpBadRequestErrorResponseSchema = zod_v4.z.object({
|
|
118
|
+
code: zod_v4.z.number().int(),
|
|
119
|
+
message: zod_v4.z.string(),
|
|
120
|
+
details: zod_v4.z.array(badRequestSchema).optional()
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/routes/field-violation.ts
|
|
125
|
+
function extractLeafError(errors) {
|
|
126
|
+
for (const branch of errors) {
|
|
127
|
+
if (!Array.isArray(branch) || !branch.length) continue;
|
|
128
|
+
const e = branch[0];
|
|
129
|
+
if (e?.code === "invalid_union" && e.errors) {
|
|
130
|
+
const deeper = extractLeafError(e.errors);
|
|
131
|
+
return {
|
|
132
|
+
path: [...e.path || [], ...deeper.path],
|
|
133
|
+
message: deeper.message,
|
|
134
|
+
expected: deeper.expected
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
path: e?.path || [],
|
|
139
|
+
message: e?.message || "Validation failed",
|
|
140
|
+
expected: e?.expected
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
path: [],
|
|
145
|
+
message: "Validation failed"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function toFieldViolation(error) {
|
|
149
|
+
let field = error.instancePath.replace(/^\//, "").replace(/\//g, ".") || "";
|
|
150
|
+
let description = error.message ?? "Validation failed";
|
|
151
|
+
if (error.keyword === "invalid_union" && error.params?.errors) {
|
|
152
|
+
const leaf = extractLeafError(error.params.errors);
|
|
153
|
+
const leafPath = leaf.path.map((p) => typeof p === "number" ? `[${p}]` : `.${p}`).join("").replace(/^\./, "");
|
|
154
|
+
if (leafPath) {
|
|
155
|
+
const sep = leafPath.startsWith("[") ? "" : ".";
|
|
156
|
+
field = field ? `${field}${sep}${leafPath}` : leafPath;
|
|
157
|
+
}
|
|
158
|
+
description = leaf.expected ? `expected ${leaf.expected}` : leaf.message;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
field: field || "unknown",
|
|
162
|
+
description,
|
|
163
|
+
reason: error.keyword
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/routes/errors.ts
|
|
169
|
+
var CollectorError = class CollectorError extends Error {
|
|
170
|
+
constructor(message, code) {
|
|
171
|
+
super(message);
|
|
172
|
+
this.code = code;
|
|
173
|
+
Object.setPrototypeOf(this, CollectorError.prototype);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/routes/error-handler.ts
|
|
179
|
+
function collectorErrorHandler(error, request, reply) {
|
|
180
|
+
if (isValidationError(error)) return reply.status(400).send({
|
|
181
|
+
code: grpcStatusCode.INVALID_ARGUMENT,
|
|
182
|
+
message: "Invalid data",
|
|
183
|
+
details: [{
|
|
184
|
+
"@type": "type.googleapis.com/google.rpc.BadRequest",
|
|
185
|
+
fieldViolations: error.validation.map(toFieldViolation)
|
|
186
|
+
}]
|
|
187
|
+
});
|
|
188
|
+
request.log.error(error);
|
|
189
|
+
if (error instanceof CollectorError) return reply.status(500).send({
|
|
190
|
+
code: error.code,
|
|
191
|
+
message: error.message
|
|
192
|
+
});
|
|
193
|
+
reply.status(500).send({ error: "Internal Server Error" });
|
|
194
|
+
}
|
|
195
|
+
function isFastifyError(error) {
|
|
196
|
+
return error instanceof Error && "code" in error && typeof error.code === "string";
|
|
197
|
+
}
|
|
198
|
+
function isValidationError(error) {
|
|
199
|
+
return isFastifyError(error) && "validation" in error && Array.isArray(error.validation);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/index.ts
|
|
204
|
+
const collectorRoutes = async function(fastify, opts) {
|
|
205
|
+
fastify.setValidatorCompiler(fastify_type_provider_zod.validatorCompiler);
|
|
206
|
+
fastify.setSerializerCompiler(fastify_type_provider_zod.serializerCompiler);
|
|
207
|
+
fastify.setErrorHandler(collectorErrorHandler);
|
|
208
|
+
fastify.register(metricsRoute, { writeMetricsDatasource: opts.telemetryDatasource });
|
|
209
|
+
fastify.register(tracesRoute, { writeTracesDatasource: opts.telemetryDatasource });
|
|
210
|
+
fastify.register(logsRoute, { writeLogsDatasource: opts.telemetryDatasource });
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
//#endregion
|
|
214
|
+
exports.collectorRoutes = collectorRoutes;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FastifyPluginAsyncZod } from "fastify-type-provider-zod";
|
|
2
|
+
import { datasource } from "@kopai/core";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const collectorRoutes: FastifyPluginAsyncZod<{
|
|
6
|
+
telemetryDatasource: datasource.WriteTelemetryDatasource;
|
|
7
|
+
}>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { collectorRoutes };
|
|
10
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;cAYa,eAAA,EAAiB,qBAAA;EAC5B,mBAAA,EAAqB,UAAA,CAAW,wBAAA;AAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FastifyPluginAsyncZod } from "fastify-type-provider-zod";
|
|
2
|
+
import { datasource } from "@kopai/core";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const collectorRoutes: FastifyPluginAsyncZod<{
|
|
6
|
+
telemetryDatasource: datasource.WriteTelemetryDatasource;
|
|
7
|
+
}>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { collectorRoutes };
|
|
10
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;cAYa,eAAA,EAAiB,qBAAA;EAC5B,mBAAA,EAAqB,UAAA,CAAW,wBAAA;AAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { serializerCompiler, validatorCompiler } from "fastify-type-provider-zod";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { otlpMetricsZod, otlpZod } from "@kopai/core";
|
|
4
|
+
import "fastify";
|
|
5
|
+
|
|
6
|
+
//#region src/routes/metrics.ts
|
|
7
|
+
const exportMetricsServiceResponseSchema = z.object({ partialSuccess: z.object({
|
|
8
|
+
rejectedDataPoints: z.string().optional(),
|
|
9
|
+
errorMessage: z.string().optional()
|
|
10
|
+
}).optional() });
|
|
11
|
+
const metricsRoute = async function(fastify, opts) {
|
|
12
|
+
fastify.route({
|
|
13
|
+
method: "POST",
|
|
14
|
+
url: "/v1/metrics",
|
|
15
|
+
schema: {
|
|
16
|
+
body: otlpMetricsZod.metricsDataSchema,
|
|
17
|
+
response: { 200: exportMetricsServiceResponseSchema }
|
|
18
|
+
},
|
|
19
|
+
handler: async (req, res) => {
|
|
20
|
+
const { rejectedDataPoints, errorMessage } = await opts.writeMetricsDatasource.writeMetrics(req.body);
|
|
21
|
+
res.send({ partialSuccess: {
|
|
22
|
+
rejectedDataPoints,
|
|
23
|
+
errorMessage
|
|
24
|
+
} });
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/routes/traces.ts
|
|
31
|
+
const exportTracesServiceResponseSchema = z.object({ partialSuccess: z.object({
|
|
32
|
+
rejectedSpans: z.string().optional(),
|
|
33
|
+
errorMessage: z.string().optional()
|
|
34
|
+
}).optional() });
|
|
35
|
+
const tracesRoute = async function(fastify, opts) {
|
|
36
|
+
fastify.route({
|
|
37
|
+
method: "POST",
|
|
38
|
+
url: "/v1/traces",
|
|
39
|
+
schema: {
|
|
40
|
+
body: otlpZod.tracesDataSchema,
|
|
41
|
+
response: { 200: exportTracesServiceResponseSchema }
|
|
42
|
+
},
|
|
43
|
+
handler: async (req, res) => {
|
|
44
|
+
const { rejectedSpans, errorMessage } = await opts.writeTracesDatasource.writeTraces(req.body);
|
|
45
|
+
res.send({ partialSuccess: {
|
|
46
|
+
rejectedSpans,
|
|
47
|
+
errorMessage
|
|
48
|
+
} });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/routes/logs.ts
|
|
55
|
+
const exportLogsServiceResponseSchema = z.object({ partialSuccess: z.object({
|
|
56
|
+
rejectedLogRecords: z.string().optional(),
|
|
57
|
+
errorMessage: z.string().optional()
|
|
58
|
+
}).optional() });
|
|
59
|
+
const logsRoute = async function(fastify, opts) {
|
|
60
|
+
fastify.route({
|
|
61
|
+
method: "POST",
|
|
62
|
+
url: "/v1/logs",
|
|
63
|
+
schema: {
|
|
64
|
+
body: otlpZod.logsDataSchema,
|
|
65
|
+
response: { 200: exportLogsServiceResponseSchema }
|
|
66
|
+
},
|
|
67
|
+
handler: async (req, res) => {
|
|
68
|
+
const { rejectedLogRecords, errorMessage } = await opts.writeLogsDatasource.writeLogs(req.body);
|
|
69
|
+
res.send({ partialSuccess: {
|
|
70
|
+
rejectedLogRecords,
|
|
71
|
+
errorMessage
|
|
72
|
+
} });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/routes/otlp-schemas.ts
|
|
79
|
+
const grpcStatusCode = {
|
|
80
|
+
OK: 0,
|
|
81
|
+
CANCELLED: 1,
|
|
82
|
+
UNKNOWN: 2,
|
|
83
|
+
INVALID_ARGUMENT: 3,
|
|
84
|
+
DEADLINE_EXCEEDED: 4,
|
|
85
|
+
NOT_FOUND: 5,
|
|
86
|
+
ALREADY_EXISTS: 6,
|
|
87
|
+
PERMISSION_DENIED: 7,
|
|
88
|
+
RESOURCE_EXHAUSTED: 8,
|
|
89
|
+
FAILED_PRECONDITION: 9,
|
|
90
|
+
ABORTED: 10,
|
|
91
|
+
OUT_OF_RANGE: 11,
|
|
92
|
+
UNIMPLEMENTED: 12,
|
|
93
|
+
INTERNAL: 13,
|
|
94
|
+
UNAVAILABLE: 14,
|
|
95
|
+
DATA_LOSS: 15,
|
|
96
|
+
UNAUTHENTICATED: 16
|
|
97
|
+
};
|
|
98
|
+
const grpcStatusSchema = z.object({
|
|
99
|
+
message: z.string(),
|
|
100
|
+
details: z.array(z.unknown()).optional()
|
|
101
|
+
});
|
|
102
|
+
const grpcStatusCodeSchema = z.number().int().min(0).max(16);
|
|
103
|
+
const otlpErrorResponseSchema = z.object({
|
|
104
|
+
code: grpcStatusCodeSchema,
|
|
105
|
+
message: z.string(),
|
|
106
|
+
details: z.array(z.unknown()).optional()
|
|
107
|
+
});
|
|
108
|
+
const fieldViolationSchema = z.object({
|
|
109
|
+
field: z.string(),
|
|
110
|
+
description: z.string(),
|
|
111
|
+
reason: z.string().optional()
|
|
112
|
+
});
|
|
113
|
+
const badRequestSchema = z.object({
|
|
114
|
+
"@type": z.literal("type.googleapis.com/google.rpc.BadRequest").optional(),
|
|
115
|
+
fieldViolations: z.array(fieldViolationSchema)
|
|
116
|
+
});
|
|
117
|
+
const otlpBadRequestErrorResponseSchema = z.object({
|
|
118
|
+
code: z.number().int(),
|
|
119
|
+
message: z.string(),
|
|
120
|
+
details: z.array(badRequestSchema).optional()
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/routes/field-violation.ts
|
|
125
|
+
function extractLeafError(errors) {
|
|
126
|
+
for (const branch of errors) {
|
|
127
|
+
if (!Array.isArray(branch) || !branch.length) continue;
|
|
128
|
+
const e = branch[0];
|
|
129
|
+
if (e?.code === "invalid_union" && e.errors) {
|
|
130
|
+
const deeper = extractLeafError(e.errors);
|
|
131
|
+
return {
|
|
132
|
+
path: [...e.path || [], ...deeper.path],
|
|
133
|
+
message: deeper.message,
|
|
134
|
+
expected: deeper.expected
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
path: e?.path || [],
|
|
139
|
+
message: e?.message || "Validation failed",
|
|
140
|
+
expected: e?.expected
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
path: [],
|
|
145
|
+
message: "Validation failed"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function toFieldViolation(error) {
|
|
149
|
+
let field = error.instancePath.replace(/^\//, "").replace(/\//g, ".") || "";
|
|
150
|
+
let description = error.message ?? "Validation failed";
|
|
151
|
+
if (error.keyword === "invalid_union" && error.params?.errors) {
|
|
152
|
+
const leaf = extractLeafError(error.params.errors);
|
|
153
|
+
const leafPath = leaf.path.map((p) => typeof p === "number" ? `[${p}]` : `.${p}`).join("").replace(/^\./, "");
|
|
154
|
+
if (leafPath) {
|
|
155
|
+
const sep = leafPath.startsWith("[") ? "" : ".";
|
|
156
|
+
field = field ? `${field}${sep}${leafPath}` : leafPath;
|
|
157
|
+
}
|
|
158
|
+
description = leaf.expected ? `expected ${leaf.expected}` : leaf.message;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
field: field || "unknown",
|
|
162
|
+
description,
|
|
163
|
+
reason: error.keyword
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/routes/errors.ts
|
|
169
|
+
var CollectorError = class CollectorError extends Error {
|
|
170
|
+
constructor(message, code) {
|
|
171
|
+
super(message);
|
|
172
|
+
this.code = code;
|
|
173
|
+
Object.setPrototypeOf(this, CollectorError.prototype);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/routes/error-handler.ts
|
|
179
|
+
function collectorErrorHandler(error, request, reply) {
|
|
180
|
+
if (isValidationError(error)) return reply.status(400).send({
|
|
181
|
+
code: grpcStatusCode.INVALID_ARGUMENT,
|
|
182
|
+
message: "Invalid data",
|
|
183
|
+
details: [{
|
|
184
|
+
"@type": "type.googleapis.com/google.rpc.BadRequest",
|
|
185
|
+
fieldViolations: error.validation.map(toFieldViolation)
|
|
186
|
+
}]
|
|
187
|
+
});
|
|
188
|
+
request.log.error(error);
|
|
189
|
+
if (error instanceof CollectorError) return reply.status(500).send({
|
|
190
|
+
code: error.code,
|
|
191
|
+
message: error.message
|
|
192
|
+
});
|
|
193
|
+
reply.status(500).send({ error: "Internal Server Error" });
|
|
194
|
+
}
|
|
195
|
+
function isFastifyError(error) {
|
|
196
|
+
return error instanceof Error && "code" in error && typeof error.code === "string";
|
|
197
|
+
}
|
|
198
|
+
function isValidationError(error) {
|
|
199
|
+
return isFastifyError(error) && "validation" in error && Array.isArray(error.validation);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/index.ts
|
|
204
|
+
const collectorRoutes = async function(fastify, opts) {
|
|
205
|
+
fastify.setValidatorCompiler(validatorCompiler);
|
|
206
|
+
fastify.setSerializerCompiler(serializerCompiler);
|
|
207
|
+
fastify.setErrorHandler(collectorErrorHandler);
|
|
208
|
+
fastify.register(metricsRoute, { writeMetricsDatasource: opts.telemetryDatasource });
|
|
209
|
+
fastify.register(tracesRoute, { writeTracesDatasource: opts.telemetryDatasource });
|
|
210
|
+
fastify.register(logsRoute, { writeLogsDatasource: opts.telemetryDatasource });
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
//#endregion
|
|
214
|
+
export { collectorRoutes };
|
|
215
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/routes/metrics.ts","../src/routes/traces.ts","../src/routes/logs.ts","../src/routes/otlp-schemas.ts","../src/routes/field-violation.ts","../src/routes/errors.ts","../src/routes/error-handler.ts","../src/index.ts"],"sourcesContent":["import { z } from \"zod/v4\";\nimport type { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\";\nimport { otlpMetricsZod, type datasource } from \"@kopai/core\";\n\n// https://github.com/open-telemetry/opentelemetry-specification/blob/49845849d2d8df07059f82033f39e96c561927cf/oteps/0122-otlp-http-json.md#response\nconst exportMetricsServiceResponseSchema = z.object({\n partialSuccess: z\n .object({\n rejectedDataPoints: z.string().optional(),\n errorMessage: z.string().optional(),\n })\n .optional(),\n});\n\nexport const metricsRoute: FastifyPluginAsyncZod<{\n writeMetricsDatasource: datasource.WriteMetricsDatasource;\n}> = async function (fastify, opts) {\n fastify.route({\n method: \"POST\",\n url: \"/v1/metrics\",\n schema: {\n body: otlpMetricsZod.metricsDataSchema,\n response: {\n 200: exportMetricsServiceResponseSchema,\n },\n },\n handler: async (req, res) => {\n const { rejectedDataPoints, errorMessage } =\n await opts.writeMetricsDatasource.writeMetrics(req.body);\n\n res.send({\n partialSuccess: {\n rejectedDataPoints,\n errorMessage,\n },\n });\n },\n });\n};\n","import { z } from \"zod/v4\";\nimport type { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\";\nimport { otlpZod, type datasource } from \"@kopai/core\";\n\n// https://github.com/open-telemetry/opentelemetry-specification/blob/49845849d2d8df07059f82033f39e96c561927cf/oteps/0122-otlp-http-json.md#response\nconst exportTracesServiceResponseSchema = z.object({\n partialSuccess: z\n .object({\n rejectedSpans: z.string().optional(),\n errorMessage: z.string().optional(),\n })\n .optional(),\n});\n\nexport const tracesRoute: FastifyPluginAsyncZod<{\n writeTracesDatasource: datasource.WriteTracesDatasource;\n}> = async function (fastify, opts) {\n fastify.route({\n method: \"POST\",\n url: \"/v1/traces\",\n schema: {\n body: otlpZod.tracesDataSchema,\n response: {\n 200: exportTracesServiceResponseSchema,\n },\n },\n handler: async (req, res) => {\n const { rejectedSpans, errorMessage } =\n await opts.writeTracesDatasource.writeTraces(req.body);\n\n res.send({\n partialSuccess: {\n rejectedSpans,\n errorMessage,\n },\n });\n },\n });\n};\n","import { z } from \"zod/v4\";\nimport type { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\";\nimport { otlpZod, type datasource } from \"@kopai/core\";\n\n// https://github.com/open-telemetry/opentelemetry-specification/blob/49845849d2d8df07059f82033f39e96c561927cf/oteps/0122-otlp-http-json.md#response\nconst exportLogsServiceResponseSchema = z.object({\n partialSuccess: z\n .object({\n rejectedLogRecords: z.string().optional(),\n errorMessage: z.string().optional(),\n })\n .optional(),\n});\n\nexport const logsRoute: FastifyPluginAsyncZod<{\n writeLogsDatasource: datasource.WriteLogsDatasource;\n}> = async function (fastify, opts) {\n fastify.route({\n method: \"POST\",\n url: \"/v1/logs\",\n schema: {\n body: otlpZod.logsDataSchema,\n response: {\n 200: exportLogsServiceResponseSchema,\n },\n },\n handler: async (req, res) => {\n const { rejectedLogRecords, errorMessage } =\n await opts.writeLogsDatasource.writeLogs(req.body);\n\n res.send({\n partialSuccess: {\n rejectedLogRecords,\n errorMessage,\n },\n });\n },\n });\n};\n","// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlphttp-response\n\nimport { z } from \"zod/v4\";\nexport const grpcStatusCode = {\n OK: 0,\n CANCELLED: 1,\n UNKNOWN: 2,\n INVALID_ARGUMENT: 3,\n DEADLINE_EXCEEDED: 4,\n NOT_FOUND: 5,\n ALREADY_EXISTS: 6,\n PERMISSION_DENIED: 7,\n RESOURCE_EXHAUSTED: 8,\n FAILED_PRECONDITION: 9,\n ABORTED: 10,\n OUT_OF_RANGE: 11,\n UNIMPLEMENTED: 12,\n INTERNAL: 13,\n UNAVAILABLE: 14,\n DATA_LOSS: 15,\n UNAUTHENTICATED: 16,\n} as const;\n\nexport const grpcStatusSchema = z.object({\n message: z.string(),\n details: z.array(z.unknown()).optional(),\n});\n\nexport const grpcStatusCodeSchema = z.number().int().min(0).max(16);\n\nexport type GrpcStatusCode =\n (typeof grpcStatusCode)[keyof typeof grpcStatusCode];\n\n// Error response body for HTTP 4xx/5xx\nexport const otlpErrorResponseSchema = z.object({\n code: grpcStatusCodeSchema,\n // The Status.message field SHOULD contain a developer-facing error message as defined in Status message schema.\n message: z.string(),\n // The server MAY include Status.details field with additional details. Read below about what this field can contain in each specific failure case.\n details: z.array(z.unknown()).optional(),\n});\n\nexport type ErrorResponse = z.infer<typeof otlpErrorResponseSchema>;\n\n// Single field violation\nexport const fieldViolationSchema = z.object({\n field: z.string(), // path like \"resourceSpans[0].spans[2].traceId\"\n description: z.string(), // human-readable explanation\n reason: z.string().optional(), // e.g. \"INVALID_TRACE_ID\"\n});\n\nexport type FieldViolation = z.infer<typeof fieldViolationSchema>;\n\n// BadRequest detail\nexport const badRequestSchema = z.object({\n \"@type\": z.literal(\"type.googleapis.com/google.rpc.BadRequest\").optional(),\n fieldViolations: z.array(fieldViolationSchema),\n});\n\n/*\n * Full error response for HTTP 400\n *\n * example:\n *\n * {\n * \"code\": 3,\n * \"message\": \"invalid trace data\",\n * \"details\": [\n * {\n * \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n * \"fieldViolations\": [\n * {\n * \"field\": \"resourceSpans[0].scopeSpans[0].spans[0].traceId\",\n * \"description\": \"traceId must be 32 hex characters\",\n * \"reason\": \"INVALID_TRACE_ID\"\n * },\n * {\n * \"field\": \"resourceSpans[0].scopeSpans[0].spans[1].startTimeUnixNano\",\n * \"description\": \"startTimeUnixNano must be a positive integer\",\n * \"reason\": \"INVALID_TIMESTAMP\"\n * }\n * ]\n * }\n * ]\n * }\n */\nexport const otlpBadRequestErrorResponseSchema = z.object({\n code: z.number().int(),\n message: z.string(),\n details: z.array(badRequestSchema).optional(),\n});\n\nexport type OtlpBadRequestErrorResponse = z.infer<\n typeof otlpBadRequestErrorResponseSchema\n>;\n","import type { FastifySchemaValidationError } from \"fastify\";\nimport type { FieldViolation } from \"./otlp-schemas.js\";\n\nexport function extractLeafError(errors: unknown[][]): {\n path: (string | number)[];\n message: string;\n expected?: string;\n} {\n for (const branch of errors) {\n if (!Array.isArray(branch) || !branch.length) continue;\n const e = branch[0] as {\n code?: string;\n path?: (string | number)[];\n message?: string;\n expected?: string;\n errors?: unknown[][];\n };\n if (e?.code === \"invalid_union\" && e.errors) {\n const deeper = extractLeafError(e.errors);\n return {\n path: [...(e.path || []), ...deeper.path],\n message: deeper.message,\n expected: deeper.expected,\n };\n }\n return {\n path: e?.path || [],\n message: e?.message || \"Validation failed\",\n expected: e?.expected,\n };\n }\n return { path: [], message: \"Validation failed\" };\n}\n\nexport function toFieldViolation(\n error: FastifySchemaValidationError\n): FieldViolation {\n let field = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\") || \"\";\n let description = error.message ?? \"Validation failed\";\n\n if (error.keyword === \"invalid_union\" && error.params?.errors) {\n const leaf = extractLeafError(error.params.errors as unknown[][]);\n const leafPath = leaf.path\n .map((p) => (typeof p === \"number\" ? `[${p}]` : `.${p}`))\n .join(\"\")\n .replace(/^\\./, \"\");\n if (leafPath) {\n const sep = leafPath.startsWith(\"[\") ? \"\" : \".\";\n field = field ? `${field}${sep}${leafPath}` : leafPath;\n }\n description = leaf.expected ? `expected ${leaf.expected}` : leaf.message;\n }\n\n return { field: field || \"unknown\", description, reason: error.keyword };\n}\n","import type { GrpcStatusCode } from \"./otlp-schemas.js\";\n\nexport class CollectorError extends Error {\n constructor(\n message: string,\n public code: GrpcStatusCode\n ) {\n super(message);\n Object.setPrototypeOf(this, CollectorError.prototype);\n }\n}\n","import {\n type FastifyError,\n type FastifyReply,\n type FastifyRequest,\n} from \"fastify\";\n\nimport {\n type OtlpBadRequestErrorResponse,\n grpcStatusCode,\n type ErrorResponse,\n} from \"./otlp-schemas.js\";\nimport { toFieldViolation } from \"./field-violation.js\";\n\nimport { CollectorError } from \"./errors.js\";\n\nexport function collectorErrorHandler(\n error: FastifyError | Error | string,\n request: FastifyRequest,\n reply: FastifyReply\n) {\n if (isValidationError(error)) {\n return reply.status(400).send({\n code: grpcStatusCode.INVALID_ARGUMENT,\n message: \"Invalid data\",\n details: [\n {\n \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n fieldViolations: error.validation.map(toFieldViolation),\n },\n ],\n } satisfies OtlpBadRequestErrorResponse);\n }\n\n request.log.error(error);\n if (error instanceof CollectorError) {\n return reply.status(500).send({\n code: error.code,\n message: error.message,\n } satisfies ErrorResponse);\n }\n\n reply.status(500).send({ error: \"Internal Server Error\" });\n}\n\nfunction isFastifyError(error: unknown): error is FastifyError {\n return (\n error instanceof Error &&\n \"code\" in error &&\n typeof (error as FastifyError).code === \"string\"\n );\n}\n\nfunction isValidationError(\n error: unknown\n): error is FastifyError & Required<Pick<FastifyError, \"validation\">> {\n return (\n isFastifyError(error) &&\n \"validation\" in error &&\n Array.isArray((error as FastifyError).validation)\n );\n}\n","import type { FastifyPluginAsyncZod } from \"fastify-type-provider-zod\";\nimport type { datasource } from \"@kopai/core\";\nimport {\n serializerCompiler,\n validatorCompiler,\n} from \"fastify-type-provider-zod\";\n\nimport { metricsRoute } from \"./routes/metrics.js\";\nimport { tracesRoute } from \"./routes/traces.js\";\nimport { logsRoute } from \"./routes/logs.js\";\nimport { collectorErrorHandler } from \"./routes/error-handler.js\";\n\nexport const collectorRoutes: FastifyPluginAsyncZod<{\n telemetryDatasource: datasource.WriteTelemetryDatasource;\n}> = async function (fastify, opts) {\n fastify.setValidatorCompiler(validatorCompiler);\n fastify.setSerializerCompiler(serializerCompiler);\n fastify.setErrorHandler(collectorErrorHandler);\n\n fastify.register(metricsRoute, {\n writeMetricsDatasource: opts.telemetryDatasource,\n });\n\n fastify.register(tracesRoute, {\n writeTracesDatasource: opts.telemetryDatasource,\n });\n\n fastify.register(logsRoute, {\n writeLogsDatasource: opts.telemetryDatasource,\n });\n};\n"],"mappings":";;;;;;AAKA,MAAM,qCAAqC,EAAE,OAAO,EAClD,gBAAgB,EACb,OAAO;CACN,oBAAoB,EAAE,QAAQ,CAAC,UAAU;CACzC,cAAc,EAAE,QAAQ,CAAC,UAAU;CACpC,CAAC,CACD,UAAU,EACd,CAAC;AAEF,MAAa,eAER,eAAgB,SAAS,MAAM;AAClC,SA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;GACxD,CACF;EACF,CAAuC;AAG1C,SAAQ,IAAI,MAAM,MAAM;AACxB,KAAI,iBAAiB,eACnB,QAAO,MAAM,OAAO,IAAI,CAAC,KAAK;EAC5B,MAAM,MAAM;EACZ,SAAS,MAAM;EAChB,CAAyB;AAG5B,OAAM,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,yBAAyB,CAAC;;AAG5D,SAAS,eAAe,OAAuC;AAC7D,QACE,iBAAiB,SACjB,UAAU,SACV,OAAQ,MAAuB,SAAS;;AAI5C,SAAS,kBACP,OACoE;AACpE,QACE,eAAe,MAAM,IACrB,gBAAgB,SAChB,MAAM,QAAS,MAAuB,WAAW;;;;;AC9CrD,MAAa,kBAER,eAAgB,SAAS,MAAM;AAClC,SAAQ,qBAAqB,kBAAkB;AAC/C,SAAQ,sBAAsB,mBAAmB;AACjD,SAAQ,gBAAgB,sBAAsB;AAE9C,SAAQ,SAAS,cAAc,EAC7B,wBAAwB,KAAK,qBAC9B,CAAC;AAEF,SAAQ,SAAS,aAAa,EAC5B,uBAAuB,KAAK,qBAC7B,CAAC;AAEF,SAAQ,SAAS,WAAW,EAC1B,qBAAqB,KAAK,qBAC3B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kopai/collector",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"author": "Vladimir Adamic",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kopai-app/kopai-mono.git",
|
|
9
|
+
"directory": "packages/collector"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kopai-app/kopai-mono/tree/main/packages/collector#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kopai-app/kopai-mono/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"fastify": "^5.0.0",
|
|
31
|
+
"zod": "^4.0.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"fastify-type-provider-zod": "^6.1.0",
|
|
35
|
+
"@kopai/core": "0.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@opentelemetry/api": "^1.9.0",
|
|
39
|
+
"@opentelemetry/api-logs": "^0.211.0",
|
|
40
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.211.0",
|
|
41
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.211.0",
|
|
42
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.211.0",
|
|
43
|
+
"@opentelemetry/resources": "^2.5.0",
|
|
44
|
+
"@opentelemetry/sdk-logs": "^0.211.0",
|
|
45
|
+
"@opentelemetry/sdk-metrics": "^2.5.0",
|
|
46
|
+
"@opentelemetry/sdk-trace-base": "^2.5.0",
|
|
47
|
+
"@opentelemetry/sdk-trace-node": "^2.5.0",
|
|
48
|
+
"@opentelemetry/semantic-conventions": "^1.39.0",
|
|
49
|
+
"tsdown": "^0.20.1",
|
|
50
|
+
"@kopai/tsconfig": "0.0.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsdown",
|
|
54
|
+
"dev": "tsdown --watch",
|
|
55
|
+
"lint": "eslint src",
|
|
56
|
+
"type-check": "tsc --noEmit",
|
|
57
|
+
"test": "vitest run"
|
|
58
|
+
}
|
|
59
|
+
}
|