Menu
Grafana Cloud

HCP Consul integration for Grafana Cloud

Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality.

Consul is split into a client-server architecture by which servers provide a control plane into the cluster of clients, and clients form a mostly stateless data plane alongside the sidecar proxy on each node running your applications and services.

HCP Consul specifically is a service provided by Hashicorp, by which they run and provide management software for your Consul cluster servers and related infrastructure. On your own infrastructure, you are required to only deploy and manage network peering, and the client and sidecar instances.

Since Consul provides first-class support for Envoy as a sidecar proxy, it is strongly recommended that you also install the Grafana integration for Envoy, assuming that you are using it.

This integration includes 3 useful alerts and 1 pre-built dashboard to help monitor and visualize HCP Consul metrics.

Grafana Alloy configuration

Before you begin

  1. In HCP Cloud, set up a Consul cluster.
  2. Determine your usage scenario and potentially set up an HCP Consul ACL token accordingly. See this tutorial from HCP for further details on how to set up ACLs for your Consul instance.
    • You can choose to use the consul_exporter configuration in the integrations Grafana Alloy configuration block, without an ACL token. This will give you access to metrics from Consul’s Status API, which is enough to populate all of the integration dashboard panels.
    • If you want the additional metrics provided, beyond what the integration dashboard supports, you can choose to additionally provide a scrape_config entry in the metrics Grafana Alloy configuration block that targets Consul’s Prometheus-compatible Agent Telemetry API. This requires an ACL token with agent:read privileges to be specified in the scrape_config entry.
    • If you want the additional metrics provided, beyond what the integration dashboard supports, you can choose to additionally provide one or more scrape_config entries in the metrics Grafana Alloy configuration block that targets your own services running on HCP Consul. This step assumes that you are using Envoy as a sidecar proxy. This requires an ACL token with node:read,service:read privileges to be specified in the scrape_configs entry in order to use Consul SD to find nodes running your service via the Catalog API.
  3. Install the correct Consul dataplane and connect it to HCP by following this official guide.
  4. Per this Consul documentation, you will need to add the following telemetry snippet to your Consul agent config. This will enable a Prometheus-compatible metrics scraping endpoint on the Consul client.
    "telemetry": {
        "prometheus_retention_time": "72h",
        "disable_hostname": true
    }
  5. If you want to expose Prometheus-compatible metrics for your services running on HCP Consul + Envoy, ensure the following snippets are present in each desired Consul service definition. See this documentation for more details.
    service {
    
    ...
    
        tags = [
            "<your_service_name>"
        ]
    
        meta {
            prometheus_port = "<desired_prometheus_port>"
        }
    
    ...
    
        connect {
            sidecar_service {
                proxy {
                    config {
                        envoy_prometheus_bind_addr = "0.0.0.0:<desired_prometheus_port>"
                    }
                }
            } 
        }
    }

Install HCP Consul integration for Grafana Cloud

  1. In your Grafana Cloud stack, click Connections in the left-hand menu.
  2. Find HCP Consul and click its tile to open the integration.
  3. Review the prerequisites in the Configuration Details tab and set up Grafana Agent to send HCP Consul metrics to your Grafana Cloud instance.
  4. Click Install to add this integration’s pre-built dashboard and alerts to your Grafana Cloud instance, and you can start monitoring your HCP Consul setup.

Configuration snippets for Grafana Alloy

Simple mode

These snippets are configured to scrape a single HCP Consul instance running locally with default ports.

First, manually copy and append the following snippets into your alloy configuration file.

Integrations snippets

river
prometheus.exporter.consul "integrations_consul_exporter" {
	server = "localhost:8500"
}

discovery.relabel "integrations_consul_exporter" {
	targets = prometheus.exporter.consul.integrations_consul_exporter.targets

	rule {
		target_label = "instance"
		replacement  = constants.hostname
	}

	rule {
		target_label = "job"
		replacement  = "integrations/consul_exporter"
	}
}

