Prometheus Federated Queries with Thanos

Prometheus is a popular open-source monitoring and alerting tool that allows you to collect and analyze time-series data from various sources. One of the key features of Prometheus is its ability to perform federated queries, which allow you to query data from multiple Prometheus instances and aggregate the results.

However, federated queries can be resource-intensive and may not scale well for large deployments. This is where Thanos comes in. Thanos is an open-source log aggregation and analysis system that provides long-term storage and querying capabilities for Prometheus metrics. By using Thanos, you can offload the work of federated queries to dedicated Thanos components, and improve the performance and scalability of your Prometheus deployment.

In this blog post, we will explore how to use Thanos for federated queries with Prometheus.

Thanos consists of several components, including the Thanos Query component, the Thanos Sidecar component, and the Thanos Store Gateway component. The Thanos Query component is responsible for querying and aggregating data from multiple Prometheus instances, while the Thanos Sidecar component is responsible for storing and retrieving data from object storage systems such as Amazon S3 or Google Cloud Storage. The Thanos Store Gateway component is responsible for providing a unified interface for querying data from multiple object storage systems.

To use Thanos for federated queries with Prometheus, you will need to deploy the Thanos components in your environment. This can be done using Kubernetes, Docker, or any other deployment tool. Once the Thanos components are deployed, you can configure your Prometheus instances to send data to the Thanos Sidecar component.

Here is an example of a Prometheus configuration file that sends data to the Thanos Sidecar component:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

remote_write:
  - url: http://thanos-sidecar.monitoring:9090/api/v1/write

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

This configuration file specifies the URL of the Thanos Sidecar component using the remote_write section. The scrape_configs section specifies the targets to scrape, which in this case is the local Prometheus instance.

Once the Prometheus instances are configured to send data to the Thanos Sidecar component, you can use the Thanos Query component to query and aggregate data from multiple Prometheus instances. The following command will query the http_requests_total metric from all Prometheus instances:

thanos query --store=thanos-store.monitoring:10901 --head-template='<h1>{{ range .LabelNames }}{{ . }} = {{ index .LabelValues 0 }}{{ end }}</h1>' 'sum(rate(http_requests_total[5m])) by (job)'

This command specifies the URL of the Thanos Store Gateway component using the --store flag, and the query to execute using the single quotes. The --head-template flag is used to specify the HTML template for the query results.

The Thanos Query component supports a wide range of query functions and operators, including sum, avg, min, max, and group_by. You can use these functions and operators to perform complex queries and aggregations on your data.

In addition to querying data, Thanos also provides long-term storage and retention capabilities for Prometheus metrics. By default, Prometheus stores data for 15 days, but this can be extended using Thanos. The Thanos Sidecar component is responsible for storing data in object storage systems, while the Thanos Store Gateway component is responsible for providing a unified interface for querying data from multiple object storage systems.

Here is an example of a Thanos configuration file that configures the Thanos Sidecar component to store data in Amazon S3:

sidecar:
  log_level: debug
  prometheus:
    url: http://prometheus:9090
  tsdb:
    path: /data
    retention: 30d
    wal_retention: 7d
    upload:
      concurrency: 10
      retry_delay: 1m
      s3:
        bucket: thanos-bucket
        prefix: thanos/
        region: us-west-2
        access_key: <access_key>
        secret_key: <secret_key>

This configuration file specifies the URL of the Prometheus instance using the prometheus.url setting, and the path to the TSDB data directory using the tsdb.path setting. The tsdb.retention setting specifies the retention period for the data, while the tsdb.wal\_retention setting specifies the retention period for the write-ahead log (WAL). The tsdb.upload section specifies the configuration for uploading data to Amazon S3, including the bucket name, prefix, region, and access and secret keys.

Once the Thanos Sidecar component is configured to store data in Amazon S3, you can use the Thanos Query component to query data from multiple object storage systems. The following command will query the http\_requests\_total metric from all object storage systems:

