Deliver Prometheus data to ApsaraMQ for Kafka

Updated at:

To export data from a Prometheus instance for custom processing, use the data delivery service of Managed Service for Prometheus. This topic describes how to create a data delivery task to send instance data to ApsaraMQ for Kafka.

Prerequisites

Limits

  • If the Prometheus instance and the destination reside in different VPCs, add the destination vSwitch CIDR block to the Prometheus instance allowlist to avoid network connection failures.

  • Supported data sources:

    Instance type

    Description

    Prometheus for Cloud Services

    Excludes free instances whose names start with cloud-product-prometheus.

    Prometheus for Container Service

    None

    Prometheus for Flink Serverless

    None

    Prometheus for Kubernetes

    None

    General-purpose

    Excludes data reported through OpenTelemetry endpoints.

  • Only real-time data generated after task creation is shipped; historical data is not supported.

Step 1: Create a data delivery task

  1. Log on to the Cloud Monitor console.

  2. In the left-side navigation pane, click Prometheus Monitoring > Data Shipping.

  3. On the Data Shipping page, select a destination region in the top menu bar and click Create Task.

  4. In the dialog box that appears, enter a Task Name and Task description, and click OK.

  5. On the Edit Task page, configure the data source and destination.

    1. Click + Add Data Source, configure the following parameters, and then click OK.

      Parameter

      Description

      Example

      Prometheus Instance

      The source Prometheus instance.

      c78cb8273c02*****

      Data Filtering

      Filter data by metric labels. Supports regular expressions. Separate multiple conditions with line breaks (AND logic).

      __name__=AliyunEcs_CPUUtilization|AliyunEcs_memory_usedutilization
      regionId=cn-hangzhou
      id=i-2ze0mxp.*

      Data Labeling

      Additional labels for shipped metric data. Separate with line breaks.

      deliver_test_key1=ssss
      deliver_test_key2=yyyy
    2. Click Add Target, set Target Type to ApsaraMQ for Kafka, configure the remaining parameters, and then click OK.

  6. On the Edit Task page, click OK, and then click Save to create the data shipping task.

Step 2: View Prometheus monitoring data

Note

To reduce the load on the target, the service delivers Prometheus monitoring data to Kafka as a JSON array compressed by using Snappy. For more information, see Snappy compression format.

View in console

  1. Log on to the ApsaraMQ for Kafka console.

  2. On the Overview page, select a region in the Resource Distribution section.

  3. On the Instances page, click the name of the target instance.

  4. In the navigation pane on the left, click Topics. Find the target topic and click Details in the Actions column. You can then view the imported data on the CloudMonitor or Message Query tab.

View with client

  1. Initialize a Kafka client. For more information, see Use a single consumer to subscribe to messages.

  2. Add the following code to the KafkaConsumerDemo.java file. The code consumes data and decompresses it by using Snappy after the Kafka client is initialized:

    public static void main(String[] args) {
    
            // Initialize the Kafka consumer first.
            
            while (true){
                try {
                    ConsumerRecords<String, byte[]> records = consumer.poll(1000);
                    // You must consume the records before the next poll, and the total time must not exceed SESSION_TIMEOUT_MS_CONFIG.
                    // We recommend that you use a dedicated thread pool to consume messages and return results asynchronously.
                    for (ConsumerRecord<String, byte[]> record : records) {
                        byte[] compressedData = record.value();
                        byte[] data = Snappy.uncompress(compressedData);
                        
                        System.out.println(new String(data));
                    }
                } catch (Exception e) {
                    try {
                        Thread.sleep(1000);
                    } catch (Throwable ignore) {
    
                    }
    
                    e.printStackTrace();
                }
            }
    }
  3. Compile and run the KafkaConsumerDemo.java file. The metric data is output in the following JSON format.

    [{
    	"instance": "*****",
    	"pod": "*****",
    	"rejected": "false",
    	"type": "validate",
    	"pod_name": "*****",
    	"endpoint": "http-metrics",
    	"__name__": "apiserver_admission_controller_admission_duration_seconds_bucket",
    	"service": "kubernetes",
    	"name": "*****",
    	"namespace": "default",
    	"le": "2.5",
    	"job": "apiserver",
    	"operation": "UPDATE",
    	"value": "675.0",
    	"timestamp": "1698732988354"
    }, {
    	"instance": "*****",
    	"pod": "*****",
    	"rejected": "false",
    	"type": "validate",
    	"pod_name": "*****",
    	"endpoint": "http-metrics",
    	"__name__": "apiserver_admission_controller_admission_duration_seconds_bucket",
    	"service": "kubernetes",
    	"name": "*****",
    	"namespace": "default",
    	"le": "+Inf",
    	"job": "apiserver",
    	"operation": "UPDATE",
    	"value": "675.0",
    	"timestamp": "1698732988354"
    }, {
    	"instance": "*****",
    	"pod": "*****",
    	"rejected": "false",
    	"type": "validate",
    	"pod_name": "*****",
    	"endpoint": "http-metrics",
    	"__name__": "apiserver_admission_controller_admission_duration_seconds_bucket",
    	"service": "kubernetes",
    	"name": "*****",
    	"namespace": "default",
    	"le": "0.005",
    	"job": "apiserver",
    	"operation": "UPDATE",
    	"value": "1037.0",
    	"timestamp": "1698732988519"
    }, {
    	"instance": "*****",
    	"pod": "*****",
    	"rejected": "false",
    	"type": "validate",
    	"pod_name": "*****",
    	"endpoint": "http-metrics",
    	"__name__": "apiserver_admission_controller_admission_duration_seconds_bucket",
    	"service": "kubernetes",
    	"name": "*****",
    	"namespace": "default",
    	"le": "0.025",
    	"job": "apiserver",
    	"operation": "UPDATE",
    	"value": "1037.0",
    	"timestamp": "1698732988519"
    }]