prometheus.scrape "integrations_consul_exporter" {
	targets    = discovery.relabel.integrations_consul_exporter.output
	forward_to = [prometheus.remote_write.metrics_service.receiver]
	job_name   = "integrations/consul_exporter"
}

Advanced mode

The following snippets provide examples to guide you through the configuration process.

To instruct Grafana Alloy to scrape your HCP Consul instances, manually copy and append the snippets to your alloy configuration file, then follow subsequent instructions.

Advanced metrics snippets

river
discovery.consul "metrics_integrations_hcp_consul_service__service_name_" {
	token = "<hcp_consul_acl_token>"
	tags  = ["<service_name>"]
}

discovery.relabel "metrics_integrations_hcp_consul_service__service_name_" {
	targets = discovery.consul.metrics_integrations_hcp_consul_service__service_name_.targets

	rule {
		target_label = "instance"
		replacement  = constants.hostname
	}

	rule {
		target_label = "job"
		replacement  = "integrations/hcp-consul"
	}

	rule {
		source_labels = ["__meta_consul_service_address"]
		target_label  = "__address__"
		replacement   = "${1}:<desired_prometheus_port>"
	}
}

prometheus.scrape "metrics_integrations_integrations_hcp_consul" {
	targets = [{
		__address__ = "localhost:8500",
		cluster     = "<your-cluster-name>",
		instance    = constants.hostname,
	}]
	forward_to = [prometheus.remote_write.metrics_service.receiver]
	job_name   = "integrations/hcp-consul"
	params     = {
		format = ["prometheus"],
	}
	metrics_path = "/v1/agent/metrics"

	authorization {
		type        = "Bearer"
		credentials = "<hcp_consul_acl_token>"
	}
}

prometheus.scrape "metrics_integrations_hcp_consul_service__service_name_" {
	targets    = discovery.relabel.metrics_integrations_hcp_consul_service__service_name_.output
	forward_to = [prometheus.remote_write.metrics_service.receiver]
	job_name   = "hcp-consul-service-<service_name>"
}

To monitor your HCP Consul instance, you must use a discovery.relabel component to discover your HCP Consul Prometheus endpoint and apply appropriate labels, followed by a prometheus.scrape component to scrape it.

Configure the following properties within each discovery.relabel component:

  • __address__: The address to your HCP Consul Prometheus metrics endpoint.
  • instance label: constants.hostname sets the instance label to your Grafana Alloy server hostname. If that is not suitable, change it to a value uniquely identifies this HCP Consul instance.

If you have multiple HCP Consul servers to scrape, configure one discovery.relabel for each and scrape them by including each under targets within the prometheus.scrape component.

Advanced integrations snippets

river
prometheus.exporter.consul "integrations_consul_exporter" {
	server = "localhost:8500"
}

discovery.relabel "integrations_consul_exporter" {
	targets = prometheus.exporter.consul.integrations_consul_exporter.targets

	rule {
		target_label = "instance"
		replacement  = constants.hostname
	}

	rule {
		target_label = "job"
		replacement  = "integrations/consul_exporter"
	}
}

prometheus.scrape "integrations_consul_exporter" {
	targets    = discovery.relabel.integrations_consul_exporter.output
	forward_to = [prometheus.remote_write.metrics_service.receiver]
	job_name   = "integrations/consul_exporter"
}

This integrations uses the prometheus.exporter.consul component to generate metrics from a HCP Consul instance.

For the full array of configuration options, refer to the prometheus.exporter.consul component reference documentation.

This exporter must be linked with a discovery.relabel component to apply the necessary relabelings.

For each HCP Consul instance to be monitored you must create a pair of these components.

Configure the following properties within each discovery.relabel component:

  • instance label: constants.hostname sets the instance label to your Grafana Alloy server hostname. If that is not suitable, change it to a value uniquely identifies this HCP Consul instance.

You can then scrape them by including each discovery.relabel under targets within the prometheus.scrape component.

Grafana Agent configuration

