Closed jinja12 closed 2 months ago
The project link: "https://github.com/jinja12/webperf"
Ok so, it has managed to start working, following is the modified code:
const { execSync } = require("child_process");
const { trace } = require("@opentelemetry/api");
const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const fs = require('fs');
class CustomConsoleSpanExporter extends ConsoleSpanExporter {
export(spans, resultCallback) {
for (const span of spans) {
const startTime = span.startTime[0] + span.startTime[1] * 1e-9; // Convert to seconds
const duration = span.duration[0]; // Convert to seconds
const logMessage = `Step Name: ${span.name}, Start Time: ${startTime.toFixed(6)} secs, Duration: ${duration.toFixed(6)} secs\n`;
console.log(logMessage);
fs.appendFileSync('traces.txt', logMessage);
}
resultCallback({ code: 0 });
}
}
const provider = new BasicTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new CustomConsoleSpanExporter()));
provider.register();
const name = 'webperf';
const version = '0.1.0';
const tracer = trace.getTracer(name, version);
async function buildPackage(packagePath) {
const span = tracer.startSpan(`Building package at ${packagePath}`);
try {
execSync(`cd ${packagePath} && npm run build`, { stdio: "inherit" });
} finally {
span.end();
}
}
async function runRandomScript() {
const span = tracer.startSpan("Running random script");
try {
execSync(`node randomScript.js`, { stdio: "inherit" });
} finally {
span.end();
}
}
async function main() {
fs.writeFileSync('traces.txt', ''); // Clear file content before starting
const packages = ["./packages_test/package1", "./packages_test/package2"];
for (const packagePath of packages) {
await buildPackage(packagePath);
}
await runRandomScript();
}
main();
Is there some solution where instead of adding a span around each build manually and instrumenting it, I can build a wrapper over this, i.e. the prebuild script will only strictly contain the functions to build packages, and I am able to build some wrapper code: instrumentBuild.js
where I can track the build times and other relevant data for each build that happens in the prebuild script ? Tagging @dyladan @pichlermarc @svrnm @blumamir @legendecas @hectorhdzg @trentm
Is there some solution where instead of adding a span around each build manually and instrumenting it, I can build a wrapper over this, i.e. the prebuild script will only strictly contain the functions to build packages, and I am able to build some wrapper code:
instrumentBuild.js
where I can track the build times and other relevant data for each build that happens in the prebuild script ?
You could perhaps write a wrapper for execSync
that creates a span around the exec.
Or you could consider writing an instrumentation for Node.js' child_process
module. However, that would of course be much more work.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.
This issue was closed because it has been stale for 14 days with no activity.
I have a basic NextJS application, inside which there are some custom build scripts which run as prebuild alongside the default
next build
package.json:The prebuild.js file:
So basically
packages_test/package1
andpackages_test/package2
are my custom packages which I build inside this script. Inside theirpackage.json
file: I just initiate asleep
of some short duration around 2-5s to simulate build time inside scripts build.UseCase: Now my aim is to get relevant Telemetry data for my build process and get an idea about the build times and other data for all builds which happen including the prebuilds. I enclosed the build process in my prebuilds script inside a span and was hoping that after
span.end()
, the trace details are logged automatically on my console. Nothing of that sort is logged, to test if my trace was even being initialized or not, I added console logs for my span context. Following is the log:I am not sure why is this not working as expected (trace details not logged after span end), any insights into this would be highly appreciated. Also, it would be great if one could suggest alternative better approaches to solve the use case which I am trying to solve. Thanks!