gcx CLI

repository·main·Indexed 19 days ago

https://github.com/grafana/gcx

A CLI tool for Grafana (Cloud, Enterprise, and OSS) providing structured access to resources including dashboards, alerts, SLOs, metrics, logs, and traces. It features a Claude Code plugin that enables AI coding agents to manage Grafana resources, perform diagnostic workflows via the grafana-debugger agent, and automate GitOps workflows. Supports authentication via OAuth, service account tokens, and Cloud Access Policy (CAP) tokens.

Tokens
567.4K
Snippets
1.7K
Records
2.3K
Agent score
61%

What's inside gcx

  1. Overview of the gcx CLI

    main

    gcx is a unified CLI designed for both human users and AI coding agents to interact with Grafana and Grafana Cloud products. It provides structured access to Grafana resources (dashboards, folders, alert rules, data sources) and Grafana Cloud-specific services like Synthetic Monitoring, k6, Fleet Management, Incidents, and Adaptive Telemetry.

    Key capabilities include:

    • GitOps: Full round-trip fidelity for pulling resources to files and pushing them back.
    • Observability as Code: Scaffolding Go projects, importing dashboards, linting with Rego, and live-reloading.
    • Multi-environment management: Using named contexts to switch between environments (e.g., dev, staging, prod).
    • AI Agent Integration: Native support for agentic workflows with auto-detection for tools like Claude Code, Copilot, and Cursor.
  2. Manage Grafana datasources with `gcx datasources`

    main

    The gcx datasources command group provides a declarative, manifest-based interface for managing the full lifecycle of Grafana datasources. Instead of using imperative flags, you interact with datasources using Kubernetes-style manifests (YAML/JSON) supplied via files or stdin. This approach supports a 'fetch → edit → apply' workflow, making it ideal for both human operators and AI agents.

    Key capabilities include:

    • CRUD Operations: create, get, list, update, and delete.
    • Health Checks: Verify the status of specific datasources or all datasources of a certain type.
    • Schema Discovery: Inspect the configuration schema of installed datasource plugins to author valid manifests.
    • Safe Secret Handling: Use the fromEnv keyword in manifests to reference environment variables, ensuring plaintext secrets never appear in command logs or outputs.
  3. Use the gcx CLI for Grafana management

    main

    The gcx CLI is a unified control plane for managing Grafana resources, including dashboards, datasources, alerting, and various Grafana Cloud product APIs (such as SLO, IRM, Synthetic Monitoring, Fleet, and k6).

    To see a list of bundled Agent Skills along with task-specific guidance, run:

    gcx agent skills list
  4. Explore the gcx CLI command tree

    main

    The gcx CLI is organized into a hierarchical command tree. It provides high-level commands for interacting with Grafana resources (like dashboards, metrics, logs, and traces), low-level API access via api, configuration management via config, and developer tooling via dev.

    Common persistent flags available at the root include:

    • --no-color: Disable color output.
    • --no-truncate: Disable table column truncation.
    • --agent: Enable agent mode.
    • --verbose / -v: Increase verbosity (can be used multiple times).
    gcx --verbose --no-color [command]
    # Example: list dashboards
    gcx dashboards list
  5. Explore the gcx command structure and providers

    main

    The gcx CLI is organized into several top-level command groups, each acting as a provider for specific Grafana features or services. You can interact with these via dedicated subcommands.

    Core Command Groups

    • gcx resources: Manage resources using GVK (Group/Version/Kind) identifiers (e.g., push, pull, get).
    • gcx datasources: Query and manage datasources like Prometheus, Loki, and Pyroscope.
    • gcx linter: Run linting rules against configurations (accessible via dev lint).
    • gcx dev: Developer utilities including import, scaffold, generate, and serve.
    • gcx setup: Aggregated cross-product status checks.

    Specialized Providers

    Each provider exposes its own command tree:

    • Signals: gcx metrics, gcx logs, gcx traces, gcx profiles.
    • Observability & Reliability: gcx alert, gcx slo, gcx synth (Synthetic Monitoring), gcx irm (OnCall/Incidents).
    • Cloud & Fleet: gcx fleet, gcx instrumentation (Beyla/Kubernetes discovery), gcx k6.
    • Frontend & AI: gcx faro, gcx assistant, gcx kg (Knowledge Graph).

    To see all available commands, run gcx --help.

  6. Use the `gcx instrumentation` CLI for Kubernetes observability

    main

    The gcx instrumentation command set is used to manage Kubernetes observability (specifically Beyla/Alloy pipelines) across your fleet. It is split into two main trees:

    1. Configuration Tree (clusters): Used to declare the desired state of instrumentation for specific clusters and namespaces. These changes are stored as part of the cluster configuration.
    2. Observation Tree (services): Used to view the actual observed state of services across the fleet. This is a projection of what is currently being seen by Prometheus and does not represent stored configuration.

    Use gcx instrumentation status to get a cross-cutting view of both declared and observed state.

    # View the status of clusters and services
    gcx instrumentation status
    
    # List all services across the fleet
    gcx instrumentation services list
    
    # Configure instrumentation for a specific cluster
    gcx instrumentation clusters enable <cluster-name>
  7. Identify the target users for `gcx instrumentation`

    main

    The gcx instrumentation surface is designed for two primary user roles:

    1. Onboarding Operators (Primary)

    Goal: End-to-end instrumentation of new clusters. Workflow: Uses setup commands, action verbs, and wait commands to monitor state-machine transitions until instrumentation is complete.

    2. Investigators/SREs (Secondary)

    Goal: Troubleshooting telemetry gaps (e.g., "Why isn't this app producing data?"). Workflow: Uses status commands, services list --status=ERROR, and granular per-workload reads to see what the collectors (like Beyla) are actually observing.

  8. Manage Agent Observability with gcx agento11y

    main

    Agent Observability allows you to inspect and manage LLM-powered application production data. It tracks generations (individual LLM API calls), groups them into conversations (user sessions), and uses evaluators (scoring functions) to assess quality. Rules bind these evaluators to production traffic by selecting specific generations and controlling sampling rates.

    All management commands are prefixed with gcx agento11y.

  9. Overview of gcx authentication methods

    main

    The gcx CLI supports several authentication methods depending on whether you are targeting a Grafana instance (API/K8s) or Grafana Cloud (GCOM/Product APIs).

    Authentication Matrix

    MethodTargetProvisioningStorage Field
    OAuth PKCEGrafana API, K8s /apisBrowser flow via gcx loginGrafanaConfig.OAuthToken, etc.
    Service account tokenGrafana API, K8s /apisGrafana UI (Administration → Service accounts)GrafanaConfig.APIToken
    mTLS client certificateGrafana API, K8s /apisIdentity-aware proxy (e.g. Teleport)GrafanaConfig.TLS
    Cloud Access Policy tokenGCOM, Cloud product APIsGrafana Cloud UI (Security → Access policies)CloudEntry.Token
    Grafana Cloud OAuthGCOM, Cloud product APIsBrowser flow via gcx cloud loginCloudEntry.OAuthToken

    Authentication Precedence

    GrafanaConfig.AuthMethod is the authoritative setting. However, if the GRAFANA_TOKEN environment variable is provided and non-blank, it will override the method for that specific invocation to use a service-account token. GRAFANA_PASSWORD can replace a password in an existing Basic auth context but does not trigger Basic auth mode on its own.

  10. Implement TypedCRUD for Resource Adapters

    main

    To avoid manual marshal/unmarshal boilerplate, use TypedCRUD[T] to implement a ResourceAdapter. This requires your domain type T to implement the ResourceNamer interface (GetResourceName() string).

    The ResourceIdentity Contract: If your resource uses composite names (e.g., name-ID like grafana-instance-health-5594), your type must implement the ResourceIdentity interface to ensure round-trip support:

    • GetResourceName() string: Extracts the identity for metadata.name.
    • SetResourceName(name string): Crucial. Must parse and restore internal state (like numeric IDs) from the name string. If this fails, Update or Delete operations may fail because the ID is lost during the K8s round-trip.