Before you begin

  1. In HCP Cloud, set up a Consul cluster.
  2. Determine your usage scenario and potentially set up an HCP Consul ACL token accordingly. See this tutorial from HCP for further details on how to set up ACLs for your Consul instance.
    • You can choose to use the consul_exporter configuration in the integrations Grafana Agent configuration block, without an ACL token. This will give you access to metrics from Consul’s Status API, which is enough to populate all of the integration dashboard panels.
    • If you want the additional metrics provided, beyond what the integration dashboard supports, you can choose to additionally provide a scrape_config entry in the metrics Grafana Agent configuration block that targets Consul’s Prometheus-compatible Agent Telemetry API. This requires an ACL token with agent:read privileges to be specified in the scrape_config entry.
    • If you want the additional metrics provided, beyond what the integration dashboard supports, you can choose to additionally provide one or more scrape_config entries in the metrics Grafana Agent configuration block that targets your own services running on HCP Consul. This step assumes that you are using Envoy as a sidecar proxy. This requires an ACL token with node:read,service:read privileges to be specified in the scrape_configs entry in order to use Consul SD to find nodes running your service via the Catalog API.
  3. Install the correct Consul dataplane and connect it to HCP by following this official guide.
  4. Per this Consul documentation, you will need to add the following telemetry snippet to your Consul agent config. This will enable a Prometheus-compatible metrics scraping endpoint on the Consul client.
    "telemetry": {
        "prometheus_retention_time": "72h",
        "disable_hostname": true
    }
  5. If you want to expose Prometheus-compatible metrics for your services running on HCP Consul + Envoy, ensure the following snippets are present in each desired Consul service definition. See this documentation for more details.
    service {
    
    ...
    
        tags = [
            "<your_service_name>"
        ]
    
        meta {
            prometheus_port = "<desired_prometheus_port>"
        }
    
    ...
    
        connect {
            sidecar_service {
                proxy {
                    config {
                        envoy_prometheus_bind_addr = "0.0.0.0:<desired_prometheus_port>"
                    }
                }
            } 
        }
    }

Install HCP Consul integration for Grafana Cloud

  1. In your Grafana Cloud stack, click Connections in the left-hand menu.
  2. Find HCP Consul and click its tile to open the integration.
  3. Review the prerequisites in the Configuration Details tab and set up Grafana Agent to send HCP Consul metrics to your Grafana Cloud instance.
  4. Click Install to add this integration’s pre-built dashboard and alerts to your Grafana Cloud instance, and you can start monitoring your HCP Consul setup.

Post-install configuration for the HCP Consul integration

This integration is configured to work with the consul_exporter, which is embedded in Grafana Agent.

Enable the integration by adding the provided snippets to your agent configuration file.

Make sure to change the server address to the address of your Consul instance that you want to monitor.

For a full description of configuration options see how to configure the consul_exporter_config block in the agent documentation.

Configuration snippets for Grafana Agent

Below integrations, insert the following lines and change the URLs according to your environment:

yaml
  consul_exporter:
    enabled: true
    server: localhost:8500
    instance: '<your-instance-name>'

Below metrics.configs.scrape_configs, insert the following lines and change the URLs according to your environment:

yaml
    - job_name: 'integrations/hcp-consul'
      metrics_path: /v1/agent/metrics
      params:
        format: ["prometheus"]
      authorization:
        credentials: '<hcp_consul_acl_token>'
      scrape_interval: 60s
      static_configs:
        - targets: ['localhost:8500']
          labels:
            cluster: '<your-cluster-name>'
            instance: '<your-instance-name>'
    - job_name: 'hcp-consul-service-<service_name>'
      metrics_path: /metrics
      consul_sd_configs:
        - server: 'localhost:8500'
          token: '<hcp_consul_acl_token>'
          tags:
          - '<service_name>'
      relabel_configs:
        - target_label: 'instance'
          replacement: '<your-instance-name>'
        - target_label: 'job'
          replacement: 'integrations/hcp-consul'
        - action: 'replace'
          source_labels: [__meta_consul_service_address]
          regex: '(.*)'
          replacement: '${1}:<desired_prometheus_port>'
          target_label: '__address__'

Full example configuration for Grafana Agent

