Skip to content

Commit

Permalink
examples(cf): add cf worker example (#3620)
Browse files Browse the repository at this point in the history
* examples(cf): add cf worker example

* examples(cf): edit readme
  • Loading branch information
adnanrahic committed Feb 12, 2024
1 parent 00a4d3c commit 9a09d3f
Show file tree
Hide file tree
Showing 18 changed files with 2,371 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/testing-cloudflare-workers/.editorconfig
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
172 changes: 172 additions & 0 deletions examples/testing-cloudflare-workers/.gitignore
@@ -0,0 +1,172 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
6 changes: 6 additions & 0 deletions examples/testing-cloudflare-workers/.prettierrc
@@ -0,0 +1,6 @@
{
"printWidth": 140,
"singleQuote": true,
"semi": true,
"useTabs": true
}
15 changes: 15 additions & 0 deletions examples/testing-cloudflare-workers/Dockerfile
@@ -0,0 +1,15 @@
FROM node
WORKDIR /app

COPY package.json package-lock.json* ./
RUN npm i

COPY src ./src
COPY test ./test
COPY wrangler.toml .
COPY tsconfig.json .
COPY schema.sql .

RUN npx wrangler d1 execute testing-cloudflare-workers --local --file=./schema.sql

CMD ["npm", "run", "docker"]
103 changes: 103 additions & 0 deletions examples/testing-cloudflare-workers/README.md
@@ -0,0 +1,103 @@
# Testing Cloudflare Workers with Tracetest and OpenTelemetry

This example is from the article [**Crafting Observable Cloudflare Workers with OpenTelemetry**]() showing how to run integration and staging tests against Cloudflare Workers using [OpenTelemetry](https://opentelemetry.io/) and Tracetest.

This is a project bootstrapped with [`C3 (create-cloudflare-cli)`](https://developers.cloudflare.com/workers/get-started/guide/#1-create-a-new-worker-project).

It's using Cloudflare Workers with [OpenTelemetry configured with otel-cf-workers](https://github.com/evanderkoogh/otel-cf-workers).

## Prerequisites

- [Tracetest Account](https://app.tracetest.io/)
- [Tracetest Agent API Key](https://docs.tracetest.io/configuration/agent)
- [Tracetest Environment Token](https://docs.tracetest.io/concepts/environment-tokens)
- [Cloudflare Workers Account](https://workers.cloudflare.com/)
- [Cloudflare Database](https://developers.cloudflare.com/d1/get-started/)

Install npm modules:

```bash
npm i
```

If you do not have `npx`, install it:

```bash
npm install -g npx
```

Create a D1 database:

```bash
npx wrangler d1 create testing-cloudflare-workers

✅ Successfully created DB 'testing-cloudflare-workers' in region EEUR
Created your database using D1's new storage backend. The new storage backend is not yet recommended for production workloads, but backs up your data via point-in-time
restore.
[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "testing-cloudflare-workers"
database_id = "<your_database_id>"
```
Set the `database_id` credentials in your `wrangler.toml`.
Configure the D1 database:
```bash
npx wrangler d1 execute testing-cloudflare-workers --local --file=./schema.sql
npx wrangler d1 execute testing-cloudflare-workers --file=./schema.sql
```
## Getting Started with Docker
0. Set Tracetest Agent API Key in `docker-compose.yaml`, and set Tracetest Environment Token in `test/run.bash`, and `wrangler.toml`. Set the Cloudflare D1 credentials in the `wrangler.toml` as well.
1. Run Docker Compose
```bash
docker compose up -d --build
```
2. Run Integration Tests
```bash
docker compose run integration-tests
```
(Optional. Trigger Tracetest Tests via `app.tracetest.io` against `http://cloudflare-worker:8787`)
## Getting Started Locally
0. Set the Cloudflare D1 credentials in the `wrangler.toml` as well.
1. Install Node Packages
```bash
npm i
```
2. Run Development Server
```bash
npm run dev
```
3. Start Tracetest Agent
```bash
tracetest start --api-key ttagent_<apikey>
```
4. Trigger Tracetest Tests via CLI
```bash
tracetest run test -f ./api.pokemon.spec.development.yaml
```
(Optional. Trigger Tracetest Tests via `app.tracetest.io` against `http://localhost:8787`)
## Learn more
Feel free to check out the [docs](https://docs.tracetest.io/), and join our [Slack Community](https://dub.sh/tracetest-community) for more info!
42 changes: 42 additions & 0 deletions examples/testing-cloudflare-workers/docker-compose.yaml
@@ -0,0 +1,42 @@
version: "3"

services:
cloudflare-worker:
image: foobar/cloudflare-worker:v1
build:
context: .
dockerfile: ./Dockerfile
restart: always
ports:
- 8787:8787
networks:
- tracetest

tracetest-agent:
image: kubeshop/tracetest-agent:latest
environment:
- TRACETEST_API_KEY=ttagent_<api_key> # Find the Agent API Key here: https://docs.tracetest.io/configuration/agent
ports:
- 4317:4317
- 4318:4318
command: ["--mode", "verbose"]
networks:
- tracetest

integration-tests:
image: foobar/integration-tests:v2
profiles:
- tests
build:
context: ./
dockerfile: ./test/Dockerfile
volumes:
- ./test/:/app/test/
depends_on:
tracetest-agent:
condition: service_started
networks:
- tracetest

networks:
tracetest:

0 comments on commit 9a09d3f

Please sign in to comment.