# dynatrace-oss/dtctl architecture

> Architecture diagram of the GitHub repository dynatrace-oss/dtctl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dynatrace-oss/dtctl
- Repository: https://github.com/dynatrace-oss/dtctl

## Overview

1. **What this repository is**
   - `dtctl` is a Go-based command-line client for the Dynatrace platform.
   - It is not a server application; the core architecture is a CLI orchestration layer around Dynatrace APIs, DQL execution, and local rendering/automation features.

2. **Top-level execution path**
   - `main.go` boots the CLI entrypoint in `cmd/`.
   - `cmd/root.go` and related files define the global command tree, flags, aliases, help text, and command routing.
   - From there, verb/resource subcommands fan out into domain handlers such as `get`, `describe`, `create`, `apply`, `delete`, `edit`, `exec`, `query`, `watch`, and `verify`.

3. **Command-layer boundary**
   - The `cmd/` package is mostly orchestration and user-facing wiring.
   - It parses CLI input, resolves aliases/contexts, handles prompts and safety checks, selects output modes, and delegates to the lower-level packages.
   - The large command surface is split by verb and by resource type, which keeps behavior localized while still sharing common helpers.

4. **Core platform interaction layer**
   - `pkg/client` is the central HTTP/API layer for Dynatrace communication.
   - It handles request construction, pagination, OAuth support, and API error normalization.
   - This is the main boundary between local CLI logic and external Dynatrace services.

5. **Authentication and environment selection**
   - `pkg/auth` implements OAuth login flows and token management.
   - `pkg/config` stores contexts, aliases, and credential-related settings, with keyring support for secret storage.
   - The effective runtime path is: CLI command → resolved context/config → auth token selection → API client.

6. **Resource/domain model layer**
   - `pkg/resources` is the main domain layer and is organized by Dynatrace resource family: workflows, dashboards/notebooks, documents, segments, settings, SLOs, buckets, lookups, anomaly detectors, cloud integrations, extensions, app functions/intents, notifications, IAM, EdgeConnect, live debugger, and more.
   - Each resource package encapsulates API-specific behavior, schema mapping, formatting helpers, and resource-specific transformations.
   - This is where the CLI’s generic verbs become concrete operations against a particular Dynatrace object type.

7. **Declarative change management**
   - `pkg/apply` implements the “apply” pipeline for syncing local definitions to remote Dynatrace resources.
   - It coordinates resource-specific appliers, detects create/update/delete-style outcomes, and returns structured results.
   - `pkg/diff` performs normalization and comparison so changes can be shown consistently even when API payloads differ in ordering or shape.
   - Together, these packages form the repo’s declarative configuration subsystem.

8. **Query, execution, and workflow operations**
   - `pkg/exec` contains logic for executing DQL and workflow/function-related actions.
   - `pkg/wait` provides polling/backoff primitives for commands that need to wait on query completion, workflow execution, or other asynchronous conditions.
   - The architecture treats these as asynchronous operations layered over the client/API boundary rather than as simple one-shot reads.

9. **Output and presentation subsystem**
   - `pkg/output` centralizes rendering for tables, JSON, YAML, CSV, charts, sparklines, live views, and AI-agent-friendly envelopes.
   - This package is a major boundary: domain code returns data structures, and output code decides how they are serialized or visualized.
   - It also includes watch/live display behaviors, which sit between polling logic and terminal UX.

10. **AI-agent and automation support**
   - The repo explicitly supports structured agent workflows through `pkg/output/agent.go`, the `skills/` directory, and the bundled skill documentation.
   - `pkg/commands`, `pkg/suggest`, and the “commands” discovery features let an agent inspect available operations and operate predictably.
   - This is a distinct architectural concern: the CLI is designed to be usable by humans and by tool-using AI agents.

11. **Safety, prompts, and operational guardrails**
   - `pkg/safety` enforces rules around potentially destructive actions, tied to scopes and context settings described in the docs.
   - `pkg/prompt` handles interactive confirmation flows when a command requires user approval.
   - This creates a clear safety boundary around mutating commands such as delete/apply/edit/restore.