Refer to the following Grafana Agent configuration for a complete example that contains all the snippets used for the HCP Consul integration. This example also includes metrics that are sent to monitor your Grafana Agent instance.

yaml
integrations:
  prometheus_remote_write:
  - basic_auth:
      password: <your_prom_pass>
      username: <your_prom_user>
    url: <your_prom_url>
  agent:
    enabled: true
    relabel_configs:
    - action: replace
      source_labels:
      - agent_hostname
      target_label: instance
    - action: replace
      target_label: job
      replacement: "integrations/agent-check"
    metric_relabel_configs:
    - action: keep
      regex: (prometheus_target_sync_length_seconds_sum|prometheus_target_scrapes_.*|prometheus_target_interval.*|prometheus_sd_discovered_targets|agent_build.*|agent_wal_samples_appended_total|process_start_time_seconds)
      source_labels:
      - __name__
  # Add here any snippet that belongs to the `integrations` section.
  # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
  consul_exporter:
    enabled: true
    server: localhost:8500
    instance: '<your-instance-name>'
logs:
  configs:
  - clients:
    - basic_auth:
        password: <your_loki_pass>
        username: <your_loki_user>
      url: <your_loki_url>
    name: integrations
    positions:
      filename: /tmp/positions.yaml
    scrape_configs:
      # Add here any snippet that belongs to the `logs.configs.scrape_configs` section.
      # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
metrics:
  configs:
  - name: integrations
    remote_write:
    - basic_auth:
        password: <your_prom_pass>
        username: <your_prom_user>
      url: <your_prom_url>
    scrape_configs:
      # Add here any snippet that belongs to the `metrics.configs.scrape_configs` section.
      # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
    - job_name: 'integrations/hcp-consul'
      metrics_path: /v1/agent/metrics
      params:
        format: ["prometheus"]
      authorization:
        credentials: '<hcp_consul_acl_token>'
      scrape_interval: 60s
      static_configs:
        - targets: ['localhost:8500']
          labels:
            cluster: '<your-cluster-name>'
            instance: '<your-instance-name>'
    - job_name: 'hcp-consul-service-<service_name>'
      metrics_path: /metrics
      consul_sd_configs:
        - server: 'localhost:8500'
          token: '<hcp_consul_acl_token>'
          tags:
          - '<service_name>'
      relabel_configs:
        - target_label: 'instance'
          replacement: '<your-instance-name>'
        - target_label: 'job'
          replacement: 'integrations/hcp-consul'
        - action: 'replace'
          source_labels: [__meta_consul_service_address]
          regex: '(.*)'
          replacement: '${1}:<desired_prometheus_port>'
          target_label: '__address__'
  global:
    scrape_interval: 60s
  wal_directory: /tmp/grafana-agent-wal

Dashboards

The HCP Consul integration installs the following dashboards in your Grafana Cloud instance to help monitor your system.

  • Consul Overview

Alerts

The HCP Consul integration includes the following useful alerts:

AlertDescription
ConsulUpCritical: Consul is not up.
ConsulMasterCritical: Consul has no master.
ConsulPeersCritical: Consul does not have peers.

Metrics

The most important metrics provided by the HCP Consul integration, which are used on the pre-built dashboard and Prometheus alerts, are as follows:

  • consul_raft_leader
  • consul_raft_leader_lastcontact_count
  • consul_raft_peers
  • consul_up
  • up

Changelog

md
# 0.0.2 - September 2023

* New Filter Metrics option for configuring the Grafana Agent, which saves on metrics cost by dropping any metric not used by this integration. Beware that anything custom built using metrics that are not on the snippet will stop working.
* New hostname relabel option, which applies the instance name you write on the text box to the Grafana Agent configuration snippets, making it easier and less error prone to configure this mandatory label.

# 0.0.1 - March 2023

* Initial release

Cost

By connecting your HCP Consul instance to Grafana Cloud, you might incur charges. To view information on the number of active series that your Grafana Cloud account uses for metrics included in each Cloud tier, see Active series and dpm usage and Cloud tier pricing.