thanos query --store=thanos-store.monitoring:10901 --head-template='<h1>{{ range .LabelNames }}{{ . }} = {{ index .LabelValues 0 }}{{ end }}</h1>' 'sum(rate(http_requests_total[5m])) by (job)'

This command specifies the URL of the Thanos Store Gateway component using the --store flag, and the query to execute using the single quotes. The --head-template flag is used to specify the HTML template for the query results.

In conclusion, Thanos is a powerful log aggregation and analysis system that provides long-term storage and querying capabilities for Prometheus metrics. By using Thanos for federated queries with Prometheus, you can offload the work of federated queries to dedicated Thanos components, and improve the performance and scalability of your Prometheus deployment.

Here are some best practices for using Thanos for federated queries with Prometheus:

  • Use the Thanos Query component to query and aggregate data from multiple Prometheus instances.

  • storage systems such as Amazon S3 or Google Cloud Storage.

  • Use the Thanos Store Gateway component to provide a unified interface for querying data from multiple object storage systems.

  • Use the --store flag to specify the URL of the Thanos Store Gateway component when querying data.

  • Use the --head-template flag to specify the HTML template for the query results.

  • Use the remote_write section in the Prometheus configuration file to specify the URL of the Thanos Sidecar component.

  • Use the tsdb.retention setting in the Thanos configuration file to specify the retention period for the data.

  • Use the tsdb.wal_retention setting in the Thanos configuration file to specify the retention period for the write-ahead log (WAL).

  • Use the tsdb.upload section in the Thanos configuration file to specify the configuration for uploading data to object storage systems.

    By following these best practices, you can ensure that your Thanos deployment is robust and scalable, and that you can effectively query and analyze data from multiple Prometheus instances.

    Here is an example of a Thanos configuration file that configures the Thanos Query component to query data from multiple Prometheus instances:

      query:
        log_level: debug
        http:
          address: 0.0.0.0:10902
        query_range:
          start: 24h
          end: 0s
          step: 15m
        query_raw:
          max_samples: 10000
        query_log:
          max_samples: 10000
        query_relabel:
          actions:
            - action: labelmap
              regex: __meta_prometheus_job(.*)
              replacement: $1
            - action: labeldrop
              regex: __meta_prometheus_job
        query_replica_label: __replica__
        query_grpc_addresses:
          - thanos-query.monitoring:10902
    

    This configuration file specifies the address and port of the Thanos Query component using the http.address setting, and the range and step for querying data using the query_range section. The query_raw and query_log sections specify the maximum number of samples to return for raw and log queries, respectively. The query_relabel section specifies the relabel actions to perform on the data, including mapping and dropping labels. The query_replica_label setting specifies the label to use for replica queries, and the query_grpc_addresses setting specifies the addresses of the Thanos Query components to query.

    Once the Thanos Query component is configured, you can use it to query data from multiple Prometheus instances. The following command will query the http_requests_total metric from all Prometheus instances:

      thanos query --store=thanos-store.monitoring:10901 --head-template='<h1>{{ range .LabelNames }}{{ . }} = {{ index .LabelValues 0 }}{{ end }}</h1>' 'sum(rate(http_requests_total[5m])) by (job)'
    

    This command specifies the URL of the Thanos Store Gateway component using the --store flag, and the query to execute using the single quotes. The --head-template flag is used to specify the HTML template for the query results.

    In conclusion, Thanos is a powerful log aggregation and analysis system that provides long-term storage and querying capabilities for Prometheus metrics. By using Thanos for federated queries with Prometheus, you can offload the work of federated queries to dedicated Thanos components, and improve the performance and scalability of your Prometheus deployment. By following the best practices outlined in this blog post, you can ensure that your Thanos deployment is robust and scalable, and that you can effectively query and analyze data from multiple Prometheus instances.

0
Subscribe to my newsletter

Read articles from Platform Engineers directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Platform Engineers
Platform Engineers

In today's global arena, secure & scalable platforms are mission-critical. Platform engineers design, build, and manage resilient infrastructure & tools for your software applications. We deliver enhanced security, fault tolerance, and elastic scalability, perfectly aligned with your business objectives.