12. **Specialized subsystems**
   - `pkg/aidetect` and `pkg/resources/copilot` support AI-related features like NL-to-DQL and assistant-style interactions.
   - `pkg/proto/livedebugger` contains generated protobufs for live debugger payloads, which indicates a binary/structured protocol boundary for that feature.
   - `pkg/diagnostic` provides URL and error diagnostics, and `pkg/util` contains shared formatting, templating, and validation helpers used across the CLI.

13. **Testing and validation structure**
   - The repository uses extensive unit tests, golden-output tests, integration tests, and end-to-end tests.
   - `pkg/output/testdata` and `cmd/testdata` validate the exact CLI presentation, while `test/integration` and `test/e2e` exercise real API behavior and command flows.
   - This strongly suggests the architecture is built around stable command contracts and reproducible terminal output.

14. **External surface area**
   - The primary external dependency is the Dynatrace platform itself, accessed over HTTP APIs and OAuth.
   - Secondary external touchpoints include terminal capabilities, keyring storage, and platform-specific shell behavior for installation/completion.
   - Documentation is also a first-class artifact: the `docs/site` and `docs/dev` trees mirror the architecture and command model for users and contributors.

Main entry points: [`cmd/commands.go`](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/commands.go) (Command verbs), [`pkg/client/client.go`](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/client/client.go) (API client), [`pkg/output/printer.go`](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/output/printer.go) (Output), [`cmd/query.go`](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/query.go) (Query cmd)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI layer"]
  node_main(("main.go<br/>entrypoint<br/>[main.go]"))
  node_rootcmd["Root cmd<br/>command tree<br/>[root.go]"]
  node_verbs["Command verbs<br/>orchestration<br/>[commands.go]"]
  node_applycmd["Apply cmd<br/>mutating command<br/>[apply.go]"]
  node_querycmd["Query cmd<br/>query command<br/>[query.go]"]
end

subgraph group_core["Core services"]
  node_client["API client<br/>http client<br/>[client.go]"]
  node_auth["OAuth auth<br/>auth flows<br/>[oauth_flow.go]"]
  node_config["Config<br/>contexts<br/>[config.go]"]
  node_safety["Safety<br/>guardrails<br/>[checker.go]"]
  node_prompt["Prompts<br/>confirmation<br/>[confirm.go]"]
  node_wait["Wait<br/>polling<br/>[query_waiter.go]"]
  node_diff["Diff<br/>comparison<br/>[differ.go]"]
  node_applypkg["Apply engine<br/>sync pipeline<br/>[applier.go]"]
end

subgraph group_domain["Domain resources"]
  node_workflow["Workflows<br/>resource family<br/>[workflow.go]"]
  node_settings["Settings<br/>resource family<br/>[settings.go]"]
  node_documents["Documents<br/>resource family<br/>[document.go]"]
  node_segments["Segments<br/>resource family<br/>[segment.go]"]
  node_integrations["Cloud & ext<br/>resource family<br/>[extension.go]"]
  node_slo["SLOs<br/>resource family<br/>[slo.go]"]
end

subgraph group_present["Presentation"]
  node_output["Output<br/>rendering<br/>[printer.go]"]
  node_watch["Watch<br/>live view<br/>[watcher.go]"]
  node_diagnostic["Diagnostics<br/>errors<br/>[error.go]"]
end

subgraph group_automation["Automation"]
  node_automation["Skills<br/>agent support<br/>[SKILL.md]"]
end

node_main -->|"starts"| node_rootcmd
node_rootcmd -->|"routes to"| node_verbs
node_verbs -->|"dispatches"| node_applycmd
node_verbs -->|"dispatches"| node_querycmd
node_verbs -->|"uses"| node_workflow
node_verbs -->|"uses"| node_settings
node_verbs -->|"uses"| node_documents
node_verbs -->|"uses"| node_segments
node_verbs -->|"uses"| node_integrations
node_verbs -->|"uses"| node_slo
node_applycmd -->|"coordinates"| node_applypkg
node_applypkg -->|"compares"| node_diff
node_applypkg -->|"writes via"| node_client
node_querycmd -->|"waits with"| node_wait
node_querycmd -->|"renders via"| node_output
node_workflow -->|"calls"| node_client
node_settings -->|"calls"| node_client
node_documents -->|"calls"| node_client
node_segments -->|"calls"| node_client
node_integrations -->|"calls"| node_client
node_slo -->|"calls"| node_client
node_client -->|"selects token"| node_auth
node_auth -->|"reads"| node_config
node_config -->|"configures"| node_client
node_verbs -->|"checks"| node_safety
node_verbs -->|"confirms"| node_prompt
node_output -->|"feeds"| node_watch
node_watch -->|"polls with"| node_wait
node_output -->|"formats"| node_diagnostic
node_automation -->|"discovers"| node_rootcmd
node_automation -->|"structures"| node_output

