floydspace / effect-aws

🚰 Effectful AWS
https://floydspace.github.io/effect-aws/
MIT License
27 stars 2 forks source link

Use X-Ray #31

Closed godu closed 7 months ago

godu commented 7 months ago

https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-awssdkclients.html

I'm not very familiar with layer management. What is the easiest way to setup X-Ray with @effect-aws/* ?

floydspace commented 7 months ago

Hey @godu

try this:

import { S3Client } from "@aws-sdk/client-s3";
import { BaseS3ServiceLayer, S3ClientInstance } from "@effect-aws/client-s3";
import { captureAWSv3Client } from "aws-xray-sdk";

// your program (no changes are here)
const program = Effect.flatMap(S3Service, (s3) => s3.headObject(args));

// S3 client instance layer which constructs the clinet wrapped in xray
const S3ClientInstanceLayer = Layer.succeed(
  S3ClientInstance,
  captureAWSv3Client(new S3Client({ region: "eu-central-1" })),
);
// the custom layer based on BaseS3ServiceLayer which requires S3ClientInstance
const CustomS3ServiceLayer = BaseS3ServiceLayer.pipe(
  Layer.provide(S3ClientInstanceLayer),
);

const result = await pipe(
  program,
  Effect.provide(CustomS3ServiceLayer), // instead of DefaultS3ServiceLayer you use custom one
  Effect.runPromise,
);
floydspace commented 7 months ago

but better if the captureAWSv3Client is wrapped in effect, bc it also may fail:

const S3ClientInstanceLayer = Layer.effect(
  S3ClientInstance,
  Effect.try(() =>
    captureAWSv3Client(new S3Client({ region: "eu-central-1" })),
  ),
);