Skip to content

Instrument Cloudflare Service Bindings #16896

Open
@timfish

Description

@timfish

Description

Cloudflare exposes lots of it's services via bindings which get attached to the Env that gets passed to your handlers:
https://developers.cloudflare.com/workers/runtime-apis/bindings/

This includes:

Since we wrap many of the functions that get passed the Env, we should be able to add wrappers around the individual service bindings to instrument them. For example, it would be great to be able to include spans for calls to R2 object storage.

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const key = url.pathname.slice(1);

    switch (request.method) {
      case "PUT":
        await env.MY_BUCKET.put(key, request.body);
        return new Response(`Put ${key} successfully!`);

      default:
        return new Response(`${request.method} is not allowed.`, {
          status: 405,
          headers: {
            Allow: "PUT",
          },
        });
    }
  },
};

I'm not sure if env in enumerable and even if it is, we probably won't know for sure what kind of service each property exposes. This feature might need bundler support so we can read the wrangler configuration and include some details in the bundle so we know what kind of service each env property exposes and how to wrap it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions