Node.js
Distributed
Add distributed tracing and event sourcing.
The distributed tracing layer
In the third lesson we add the distributed tracing layer. OpenTelemetry is the open standard for tracking a request's path across services with a single trace ID. In this project, instead of wiring up the real OpenTelemetry framework, we write a distributedLayer function that represents it.
The distributedLayer function returns { tracing: 'OpenTelemetry', status: 'hazır' }. The output gets a new line OpenTelemetry: hazır. The starting code is appended on top of the setupProject and nativeAddon solutions. This cumulative growth guarantees the correct order when integrationReport later calls all layers. A real OpenTelemetry integration uses the @opentelemetry/node package and HTTP header propagation; here modeling is enough.
The tracing context
A distributed tracing context works with trace ID and span ID. In this project distributedLayer only carries the tracing framework's name; real context propagation comes later. The call pattern still matches the previous layers.
Example: console.log(distributedLayer().tracing + ': ' + distributedLayer().status). This produces the line OpenTelemetry: hazır. The previous lines (ExpertProject, 1.0, N-API: hazır) stay; the new line appends after them. Thanks to cumulative growth, each lesson's output continues the previous one. This ensures that when integrationReport later gathers all layers into one string, every piece lands in the right order.
Exercise Add the distributed tracing layer
Take the previous setupProject and nativeAddon solutions. Add function distributedLayer(); return { tracing: 'OpenTelemetry', status: 'hazır' }. Print console.log(distributedLayer().tracing + ': ' + distributedLayer().status).
Show example solution
function setupProject() {
return { name: 'ExpertProject', version: '1.0' };
}
console.log(setupProject().name);
console.log(setupProject().version);
function nativeAddon() {
return { api: 'N-API', status: 'hazır' };
}
console.log(nativeAddon().api + ': ' + nativeAddon().status);
function distributedLayer() {
return { tracing: 'OpenTelemetry', status: 'hazır' };
}
console.log(distributedLayer().tracing + ': ' + distributedLayer().status);
Test yourself
-
Match each piece to its role
- OpenTelemetry
- Tracing standard
- distributedLayer
- Layer function
- tracing
- Tracing name
- trace ID
- Ties the request
-
What does the tracing id do in distributed tracing?
function distributedLayer() { return {tracing: 'OpenTelemetry', status: 'hazır'}; }
- A database password
- Marks the request path
- A user session
- Only the cache
Show answer
Marks the request path — The tracing id marks the request path; it ties all services together.
-
Complete the tracing field
return { tracing: '____', status: 'hazır' };
Show answer
return { tracing: 'OpenTelemetry', status: 'hazır' };
Next lesson: Performance