How dynatrace-oss/dtctl works
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: commands.go Command verbs · client.go API client · printer.go Output · query.go Query cmd
Components 23
CLI layer
Entrypoint, routing, command handlers
- main.go · Bootstraps the CLI application
main.go - Root cmd · Global command registration, flags, routing, help
cmd/root.go - Command verbs · Verb/resource command handlers and shared CLI flows
cmd/commands.go - Apply cmd · CLI entry for declarative sync and change application
cmd/apply.go - Query cmd · CLI entry for DQL querying and result handling
cmd/query.go
Core services
API, auth, config, safety, polling
- API client · Dynatrace HTTP layer with pagination and error normalization
pkg/client/client.go - OAuth auth · Login flows and token management
pkg/auth/oauth_flow.go - Config · Contexts, aliases, and stored runtime settings
pkg/config/config.go - Safety · Scope and context checks for risky actions
pkg/safety/checker.go - Prompts · Interactive approval and confirmation flows
pkg/prompt/confirm.go - Wait · Backoff and async condition waiting
pkg/wait/query_waiter.go - Diff · Normalization and comparison for declarative change detection
pkg/diff/differ.go - Apply engine · Coordinates resource apply operations and structured results
pkg/apply/applier.go
Domain resources
Dynatrace resource families and resource-specific logic
- Workflows · Workflow definitions, executions, and related operations
pkg/resources/workflow/workflow.go - Settings · Settings objects and schema decoding
pkg/resources/settings/settings.go - Documents · Dashboards, notebooks, multipart/trash handling
pkg/resources/document/document.go - Segments · Segment filters and AST-based matching
pkg/resources/segment/segment.go - Cloud & ext · Azure, GCP, extensions, buckets, EdgeConnect, and related configs
pkg/resources/extension/extension.go - SLOs · Service-level objective operations
pkg/resources/slo/slo.go
Presentation
Rendering, watch/live output, and diagnostics
- Output · Tables, JSON, YAML, CSV, charts, live views, and agent envelopes
pkg/output/printer.go - Watch · Polling-driven live change display and diffs
pkg/watch/watcher.go - Diagnostics · URL and error diagnostics for user-facing failures
pkg/diagnostic/error.go
Automation
Agent skills and command discovery
- Skills · Embedded skills and command discovery for AI-assisted use
skills/dtctl/SKILL.md
Connections 31
- 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