APM: Docker + Express + Prisma + PG + Typescript

This sandbox was created to test and workwith Datadogs Node Tracer. You will be working with a REST API that contains routes using Express for a small blogging application in TypeScript using Prisma and a PostgreSQL database. All REST API routes will be tested using curl, a terminal-based HTTP client.

Getting Started

This sandbox requires Docker to run. Confirm your version like so:

docker -v

Sections

  • Step 1: Configure the ENV file with your Datadog Agent API Key
  • Step 2: Spin up Docker containers
  • Step 3: Migrate prisma schema
  • Step 4: Generating Traces
  • Step 5: Shutting it down

Step 1: Configure the ENV file with your Datadog Agent API Key

Make sure that in your ~ directory, you have a file called sandbox.docker.env that contains:

DD_API_KEY=<Your API Key>

Now when you run the next steps, you don't have to worry about your API Key in plain text somehow making its way into the repo.

Step 2: Spin up Docker containers

  1. Build the image: docker-compose build
  2. Spin up the containers: docker-compose up or docker-compose up -d to run containers in the background.

Step 3: Migrate prisma schema

Navigate to your Docker dashboard and check that all your images are up and running: Docker images running

postgres - to execute and run the Postgres database container on port 5432. To access the database, set the environment as POSTGRES_USER: postgres, POSTGRES_PASSWORD: postgres, and POSTGRES_DB: myblog.

node-prisma-app_datadog-agent_1 - This is the Datadog Agent image that the Traces will be sent to.

pgadmin - Pgadmin will help you have a visual representation of the Postgres database. It has an interactive UI that lets you see the data that you manipulate. Prisma automatically sets this database and table.

prisma-postgres-api - this service runs the Prisma API that you have just created. It will access the Postgres service, run the Prisma schema and populate our data models to the database.

Then, hover over the prisma-postgres-api and open the integrated Docker API: Prisma cli

This will launch an interactive CLI to run your API commands. In this case, you want to run the npx prisma migrate dev command. This will allow the database to sync with the schema in the application.

Step 4: Generating traces

HTTP Method Route Description
GET /users Fetches all created users.
GET /feed Fetches all published posts.
GET /post/:id Fetches a specific post by its ID.
POST /user Creates a new user.
POST /post Creates a new post (as a draft).
PUT /post/publish/:id Sets the published field of a post to true.
DELETE post/:id Deletes a post by its ID.
  1. Create a user:
curl -X POST -H "Content-Type: application/json" -d '{"name":"barb", "email":"barb@gmail.com"}' http://localhost:3000/user
curl -X POST -H "Content-Type: application/json" -d '{"name":"debbie", "email":"debbie@gmail.com"}' http://localhost:3000/user
curl -X POST -H "Content-Type: application/json" -d '{"name":"bob", "email":"bob@gmail.com"}' http://localhost:3000/user
curl -X POST -H "Content-Type: application/json" -d '{"name":"willy", "email":"willy@gmail.com"}' http://localhost:3000/user
curl -X POST -H "Content-Type: application/json" -d '{"name":"sam", "email":"sam@gmail.com"}' http://localhost:3000/user
  1. To create a post:
curl -X POST -H "Content-Type: application/json" -d '{"title":"I like green eggs and ham", "authorEmail":"barb@gmail.com"}' http://localhost:3000/post
curl -X POST -H "Content-Type: application/json" -d '{"title":"I like eggs", "authorEmail":"debbie@gmail.com"}' http://localhost:3000/post
curl -X POST -H "Content-Type: application/json" -d '{"title":"What a beautiful day", "authorEmail":"bob@gmail.com"}' http://localhost:3000/post
curl -X POST -H "Content-Type: application/json" -d '{"title":"Good morning sunshine the earth says hello", "authorEmail":"willy@gmail.com"}' http://localhost:3000/post
curl -X POST -H "Content-Type: application/json" -d '{"title":"Sam I am", "authorEmail":"sam@gmail.com"}' http://localhost:3000/post
  1. To pubilish a post:
curl -X PUT http://localhost:3000/post/publish/1
curl -X PUT http://localhost:3000/post/publish/2
curl -X PUT http://localhost:3000/post/publish/3
curl -X PUT http://localhost:3000/post/publish/4
curl -X PUT http://localhost:3000/post/publish/5
  1. To see all users:
curl http://localhost:3000/users
  1. To see feed of published posts:
curl http://localhost:3000/feed
  1. To delete a post:
curl -X DELETE http://localhost:3000/post/1
curl -X DELETE http://localhost:3000/post/2
curl -X DELETE http://localhost:3000/post/3
curl -X DELETE http://localhost:3000/post/4
curl -X DELETE http://localhost:3000/post/5

Step 5: Shutting it down

If you are still in the same terminal you have initialized the app from, you can use Ctrl + C to stop the containers, then run docker-compose down to remove the containers and the network. If you are in a different terminal, you can simply run docker-compose down to both stop the application and remove the containers/network.

Credit

I was able to get the prisma query traces to appear in Datadog thanks to the solution provided in this Github issue: DataDog/dd-trace-js#1244

Frameworks and Libraries

This application uses a number of libraries:

  • [node.js] - for the backend
  • [Express] - fast node.js network app framework
  • [Prisma] - an open-source ORM for Node.js and TypeScript