# datadog-analyser

Tools for querying Datadog CI Visibility and APM data.

## Setup

Install dependencies:
```bash
pnpm install
```

Create a `.env` file in the project root:
```
DD_API_KEY=your_api_key
DD_APP_KEY=your_app_key
DD_SITE=datadoghq.com  # optional, defaults to datadoghq.com
```

## Commands

### service-hits

Find all `playwright-tests` CI tests from the last N minutes that hit a given service, based on APM trace data.

```bash
pnpm dev service-hits <service> [minutes_back] [--tag <tag>] [--show-routes]
```

| Argument | Description | Default |
|---|---|---|
| `service` | Datadog APM service name to search for | required |
| `minutes_back` | How far back to look for CI test runs | `5` |
| `--tag <tag>` | Filter CI tests by tag | none |
| `--show-routes` | Write a JSON report of routes hit per test | off |

**Examples:**
```bash
# All playwright-tests that hit graphql-collaborator in the last 5 minutes
pnpm dev service-hits graphql-collaborator

# Last 10 minutes, filtered by tag
pnpm dev service-hits graphql-collaborator 10 --tag collaborators

# Output a JSON routes report
pnpm dev service-hits graphql-collaborator 5 --tag collaborators --show-routes
```

When `--tag` is used, tests matching the tag that did **not** hit the service are also listed.

When `--show-routes` is used, a JSON file is written (`service-hits-<service>-<timestamp>.json`) with the following structure:

```json
[
  {
    "testName": "Collaborators list is displayed",
    "testSuite": "/features/collaborators/collaborators.feature",
    "traceId": "1234567890",
    "service": "graphql-collaborator",
    "hitsService": true,
    "routes": ["GET /collaborators", "POST /collaborators/search"]
  }
]
```

> **Note:** Route matching uses APM `resource_name` and only includes entries that start with an HTTP method (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`, etc.).

### tag-analysis (legacy)

Analyse which repositories and pipelines ran tests with a given tag over the last N days.

```bash
pnpm dev <tag> [days_back]
```

```bash
pnpm dev "@id_abc123" 30
```

## Project Structure

```
datadog-analyser/
├── src/
│   ├── index.ts                          # Entry point / command router
│   ├── api/
│   │   ├── DatadogClient.ts              # HTTP client for Datadog API
│   │   └── types.ts                      # Shared types
│   └── analyzers/
│       ├── BaseTestAnalyzer.ts           # Base class for CI visibility queries
│       ├── TestRepositoryAnalyzer.ts     # Tag-based repository analysis
│       └── ServiceHitAnalyzer.ts         # Service hit detection via APM spans
├── dist/                                 # Compiled JavaScript output
├── package.json
├── tsconfig.json
└── pnpm-lock.yaml
```
