Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Redis

Làm chủ Redis Data Source Plug-in cho Grafana: Hướng dẫn Step-by-Step

Tháng này, Redis phát hành Redis Data Source for Grafana plug-in mới, kết nối open source application monitoring tool phổ biến đến Redis. Để hiểu cách hoạt động, xem ví dụ self-referential: dùng plug-in xem nó được download bao nhiêu lần theo thời gian. (Grafana plug-in repository không cung cấp statistics này out of the box.)

Muốn tìm hiểu thêm? Đọc Introducing the Redis Data Source Plug-in for Grafana

Redis Data Source for Grafana là gì?

Nếu không quen Grafana, đó là tool phổ biến build dashboard monitor application, infrastructure, software component. Redis Data Source for Grafana là plug-in cho phép user kết nối Redis database và build dashboard Grafana dễ dàng monitor Redis data. Cung cấp predefined dashboard out-of-the-box, cũng cho phép build customized dashboard tuned cho nhu cầu cụ thể.

Làm chủ Redis Data Source Plug-in cho Grafana: Hướng dẫn Step-by-Step

Redis Data Source for Grafana plug-in cài đặt bằng grafana-cli, Docker, hoặc Grafana Cloud. Hoặc build từ source theo hướng dẫn GitHub.

grafana-cli plugins install redis-datasource

Prerequisites

Demo này dùng:

  • Node.js 10.x với npm
  • Docker
  • Docker Compose
  • Redis 5.0.x hoặc mới hơn với RedisTimeSeries (demo dùng Redis Docker container)
  • Grafana 7.0 hoặc mới hơn với Redis Data Source (demo dùng Grafana Docker container)

Cách lấy thông tin Grafana plug-in

Thông tin bất kỳ plug-in registered trong Grafana repository lấy được qua API JSON format:

GET https://grafana.com/api/plugins/redis-datasource/versions/latest
{
  "id": 2613,
  "pluginId": 639,
  "pluginSlug": "redis-datasource",
  "version": "1.1.2",
  "url": "https://github.com/RedisTimeSeries/grafana-redis-datasource/",
  ...
  "downloads": 1153,
  "verified": false,
  "status": "active",
  "packages": {
    "any": {
      "md5": "ea0a2c9cb11c9fad66703ba4291e61cb",
      "packageName": "any",
      "downloadUrl": "/api/plugins/undefined/versions/1.1.2/download"
    }
  },

Ví dụ này, muốn biết Redis Data Source for Grafana plug-in download bao nhiêu lần/ngày, tìm spike sau tweet hoặc post blog Redis. Quyết định dùng RedisTimeSeries (module thêm time-series data structure vào Redis) track downloads mỗi giờ.

Dùng lệnh TS.ADD với automatic timestamp và labels `plugin` và `version`. X là số download và latest version `1.1.2` từ API. Labels dùng sau query time series.

127.0.0.1:6379> ts.add redis-datasource * X LABELS plugin redis-datasource version 1.1.2

Viết script đơn giản dùng ioredis và Axios libraries call API và dùng plug-in info thêm time-series samples:

/** * A robust, performance-focused and full-featured Redis client for Node.js. * * @see https://github.com/luin/ioredis */ const Redis = require("ioredis"); /** * Promise based HTTP client for the browser and node.js * * @see https://github.com/axios/axios */ const axios = require("axios"); /** * You can also specify connection options as a redis:// URL or rediss:// URL when using TLS encryption */ const redis = new Redis("redis://localhost:6379"); /** * Main * * @async * @param {string} plugin Plugin name */ async function main(plugin) { /** * Get Plugin's data * * @see https://grafana.com/api/plugins/redis-datasource/versions/latest */ const response = await axios.get( `https://grafana.com/api/plugins/${plugin}/versions/latest` ); /** * Response */ const data = response.data; if (!data) { console.log("Where is the data?"); return; } /** * Add Time-series sample with plugin and version labels */ await redis.send_command( "TS.ADD", data.pluginSlug, "*", data.downloads, "LABELS", "plugin", data.pluginSlug, "version", data.version ); /** * Close Redis connection */ await redis.quit(); } /** * Start */ main("redis-datasource");

Môi trường script của tôi

Dùng package.json cài dependencies và chạy command dùng `npm`:

{ "author": "Mikhail Volkov", "dependencies": { "axios": "^0.19.2", "ioredis": "^4.17.3" }, "description": "Get statistics for Grafana Plugin", "devDependencies": { "@types/node": "^14.0.27" }, "license": "ISC", "name": "grafana-plugin-stats", "scripts": { "redis-cli": "docker exec -it redistimeseries redis-cli", "start": "docker-compose up", "start:app": "node grafana-plugin-stats.ts" }, "version": "1.0.0" }

Orchestrate Docker containers dùng docker-compose:

  • Redis service based on redis/redistimeseries image, có RedisTimeSeries module enabled.
  • Grafana service dùng latest Grafana release với Redis Data Source plug-in installed từ repository.
version: "3.4"
services:
  redis:
    container_name: redistimeseries
    image: redis/redistimeseries:latest
    ports:
      - 6379:6379
  grafana:
    container_name: grafana
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    environment:
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_BASIC_ENABLED=false
      - GF_ENABLE_GZIP=true
      - GF_INSTALL_PLUGINS=redis-datasource

Chạy script mỗi giờ collect download data, dùng crontab trên Linux server cloud:

root@grafana:~# crontab -l
5 * * * * node /root/grafana-plugin-stats/stats.ts

Test Redis Data Source for Grafana plug-in

Chạy script collect data, cần cài Node.js, Docker, Docker Compose, theo hướng dẫn OS:

> docker-compose up -d
Starting grafana ... done
Starting redistimeseries ... done
...
redistimeseries | 1:M 08 Aug 2020 21:13:20.405 * <timeseries> Redis version found by RedisTimeSeries : 6.0.1 - oss
...
grafana | installing redis-datasource @ 1.1.2
grafana | from: https://grafana.com/api/plugins/redis-datasource/versions/1.1.2/download
...
grafana | t=2020-08-08T21:13:23+0000 lvl=info msg="Registering plugin" logger=plugins name=Redis
grafana | t=2020-08-08T21:13:23+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=[::]:3000 protocol=http subUrl= socket=

Sau chạy script, check RedisTimeSeries data dùng TS.MRANGE. Query range across multiple time-series dùng filters forward hoặc reverse:

127.0.0.1:6379> ts.mrange - + withlabels filter plugin=redis-datasource
1) 1) "diff:redis-datasource"
   2) 1) 1) "value" 2) "diff"
      2) 1) "type" 2) "datasource"
      3) 1) "plugin" 2) "redis-datasource"
      4) 1) "version" 2) "1.1.2"
   3) 1) 1) (integer) 1597125602559 2) 0
      2) 1) (integer) 1597129202847 2) 1
      3) 1) (integer) 1597132802738 2) 10