click node_main "https://github.com/dynatrace-oss/dtctl/blob/main/main.go"
click node_rootcmd "https://github.com/dynatrace-oss/dtctl/blob/main/cmd/root.go"
click node_verbs "https://github.com/dynatrace-oss/dtctl/blob/main/cmd/commands.go"
click node_applycmd "https://github.com/dynatrace-oss/dtctl/blob/main/cmd/apply.go"
click node_querycmd "https://github.com/dynatrace-oss/dtctl/blob/main/cmd/query.go"
click node_client "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/client/client.go"
click node_auth "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/auth/oauth_flow.go"
click node_config "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/config/config.go"
click node_safety "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/safety/checker.go"
click node_prompt "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/prompt/confirm.go"
click node_wait "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/wait/query_waiter.go"
click node_diff "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/diff/differ.go"
click node_applypkg "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/apply/applier.go"
click node_output "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/output/printer.go"
click node_watch "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/watch/watcher.go"
click node_diagnostic "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/diagnostic/error.go"
click node_workflow "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/workflow/workflow.go"
click node_settings "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/settings/settings.go"
click node_documents "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/document/document.go"
click node_segments "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/segment/segment.go"
click node_integrations "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/extension/extension.go"
click node_slo "https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/slo/slo.go"
click node_automation "https://github.com/dynatrace-oss/dtctl/blob/main/skills/dtctl/SKILL.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_rootcmd,node_verbs,node_applycmd,node_querycmd toneBlue
class node_client,node_auth,node_config,node_safety,node_prompt,node_wait,node_diff,node_applypkg toneAmber
class node_workflow,node_settings,node_documents,node_segments,node_integrations,node_slo toneMint
class node_output,node_watch,node_diagnostic toneRose
class node_automation toneIndigo
```

## Components

### CLI layer

Entrypoint, routing, command handlers

- [main.go](https://github.com/dynatrace-oss/dtctl/blob/main/main.go): entrypoint · Bootstraps the CLI application · `main.go`
- [Root cmd](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/root.go): command tree · Global command registration, flags, routing, help · `cmd/root.go`
- [Command verbs](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/commands.go): orchestration · Verb/resource command handlers and shared CLI flows · `cmd/commands.go`
- [Apply cmd](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/apply.go): mutating command · CLI entry for declarative sync and change application · `cmd/apply.go`
- [Query cmd](https://github.com/dynatrace-oss/dtctl/blob/main/cmd/query.go): query command · CLI entry for DQL querying and result handling · `cmd/query.go`

### Core services

API, auth, config, safety, polling

- [API client](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/client/client.go): http client · Dynatrace HTTP layer with pagination and error normalization · `pkg/client/client.go`
- [OAuth auth](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/auth/oauth_flow.go): auth flows · Login flows and token management · `pkg/auth/oauth_flow.go`
- [Config](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/config/config.go): contexts · Contexts, aliases, and stored runtime settings · `pkg/config/config.go`
- [Safety](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/safety/checker.go): guardrails · Scope and context checks for risky actions · `pkg/safety/checker.go`
- [Prompts](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/prompt/confirm.go): confirmation · Interactive approval and confirmation flows · `pkg/prompt/confirm.go`
- [Wait](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/wait/query_waiter.go): polling · Backoff and async condition waiting · `pkg/wait/query_waiter.go`
- [Diff](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/diff/differ.go): comparison · Normalization and comparison for declarative change detection · `pkg/diff/differ.go`
- [Apply engine](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/apply/applier.go): sync pipeline · Coordinates resource apply operations and structured results · `pkg/apply/applier.go`

### Domain resources

Dynatrace resource families and resource-specific logic

- [Workflows](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/workflow/workflow.go): resource family · Workflow definitions, executions, and related operations · `pkg/resources/workflow/workflow.go`
- [Settings](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/settings/settings.go): resource family · Settings objects and schema decoding · `pkg/resources/settings/settings.go`
- [Documents](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/document/document.go): resource family · Dashboards, notebooks, multipart/trash handling · `pkg/resources/document/document.go`
- [Segments](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/segment/segment.go): resource family · Segment filters and AST-based matching · `pkg/resources/segment/segment.go`
- [Cloud & ext](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/extension/extension.go): resource family · Azure, GCP, extensions, buckets, EdgeConnect, and related configs · `pkg/resources/extension/extension.go`
- [SLOs](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/resources/slo/slo.go): resource family · Service-level objective operations · `pkg/resources/slo/slo.go`

### Presentation

Rendering, watch/live output, and diagnostics

- [Output](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/output/printer.go): rendering · Tables, JSON, YAML, CSV, charts, live views, and agent envelopes · `pkg/output/printer.go`
- [Watch](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/watch/watcher.go): live view · Polling-driven live change display and diffs · `pkg/watch/watcher.go`
- [Diagnostics](https://github.com/dynatrace-oss/dtctl/blob/main/pkg/diagnostic/error.go): errors · URL and error diagnostics for user-facing failures · `pkg/diagnostic/error.go`

### Automation

Agent skills and command discovery

- [Skills](https://github.com/dynatrace-oss/dtctl/blob/main/skills/dtctl/SKILL.md): agent support · Embedded skills and command discovery for AI-assisted use · `skills/dtctl/SKILL.md`

## Connections

- main.go → Root cmd: starts · Program entry initializes the CLI tree
- Root cmd → Command verbs: routes to · Root command fans out into verb and resource handlers
- Command verbs → Apply cmd: dispatches · Mutating commands are wired through the command layer
- Command verbs → Query cmd: dispatches · Query commands are routed from the shared CLI surface
- Command verbs → Workflows: uses · Resource handlers call into domain packages
- Command verbs → Settings: uses · Resource handlers call into domain packages
- Command verbs → Documents: uses · Resource handlers call into domain packages
- Command verbs → Segments: uses · Resource handlers call into domain packages
- Command verbs → Cloud & ext: uses · Resource handlers call into domain packages
- Command verbs → SLOs: uses · Resource handlers call into domain packages
- Apply cmd → Apply engine: coordinates · CLI apply delegates to the apply pipeline
- Apply engine → Diff: compares · Local definitions are normalized and diffed before sync
- Apply engine → API client: writes via · Apply operations persist changes through the API client
- Query cmd → Wait: waits with · Long-running query flows use polling and backoff
- Query cmd → Output: renders via · Query results are formatted for terminal output
- Workflows → API client: calls · Domain packages use the shared Dynatrace client
- Settings → API client: calls · Domain packages use the shared Dynatrace client
- Documents → API client: calls · Domain packages use the shared Dynatrace client
- Segments → API client: calls · Domain packages use the shared Dynatrace client
- Cloud & ext → API client: calls · Domain packages use the shared Dynatrace client
- SLOs → API client: calls · Domain packages use the shared Dynatrace client
- API client → OAuth auth: selects token · Client requests are authenticated through OAuth/token management
- OAuth auth → Config: reads · Auth flows use stored contexts and credentials
- Config → API client: configures · Resolved context drives the effective API target and credentials
- Command verbs → Safety: checks · Mutating commands enforce guardrails before execution
- Command verbs → Prompts: confirms · Interactive approvals are requested for risky operations
- Output → Watch: feeds · Live views build on the shared rendering subsystem
- Watch → Wait: polls with · Watch mode relies on polling primitives
- Output → Diagnostics: formats · User-facing failures are rendered with diagnostics
- Skills → Root cmd: discovers · Agent-facing command discovery is exposed through the CLI surface
- Skills → Output: structures · Agent mode uses structured output envelopes
