Skip to content

Commit 29a77d3

Browse files
authored
Merge pull request #78 from kaustavbecs/main
Adding Notebook and CDK to push Agent observability traces to Langfuse
2 parents 8b98ccd + d8eae11 commit 29a77d3

File tree

33 files changed

+12399
-1
lines changed

33 files changed

+12399
-1
lines changed

CONTRIBUTORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
- [Bhavin Patel](https://github.com/bhavinjpatel)
1414
- [Ryan Sachs](https://github.com/sachsry)
1515
- [SaiJeevan Devireddy](https://github.com/devireds)
16+
- [Alex Thewsey](https://github.com/athewsey)
17+
- [Kaustav Dey](https://github.com/kaustavbecs)
18+
- [Tony Santiago](https://github.com/tsanti)

RELEASE_NOTES.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ Adding [Agent with access to house security camera in cloudformation](/examples/
9797

9898
[Human-in-the-loop Agent](/examples/agents/human_in_the_loop/)
9999

100+
100101
## 03/31/2025
101102

102-
[Inline Agent SDK](./src/InlineAgent/)
103+
[Inline Agent SDK](./src/InlineAgent/)
104+
105+
## 04/02/2025
106+
107+
[Langfuse self deployment](/examples/agent_observability/deploy-langfuse-on-ecs-fargate-with-typescript-cdk/)

examples/agent_observability/OpenTelemetry-Agent-Instrumentation/trace-bedrock-agents-with-langfuse.ipynb

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"env": {
3+
"jest": true,
4+
"node": true
5+
},
6+
"root": true,
7+
"plugins": [
8+
"@typescript-eslint",
9+
"import"
10+
],
11+
"parser": "@typescript-eslint/parser",
12+
"parserOptions": {
13+
"ecmaVersion": 2018,
14+
"sourceType": "module",
15+
"project": "./tsconfig.json"
16+
},
17+
"extends": [
18+
"plugin:import/typescript",
19+
"plugin:prettier/recommended"
20+
],
21+
"settings": {
22+
"import/parsers": {
23+
"@typescript-eslint/parser": [
24+
".ts",
25+
".tsx"
26+
]
27+
},
28+
"import/resolver": {
29+
"node": {},
30+
"typescript": {
31+
"project": "./tsconfig.json",
32+
"alwaysTryTypes": true
33+
}
34+
}
35+
},
36+
"ignorePatterns": [
37+
"*.js",
38+
"*.d.ts",
39+
"node_modules/",
40+
"*.generated.ts",
41+
"coverage",
42+
"!.projenrc.ts",
43+
"!projenrc/**/*.ts"
44+
],
45+
"rules": {
46+
"@typescript-eslint/no-require-imports": [
47+
"error"
48+
],
49+
"import/no-extraneous-dependencies": [
50+
"error",
51+
{
52+
"devDependencies": [
53+
"**/test/**",
54+
"**/build-tools/**",
55+
".projenrc.ts",
56+
"projenrc/**/*.ts"
57+
],
58+
"optionalDependencies": false,
59+
"peerDependencies": true
60+
}
61+
],
62+
"import/no-unresolved": [
63+
"error"
64+
],
65+
"import/order": [
66+
"warn",
67+
{
68+
"groups": [
69+
"builtin",
70+
"external"
71+
],
72+
"alphabetize": {
73+
"order": "asc",
74+
"caseInsensitive": true
75+
}
76+
}
77+
],
78+
"import/no-duplicates": [
79+
"error"
80+
],
81+
"no-shadow": [
82+
"off"
83+
],
84+
"@typescript-eslint/no-shadow": [
85+
"error"
86+
],
87+
"key-spacing": [
88+
"error"
89+
],
90+
"no-multiple-empty-lines": [
91+
"error"
92+
],
93+
"@typescript-eslint/no-floating-promises": [
94+
"error"
95+
],
96+
"no-return-await": [
97+
"off"
98+
],
99+
"@typescript-eslint/return-await": [
100+
"error"
101+
],
102+
"no-trailing-spaces": [
103+
"error"
104+
],
105+
"dot-notation": [
106+
"error"
107+
],
108+
"no-bitwise": [
109+
"error"
110+
],
111+
"@typescript-eslint/member-ordering": [
112+
"error",
113+
{
114+
"default": [
115+
"public-static-field",
116+
"public-static-method",
117+
"protected-static-field",
118+
"protected-static-method",
119+
"private-static-field",
120+
"private-static-method",
121+
"field",
122+
"constructor",
123+
"method"
124+
]
125+
}
126+
]
127+
},
128+
"overrides": [
129+
{
130+
"files": [
131+
".projenrc.ts"
132+
],
133+
"rules": {
134+
"@typescript-eslint/no-require-imports": "off",
135+
"import/no-extraneous-dependencies": "off"
136+
}
137+
}
138+
]
139+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Ignore python notebook checkpoints
2+
.ipynb_checkpoints
3+
4+
# Ignore python venv folder
5+
.venv
6+
7+
# Ignore .DS_Store files
8+
.DS_Store
9+
10+
# Ignore build output folders
11+
12+
build/
13+
dist/
14+
15+
# Add lib folder
16+
!lib
17+
18+
# Ignore node_modules folder
19+
node_modules/
20+
21+
# Ignore log files and databases
22+
*.log
23+
*.sql
24+
*.sqlite
25+
26+
# Ignore OS generated files
27+
.DS_Store
28+
29+
.idea
30+
31+
.vscode
32+
.vscode/**
33+
34+
._*
35+
.Spotlight-V100
36+
.Trashes
37+
ehthumbs.db
38+
Thumbs.db
39+
40+
# Ignore python notebook checkpoints
41+
/.ipynb_checkpoints/
42+
43+
44+
# ignore cdk-related directories
45+
dist
46+
cdk.out
47+
.cdk.staging
48+
49+
config.py
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"overrides": []
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MIT No Attribution
2+
3+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
13+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
15+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Self-Host Langfuse on Amazon ECS with Fargate using TypeScript CDK
2+
3+
Langfuse is an Open Source LLM Engineering platform that helps teams collaboratively debug, analyze, and iterate on their LLM applications.
4+
5+
This repository demonstrates how to deploy a self-hosted Langfuse solution using AWS Fargate for Amazon ECS. It is designed for initial experimentation and is not suitable for production use. Additionally, it does not include all capabilities available in the Langfuse Enterprise Edition. For production-ready deployments, check out the [Langfuse offerings through AWS Marketplace](https://aws.amazon.com/marketplace/seller-profile?id=seller-nmyz7ju7oafxu).
6+
7+
## Architecture overview
8+
9+
This deployment involves multiple AWS services to host Langfuse components as described in their [official documentation on self-hosting](https://langfuse.com/self-hosting#architecture). In this sample, shown also in the architecture diagram below, we use:
10+
11+
1. [AWS Fargate for Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html) to host the required containers without the need to manage servers or clusters of Amazon EC2 instances
12+
2. [Amazon RDS](https://aws.amazon.com/rds/postgresql/) for the Postgres OLTP store
13+
3. [Amazon Elasticache](https://aws.amazon.com/elasticache/) for the Valkey cache
14+
4. [Amazon EFS](https://aws.amazon.com/efs/) for durable managed storage to back the deployed [ClickHouse](https://clickhouse.com/docs/intro) OLAP system
15+
5. [Amazon CloudFront](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html) for high-performance CDN and HTTPS connectivity to the deployed Langfuse service.
16+
17+
![](doc/CDK-Langfuse-Architecture.png "Architecture diagram showing LLM applications and web browsers connecting to Langfuse through the above described components")
18+
19+
### Request Flow Sequence:
20+
21+
1. **Client Request**: LLM applications and web browsers initiate requests through the Langfuse SDK or directly via the web interface. These requests are routed to Amazon CloudFront, which serves as the Content Delivery Network (CDN) to provide HTTPS termination, caching, and global distribution.
22+
2. **Routing Through Application Load Balancer**: CloudFront forwards the requests to the Application Load Balancer (ALB) within the Virtual Private Cloud (VPC). The ALB ensures proper routing of traffic to the ECS cluster hosting Langfuse components.
23+
3. **Processing in ECS Cluster**: The ECS cluster, powered by AWS Fargate, processes requests using two primary components:
24+
25+
* Langfuse Web Server: Handles API endpoints and user-facing interactions.
26+
* Langfuse Worker: Processes background tasks such as event batching and analytics.
27+
28+
4. **Data Storage Operations**: Depending on the type of data being processed, requests interact with various storage systems:
29+
30+
* Amazon RDS Aurora Postgres OLTP: Stores transactional data such as user configurations, API keys, and project metadata.
31+
* Amazon EFS-backed ClickHouse OLAP: Handles analytical workloads for high-volume trace and span data.
32+
* Amazon S3: Stores raw objects like logs or files uploaded during processing.
33+
34+
5. **Caching with ElastiCache**: The system utilizes Amazon ElastiCache (Valkey cache) for Redis-compatible caching to optimize performance by reducing database load and enabling faster access to frequently queried data.
35+
6. **Response Delivery**: Once processing is complete, responses are sent back to clients through the same path, ensuring secure delivery via CloudFront. Metrics and logs generated during processing from ECS are also forwarded to Amazon CloudWatch for observability.
36+
37+
This flow ensures efficient handling of requests while leveraging AWS's managed services for scalability, reliability, and security.
38+
39+
## Deployment option 1: Quick start
40+
41+
If you don't have CDK development tooling set up already, and would just like to deploy the Langfuse architecture with the default settings - you can use the [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) template in [cfn_bootstrap.yml](./cfn_bootstrap.yml): Open the [CloudFormation Console](https://console.aws.amazon.com/cloudformation/home?#/stacks/create) in your target AWS Account and Region, click **Create stack**, and upload the template file.
42+
43+
This "bootstrap" stack sets up a CI project in [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) which pulls the sample code and performs the below app CDK setup steps for you automatically in the Cloud - with no local development environment required. ⚠️ **Note** though, that the CodeBuild project is granted *broad permissions* to deploy all the sample's AWS resources on your behalf - so is not recommended for use in production environments as-is.
44+
45+
## Deployment option 2: Developer setup (Suggested workflow)
46+
47+
If you are comfortable with CDK deployment or want to customize the app, you'll need to set up your local development environment rather than using the quick-start template above.
48+
49+
### Prerequisites:
50+
51+
1. Docker or Finch
52+
53+
This project requires a local container build environment. If your organization doesn't support [Docker Desktop](https://www.docker.com/products/docker-desktop/), you can instead install [Finch](https://runfinch.com/). If using Finch instead of Docker, remember to:
54+
55+
- Initialise the VM with `finch vm start`, and
56+
- Tell CDK how to build containers, by running `export CDK_DOCKER=finch` (on MacOS/Linux), or `SET CDK_DOCKER=finch` (on Windows)
57+
58+
2. NodeJS
59+
60+
This project requires [NodeJS](https://nodejs.org/) v20+
61+
62+
3. AWS CLI login
63+
64+
To actually deploy the infrastructure to a target AWS Account (and possibly, even to synthesize a concrete template), you'll need to [configure your AWS CLI credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). Note that whatever principal you log in to AWS with (User, Role, etc) will need the relevant *IAM Permissions* to deploy and manage all types of resources used by the sample - which is a broad set.
65+
66+
You'll also want to set your target *AWS Region*. You can check your current active AWS Account by running `aws sts get-caller-identity`, and your selected region with `aws configure get region`.
67+
68+
69+
### Development workflow
70+
71+
Once your development environment is set up, this sample works like a standard CDK app project.
72+
73+
First, install the project's dependencies:
74+
75+
```bash
76+
npm install
77+
```
78+
79+
Then, you can deploy it to AWS:
80+
81+
```bash
82+
# Deploy or update all Stacks in the app:
83+
# (Optionally specify --require-approval never to suppress approval prompts)
84+
npx cdk deploy --all
85+
```
86+
87+
To **delete** the deployed infrastructure when you're done exploring, and avoid ongoing charges:
88+
89+
> ⚠️ **Warning:** Running the below will irreversibly delete any data you've stored in your deployed Langfuse instance!
90+
91+
```bash
92+
npx cdk destroy --all
93+
```
94+
95+
**Other useful commands** for the project include:
96+
- `npx cdk destroy` to delete the deployed infrastructure
97+
- `npx cdk synth` to ["synthesize"](https://docs.aws.amazon.com/cdk/v2/guide/configure-synth.html) the CDK application to deployable CloudFormation template(s), without actually deploying
98+
- `npx cdk list` to list all CDK stacks and their dependencies
99+
100+
Refer to the [CDK CLI commands guide](https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd.html) for a full reference
101+
102+
103+
## License
104+
This library is licensed under the MIT-0 License. See the LICENSE file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ARG BASE_IMAGE=clickhouse:latest
2+
3+
FROM ${BASE_IMAGE}
4+
5+
COPY ./ecs.config.xml /etc/clickhouse-server/config.d/ecs.config.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<clickhouse>
2+
<!-- ClickHouse configuration overrides for running in Amazon ECS -->
3+
<logger>
4+
<level>information</level>
5+
<!-- Send logs to console (therefore CloudWatch) rather than saving local files -->
6+
<console>true</console>
7+
</logger>
8+
</clickhouse>

0 commit comments

Comments
 (0)