Effect Trace ID Inquiry

so in t3.chat, I presume effect is being used and some sort of external OTEL is used for fetching logs with a trace ID. I am attempting something similar in a NestJS app, just that I want to return a response to the user with the traceId so when the user get's the feedback, I can also use the traceId to find the detailed logs. here's a sample UserService. I would appreciate any help
// User Service
import { Data, Effect, Cause } from 'effect';
import { Injectable, HttpStatus } from '@nestjs/common';
import { CustomHttpException } from './helpers/exception';

class UserServiceError extends Data.TaggedError('UserServiceError')<{
traceId?: string;
message?: string;
status?: HttpStatus;
}> {}

@Injectable()
export class UserService {
constructor() {}
getRandomUser = Effect.gen(function* () {
const random = () => Math.floor(Math.random() * 2);
const user =
random() === 0 ? null : { id: 'place-holder', name: 'John Doe' };

if (!user) {
return yield* Effect.fail(
new UserServiceError({
message: 'User not found',
traceId: "undefined... I don't know how to get it",
status: HttpStatus.NOT_FOUND,
}),
);
}

return user;
}).pipe(
Effect.tapError((err) => Effect.logError(err, Cause.fail(err))),
Effect.catchTag('UserServiceError', (err) =>
Effect.fail(
new CustomHttpException(
{
message: err.message ?? 'User not found',
traceId: err.traceId ?? "undefined... I don't know how to get it",
},
err.status ?? HttpStatus.NOT_FOUND,
),
),
),
Effect.withSpan('getRandomUser'),
);
}
// User Service
import { Data, Effect, Cause } from 'effect';
import { Injectable, HttpStatus } from '@nestjs/common';
import { CustomHttpException } from './helpers/exception';

class UserServiceError extends Data.TaggedError('UserServiceError')<{
traceId?: string;
message?: string;
status?: HttpStatus;
}> {}

@Injectable()
export class UserService {
constructor() {}
getRandomUser = Effect.gen(function* () {
const random = () => Math.floor(Math.random() * 2);
const user =
random() === 0 ? null : { id: 'place-holder', name: 'John Doe' };

if (!user) {
return yield* Effect.fail(
new UserServiceError({
message: 'User not found',
traceId: "undefined... I don't know how to get it",
status: HttpStatus.NOT_FOUND,
}),
);
}

return user;
}).pipe(
Effect.tapError((err) => Effect.logError(err, Cause.fail(err))),
Effect.catchTag('UserServiceError', (err) =>
Effect.fail(
new CustomHttpException(
{
message: err.message ?? 'User not found',
traceId: err.traceId ?? "undefined... I don't know how to get it",
},
err.status ?? HttpStatus.NOT_FOUND,
),
),
),
Effect.withSpan('getRandomUser'),
);
}
Solution:
solved with this implementation, thank you! ```ts import { Injectable } from '@nestjs/common'; import { Tracer } from '@effect/opentelemetry';...
Jump to solution
4 Replies
prudent
prudentOP2w ago
@Dom @Ben Davis @markr I would appreciate if you could help me out here, sorry for the ping
markr
markr2w ago
Effect Documentation
Tracing in Effect
Explore tracing in distributed systems to track request lifecycles across services using spans and traces for debugging and performance optimization.
prudent
prudentOP2w ago
@markr I have this configured, problem is accessing the traceId when throwing the exception
Solution
prudent
prudent2w ago
solved with this implementation, thank you!
import { Injectable } from '@nestjs/common';
import { Tracer } from '@effect/opentelemetry';
import { Effect, Cause, Config } from 'effect';

@Injectable()
export class AppService {
getHello() {
return Effect.gen(function* () {
const span = yield* Tracer.currentOtelSpan;
return yield* Effect.succeed({
message: 'Hello',
data: {
uptime: process.uptime(),
traceId: span.spanContext().traceId ?? 'no-trace-id',
version: yield* Config.string('npm_package_version'),
environment: yield* Config.string('NODE_ENV'),
},
});
}).pipe(
Effect.withSpan('getHello'),
Effect.tapError((err) => Effect.logError(err, Cause.fail(err))),
);
}
}
import { Injectable } from '@nestjs/common';
import { Tracer } from '@effect/opentelemetry';
import { Effect, Cause, Config } from 'effect';

@Injectable()
export class AppService {
getHello() {
return Effect.gen(function* () {
const span = yield* Tracer.currentOtelSpan;
return yield* Effect.succeed({
message: 'Hello',
data: {
uptime: process.uptime(),
traceId: span.spanContext().traceId ?? 'no-trace-id',
version: yield* Config.string('npm_package_version'),
environment: yield* Config.string('NODE_ENV'),
},
});
}).pipe(
Effect.withSpan('getHello'),
Effect.tapError((err) => Effect.logError(err, Cause.fail(err))),
);
}
}

Did you find this page helpful?