Skip to content

Commit 66de9c0

Browse files
art049adriencaccia
authored andcommitted
feat: integrate CodSpeed Mongo Tracer
1 parent 894136d commit 66de9c0

File tree

11 files changed

+404
-14
lines changed

11 files changed

+404
-14
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.rollup.cache
33
dist
4+
generated

packages/benchmark.js-plugin/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Measurement,
3+
mongoMeasurement,
34
optimizeFunction,
45
optimizeFunctionSync,
56
setupCore,
@@ -192,18 +193,22 @@ async function runBenchmarks({
192193

193194
if (isAsync) {
194195
await optimizeFunction(benchPayload);
196+
await mongoMeasurement.start(uri);
195197
await (async function __codspeed_root_frame__() {
196198
Measurement.startInstrumentation();
197199
await benchPayload();
198200
Measurement.stopInstrumentation(uri);
199201
})();
202+
await mongoMeasurement.stop(uri);
200203
} else {
201204
optimizeFunctionSync(benchPayload);
205+
await mongoMeasurement.start(uri);
202206
(function __codspeed_root_frame__() {
203207
Measurement.startInstrumentation();
204208
benchPayload();
205209
Measurement.stopInstrumentation(uri);
206210
})();
211+
await mongoMeasurement.stop(uri);
207212
}
208213

209214
if (typeof bench.options.teardown === "function") {

packages/core/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
build
2-
prebuilds
2+
prebuilds
3+
generated

packages/core/moon.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tasks:
22
clean:
33
args:
44
- build
5+
- generated/openapi
56
build:
67
deps:
78
- "build-native-addon"
@@ -14,3 +15,10 @@ tasks:
1415
- "binding.gyp"
1516
outputs:
1617
- "prebuilds"
18+
19+
build-tracer-client:
20+
inputs:
21+
- "./tracer.spec.json"
22+
outputs:
23+
- "src/generated/openapi"
24+
command: openapi --client axios --input ./tracer.spec.json --name MongoTracer --output ./src/generated/openapi

packages/core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
"@types/find-up": "^4.0.0",
2424
"node-addon-api": "^5.1.0",
2525
"node-gyp": "^9.3.1",
26+
"openapi-typescript-codegen": "^0.23.0",
2627
"prebuildify": "^5.0.1"
2728
},
2829
"dependencies": {
30+
"axios": "^1.4.0",
2931
"find-up": "^6.3.0",
3032
"node-gyp-build": "^4.6.0"
3133
}

packages/core/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { checkV8Flags } from "./introspection";
2+
import { MongoMeasurement } from "./mongoMeasurement";
23
import native_core from "./native_core";
34
import { initOptimization } from "./optimization";
45

@@ -8,13 +9,16 @@ const linuxPerf = new native_core.LinuxPerf();
89

910
export const isBound = native_core.isBound;
1011

12+
export let mongoMeasurement: MongoMeasurement;
13+
1114
export const setupCore = () => {
1215
initOptimization();
1316
native_core.Measurement.stopInstrumentation(
1417
`Metadata: codspeed-node ${__VERSION__}`
1518
);
1619
linuxPerf.start();
1720
checkV8Flags();
21+
mongoMeasurement = new MongoMeasurement();
1822
};
1923

2024
export const teardownCore = () => {

packages/core/src/mongoMeasurement.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { MongoTracer } from "./generated/openapi";
2+
3+
export class MongoMeasurement {
4+
private tracerClient: MongoTracer | undefined;
5+
6+
constructor() {
7+
const serverUrl = process.env.CODSPEED_MONGO_INSTR_SERVER_ADDRESS;
8+
// TODO
9+
const mongoUriEnvName = process.env.CODSPEED_MONGO_INSTR_URI_ENV_NAME;
10+
if (mongoUriEnvName === undefined) {
11+
throw new Error("CODSPEED_MONGO_INSTR_URI_ENV_NAME is not defined");
12+
}
13+
const mongoUri = process.env[mongoUriEnvName];
14+
if (mongoUri === undefined) {
15+
throw new Error(`Environment variable ${mongoUriEnvName} is not defined`);
16+
}
17+
process.env[mongoUriEnvName] =
18+
"mongodb://localhost:27018?directConnection=true";
19+
20+
if (serverUrl !== undefined) {
21+
this.tracerClient = new MongoTracer({
22+
BASE: serverUrl,
23+
});
24+
}
25+
}
26+
27+
public async start(uri: string) {
28+
if (this.tracerClient !== undefined) {
29+
await this.tracerClient.instrumentation.start({
30+
uri,
31+
});
32+
}
33+
}
34+
35+
public async stop(uri: string) {
36+
if (this.tracerClient !== undefined) {
37+
await this.tracerClient.instrumentation.stop({
38+
uri,
39+
});
40+
}
41+
}
42+
}

packages/core/tracer.spec.json

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "CodSpeed MongoDB Tracer",
5+
"description": "Instrumentation API for CodSpeed Tracer",
6+
"version": "0.1.0"
7+
},
8+
"paths": {
9+
"/benchmark/start": {
10+
"post": {
11+
"tags": ["instrumentation"],
12+
"operationId": "start",
13+
"requestBody": {
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/InstrumentationRequestBody"
18+
}
19+
}
20+
},
21+
"required": true
22+
},
23+
"responses": {
24+
"200": {
25+
"description": "successful operation",
26+
"content": {
27+
"application/json": {
28+
"schema": {
29+
"$ref": "#/components/schemas/InstrumentationStatus"
30+
}
31+
}
32+
}
33+
},
34+
"4XX": {
35+
"$ref": "#/components/responses/Error"
36+
},
37+
"5XX": {
38+
"$ref": "#/components/responses/Error"
39+
}
40+
}
41+
}
42+
},
43+
"/benchmark/stop": {
44+
"post": {
45+
"tags": ["instrumentation"],
46+
"operationId": "stop",
47+
"requestBody": {
48+
"content": {
49+
"application/json": {
50+
"schema": {
51+
"$ref": "#/components/schemas/InstrumentationRequestBody"
52+
}
53+
}
54+
},
55+
"required": true
56+
},
57+
"responses": {
58+
"200": {
59+
"description": "successful operation",
60+
"content": {
61+
"application/json": {
62+
"schema": {
63+
"$ref": "#/components/schemas/InstrumentationStatus"
64+
}
65+
}
66+
}
67+
},
68+
"4XX": {
69+
"$ref": "#/components/responses/Error"
70+
},
71+
"5XX": {
72+
"$ref": "#/components/responses/Error"
73+
}
74+
}
75+
}
76+
},
77+
"/status": {
78+
"get": {
79+
"tags": ["instrumentation"],
80+
"operationId": "status",
81+
"responses": {
82+
"200": {
83+
"description": "successful operation",
84+
"content": {
85+
"application/json": {
86+
"schema": {
87+
"$ref": "#/components/schemas/InstrumentationStatus"
88+
}
89+
}
90+
}
91+
},
92+
"4XX": {
93+
"$ref": "#/components/responses/Error"
94+
},
95+
"5XX": {
96+
"$ref": "#/components/responses/Error"
97+
}
98+
}
99+
}
100+
},
101+
"/terminate": {
102+
"post": {
103+
"tags": ["instrumentation"],
104+
"operationId": "terminate",
105+
"responses": {
106+
"200": {
107+
"description": "successful operation",
108+
"content": {
109+
"application/json": {
110+
"schema": {
111+
"$ref": "#/components/schemas/AggregatorStore"
112+
}
113+
}
114+
}
115+
},
116+
"4XX": {
117+
"$ref": "#/components/responses/Error"
118+
},
119+
"5XX": {
120+
"$ref": "#/components/responses/Error"
121+
}
122+
}
123+
}
124+
}
125+
},
126+
"components": {
127+
"responses": {
128+
"Error": {
129+
"description": "Error",
130+
"content": {
131+
"application/json": {
132+
"schema": {
133+
"$ref": "#/components/schemas/Error"
134+
}
135+
}
136+
}
137+
}
138+
},
139+
"schemas": {
140+
"AggregatorMetadata": {
141+
"type": "object",
142+
"properties": {
143+
"name": {
144+
"type": "string"
145+
},
146+
"version": {
147+
"type": "string"
148+
}
149+
},
150+
"required": ["name", "version"]
151+
},
152+
"AggregatorStore": {
153+
"type": "object",
154+
"properties": {
155+
"metadata": {
156+
"nullable": true,
157+
"allOf": [
158+
{
159+
"$ref": "#/components/schemas/AggregatorMetadata"
160+
}
161+
]
162+
},
163+
"queries": {
164+
"type": "object",
165+
"additionalProperties": {
166+
"type": "array",
167+
"items": {
168+
"$ref": "#/components/schemas/MongoQuery"
169+
}
170+
}
171+
}
172+
},
173+
"required": ["queries"]
174+
},
175+
"Document": {
176+
"type": "object"
177+
},
178+
"Error": {
179+
"description": "Error information from a response.",
180+
"type": "object",
181+
"properties": {
182+
"error_code": {
183+
"type": "string"
184+
},
185+
"message": {
186+
"type": "string"
187+
},
188+
"request_id": {
189+
"type": "string"
190+
}
191+
},
192+
"required": ["message", "request_id"]
193+
},
194+
"InstrumentationRequestBody": {
195+
"type": "object",
196+
"properties": {
197+
"uri": {
198+
"type": "string"
199+
}
200+
},
201+
"required": ["uri"]
202+
},
203+
"InstrumentationStatus": {
204+
"type": "object",
205+
"properties": {
206+
"currentUri": {
207+
"nullable": true,
208+
"type": "string"
209+
}
210+
}
211+
},
212+
"MongoQuery": {
213+
"type": "object",
214+
"properties": {
215+
"collection": {
216+
"type": "string"
217+
},
218+
"database": {
219+
"type": "string"
220+
},
221+
"explanation": {
222+
"nullable": true,
223+
"allOf": [
224+
{
225+
"$ref": "#/components/schemas/Document"
226+
}
227+
]
228+
},
229+
"op": {
230+
"type": "string"
231+
},
232+
"query_documents": {
233+
"type": "array",
234+
"items": {
235+
"$ref": "#/components/schemas/Document"
236+
}
237+
},
238+
"response_documents": {
239+
"type": "array",
240+
"items": {
241+
"$ref": "#/components/schemas/Document"
242+
}
243+
}
244+
},
245+
"required": [
246+
"collection",
247+
"database",
248+
"op",
249+
"query_documents",
250+
"response_documents"
251+
]
252+
}
253+
}
254+
},
255+
"tags": [
256+
{
257+
"name": "instrumentation"
258+
}
259+
]
260+
}

0 commit comments

Comments
 (0)