Effect-TS / website

Source code for Effect's documentation website
https://www.effect.website
105 stars 84 forks source link

From Discord: Challenges in Building a Telemetry Layer from an Effect #841

Open effect-bot opened 1 month ago

effect-bot commented 1 month ago

Summary

Summary

User's Issue:

Debugging Steps:

  1. Initial Setup:

    • The user created a telemetry layer using Effect.try and cached it using an Option.
    • They provided this layer to effects using Effect.provide.
  2. Observed Behavior:

    • When the telemetry layer is created directly (without effects), it works fine.
    • When created from an effect, the spans do not show up.
  3. Discussion Points:

    • The user noted that the telemetry layer gets created once and reused when using their caching mechanism.
    • They observed that without caching, the layer gets created on every request, which is not desired.
  4. Suggestions:

    • datner_ suggested using ManagedRuntime to ensure the layer is constructed only once and reused across multiple requests.
    • The user implemented ManagedRuntime and found that it resolves the caching issue but the telemetry layer still does not work.
  5. Final Insight:

    • datner_ pointed out that instead of just merging the tracing layer, the entire app should be provided with the tracingLive layer.

Key Takeaways

  1. Layer Memoization:

    • Layers in Effect are memoized, meaning they should only construct once even if composed multiple times.
    • Using ManagedRuntime can ensure that layers are constructed once and reused across multiple requests in a long-lived process.
  2. Providing Layers:

    • When constructing the application layer, ensure that the entire app is provided with the necessary layers (e.g., tracingLive).
  3. Effect Execution:

    • Avoid calling Effect.runPromise repeatedly for short-lived executions. Instead, use a managed runtime to maintain the state of layers.
  4. Debugging Layers:

    • If a layer works when created directly but not when created from an effect, it might indicate an issue with how the layer is being provided or constructed.
  5. Documentation and DX:

    • The experience highlighted potential areas for improving documentation and developer experience, especially around providing layers and using managed runtimes.

Discord thread

https://discord.com/channels/795981131316985866/1285261463610720297

datner commented 1 month ago

TL;DR

'providing' something might need a bit more clarification from a mental model standpoint