Automating Span Naming in Effect Functions

I love the convenience of the Effect.fn function for defining functions within my services. Since I follow the ${serviceName}:${functionName} naming convention for spans, it was inefficient to define the name twice—once for the function name and again for the span. This redundancy created maintenance issues, such as forgetting to update the span name when renaming a function.

I set out to eliminate this redundancy by automatically deriving the span name from the object key that defines each function.

This is the solution I came up with:

const serviceName = "generous.core.services.NotificationService";

export class NotificationService extends Effect.Service<NotificationService>()(
  serviceName,
  {
    effect: defineServiceFunctions(serviceName, {
      submit: function* (notification: Notification) {
        // ...
      },
      getAll: function* (dto: {
        tenant: Tenant;
      }) {
        // ...
      },
      markAsRead: function* (dto: {
        tenant: Tenant;
      }) {
        // ...
      },
    }),
  },
) { }


I'm super curious to hear from you what you think about this approach 🙂
Was this page helpful?