Lệnh TS.MRANGE với filter `plugin` lấy samples chỉ cho `redis-datasource` plug-in. Option WITHLABELS trả về labels.

Cách hiển thị RedisTimeSeries data trong Grafana

Mở Grafana browser `https://localhost:3000` tạo data source chọn Configuration -> Data Sources. Redis Data Source for Grafana hỗ trợ TLS và kết nối open source Redis OSS, Redis Enterprise, Redis Enterprise Cloud bất kỳ đâu dùng direct connection.

Làm chủ Redis Data Source Plug-in cho Grafana: Hướng dẫn Step-by-Step

Bước tiếp: tạo dashboard với graph panel visualize data. Chọn "Redis Datasource" và "RedisTimeSeries commands" trong query editor. Dùng command TS.MRANGE với filter plug-in name.

Làm chủ Redis Data Source Plug-in cho Grafana: Hướng dẫn Step-by-Step

Cuối cùng, đặt plug-in Legend Labels và version làm Value Label, dễ display series cho version sau của Redis Data Source for Grafana.

Kiểm tra kết quả

Dùng TS.INFO xem info và statistics cho time series. Hiện tại collect download data 250 giờ, xem memory (bytes) allocated cho time-series và info khác.

127.0.0.1:6379> ts.info diff:redis-datasource
1) totalSamples 2) (integer) 250
3) memoryUsage 4) (integer) 4313
5) firstTimestamp 6) (integer) 1597125602559
7) lastTimestamp 8) (integer) 1598022003033
9) retentionTime 10) (integer) 0
11) chunkCount 12) (integer) 1
13) maxSamplesPerChunk 14) (integer) 256
15) labels 16) 1) 1) "value" 2) "diff"
      2) 1) "type" 2) "datasource"
      3) 1) "plugin" 2) "redis-datasource"
      4) 1) "version" 2) "1.1.2"
17) sourceKey 18) (nil)
19) rules 20) (empty list or set)

Thời điểm publish, Redis Data Source for Grafana plug-in downloaded >3500 lần! Nhận valuable feedback community và tiếp tục develop features mới cho data source.

Thêm info, xem GitHub repository project và cho biết questions trong issues.

Kết luận

Hy vọng bài post, và ví dụ dùng Redis Data Source for Grafana track downloads plug-in theo thời gian, demo power và ease of use tool mới này và inspire bạn monitor application data (transactions, streams, queues, etc.) dùng RedisTimeSeries. Stay tuned cho bài hơn về how và why dùng Redis Data Source for Grafana plug-in.