Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Create a wrapper library to tag span with API metadata #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
85de889
Create bufftracer library
philippemiguet Oct 3, 2021
aebeddc
Add editor config
philippemiguet Oct 3, 2021
41c01bb
Reuse DD_SERVICE_NAME
philippemiguet Oct 3, 2021
92da655
Update README.md
philippemiguet Oct 3, 2021
93e64cc
Update README.md
philippemiguet Oct 3, 2021
39ac0d6
Create BuffTracerError class
philippemiguet Oct 4, 2021
95bf57f
Bump to 0.0.2
philippemiguet Oct 4, 2021
35ee8f9
Export types
philippemiguet Oct 4, 2021
8aae01c
Allow to only define DD_ENABLE_TRACING=false locally
philippemiguet Oct 4, 2021
8cfeb15
Bump to 0.0.4
philippemiguet Oct 4, 2021
374cfe9
switch to DD_TRACE_AGENT_HOSTNAME
philippemiguet Oct 4, 2021
17d5d5e
Move type guards to utils
philippemiguet Oct 4, 2021
0cd9a69
Change return type to void
philippemiguet Oct 4, 2021
d9b5acc
remove editor config
philippemiguet Oct 4, 2021
92a2fc9
0.0.6
philippemiguet Oct 4, 2021
4014465
Add debug logs
philippemiguet Oct 4, 2021
100f727
Bump version
philippemiguet Oct 4, 2021
1abbfe6
fix earlier of dd-trace
philippemiguet Oct 4, 2021
3d71eb1
0.0.8
philippemiguet Oct 4, 2021
bf38dee
Drop initTracer because dd-trace does not work with it
philippemiguet Oct 4, 2021
3383ae0
Update version command
philippemiguet Oct 4, 2021
24b2178
Update version command
philippemiguet Oct 4, 2021
88253c6
Update readme
philippemiguet Oct 4, 2021
119cd2f
v0.0.13
philippemiguet Oct 4, 2021
d91577d
v0.0.8
philippemiguet Oct 4, 2021
081adfd
update readme
philippemiguet Oct 4, 2021
a3e05a7
Split fields into several tag to fit within DD 200 chars limit
philippemiguet Oct 5, 2021
e28aea0
v0.1.0
philippemiguet Oct 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/base-backend-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ This package provides the base setup to build a new backend package at Buffer:
- TypeScript
- Jest
- Package commands

## How to use

For now, simply copy-paste this folder, then manually edit the package.json:
- name
- version
- description
- homepage
- keywords

In the future, we should write a `npx` script instead of having to copy-paste.
4 changes: 2 additions & 2 deletions backend/base-backend-package/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/base-backend-package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bufferapp/base-backend-package",
"version": "0.0.7",
"version": "0.0.8",
"description": "A base package for writing new backend packages.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
37 changes: 37 additions & 0 deletions backend/bufftracer/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"extends": [
"standard",
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended" // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
"ignorePatterns": ["node_modules/*", "lib/*"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"prettier",
"@typescript-eslint",
"jest"
],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error",
"no-underscore-dangle": "off"
}
}

7 changes: 7 additions & 0 deletions backend/bufftracer/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"bracketSpacing": true
}
57 changes: 57 additions & 0 deletions backend/bufftracer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# BuffTracer

A Datadog wrapper library to enable DD APM and tag API traces

## Getting Started

### Install npm package
```shell
npm install --save-exact @bufferapp/bufftracer
```

### Initialize tracer at the start of the service
This should be the first import in your `index.ts`file.
```typescript
import '@bufferapp/bufftracer'
```

### Declare the following required env variables in the helm charts of each service
```yaml
// core-api.yaml

- name: DD_ENABLE_TRACING
value: 'true'
- name: DD_SERVICE_NAME
value: 'core-api'
- name: DD_TRACE_AGENT_HOSTNAME
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: DD_TRACE_AGENT_PORT
value: '8126'
- name: NODE_ENV # or APP_STAGE
value: production
```

## What it does

The library parses the incoming request, generates metadata, sets DD resource name to be the edge/endpoint and tags the DD span with the metadata.

#### API metadata format

```typescript
type APIRequestMetadata = {
name: 'core-api' | string
type: 'graphql' | 'rpc' | 'rest'
client: 'buffertools-graphql-playground' | string
edge: string
fields: Array<string>
deprecatedFields: Array<string>
args: Array<string>
}
```

#### Datadog APM tag
![image](https://user-images.githubusercontent.com/727592/135768930-aee25005-4af3-49f9-b34a-bbcf0646f320.png)

The DD APM tags are then used to generate DD custom metrics named `api.request`, which we can use to build dashboard and know who is consuming which parts of our API.
12 changes: 12 additions & 0 deletions backend/bufftracer/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
verbose: true,
modulePathIgnorePatterns: ['node_modules/'],
preset: 'ts-jest',
setupFilesAfterEnv: ['./jest.env.ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
}

export default config
6 changes: 6 additions & 0 deletions backend/bufftracer/jest.env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
process.env.DD_SERVICE_NAME = 'foo-api'
process.env.DD_TRACE_AGENT_HOSTNAME = 'localhost'
process.env.DD_TRACE_AGENT_PORT = '80'
process.env.DD_ENABLE_TRACING = 'true'
process.env.APP_STAGE = 'test'
process.env.NODE_ENV = 'test'
Loading