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

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Monitoring application alone không phải lúc nào cũng đủ để có full picture của systems. Thường xuyên, services chạy trong satellite apps (hoặc supporting apps) có thể có acute impact trên day-to-day operations. Database servers là well-known examples. Backup scripts và other background jobs cũng có thể slow systems và thường bị overlooked.

AppSignal APM cho Node.js, Ruby APM và Elixir APM tự động instrument app itself. Nhưng AppSignal không watch satellite processes này by default. Để extend monitoring everywhere và có all data trong single app, bạn có thể cài đặt AppSignal's standalone agent.

Standalone Agent Của AppSignal

Standalone agent dựa trên same software mà chúng ta thường dùng để instrument Ruby, Elixir, hoặc JavaScript applications. Software này cũng có thể run ở standalone mode.

Standalone agent có thể dùng để monitor:

  • Infrastructure: machines là part của system nhưng không chạy application code.
  • Background jobs: như intensive cron jobs hoặc long-running data-processing scripts. Nếu background jobs viết bằng supported languages (Ruby, Elixir, hoặc Node.js) bạn có thể dùng standard integration.
  • More languages: programs viết bằng languages khác ngoài những ngôn ngữ supported out of the box.

Ví dụ, với standalone agent, chúng ta có thể track machine learning model viết bằng Python, instrument backup scripts, monitor Kafka brokers, hoặc collect host metrics trong web farms. Bạn có thể view all thông tin này trên AppSignal để complement metrics đã có cho main applications.

Cách Hoạt Động

Agent được ship as deb hoặc rpm package và không có language dependencies. Nó chạy trên bất kỳ Debian/Ubuntu hoặc Red Hat-based systems. Cho detailed installation instructions, check agent documentation.

Khi installed, agent configured trong vài minutes và chạy forever as daemon, silently monitoring infrastructure. Thêm vào đó, agent include StatsD server relay bất kỳ custom data bạn log vào AppSignal dashboard.

StatsD Là Gì?

StatsD là standard cho collecting và aggregating arbitrary data. Nó focus trên logging metric và performance information. Nó dùng lightweight text protocol over UDP connections có small footprint trong machine.

Một StatsD message trông như sau:

KEY:VALUE|TYPE

Nơi KEY là arbitrary string bất kỳ, và VALUE là number. Type value define cách number được processed.

Chúng tôi support ba loại metrics:

  • c: counter increment mỗi lần được gọi. Ví dụ, active_users:1|c thêm 1 vào counter active_users.
  • g: gauge lấy numeric value và maintain nó cho đến khi updated. Useful để record values thay đổi up/down theo thời gian, như throughput, số active users, hoặc số pending tasks trong queue.
  • t: lưu timing values. Type này ideal cho tracking durations. AppSignal calculates means, count, và percentiles cho all logged timings.

Other unsupported metric types sẽ bị silently ignored.

Gửi Data Đến StatsD

Standalone agent listen cho UDP packets trên port 8125. Chúng ta có thể gửi StatsD-formatted strings từ command line dùng netcat:

echo "my.metric:1|c" | nc -u -w0 127.0.0.1 8125

Vì dùng UDP, chúng ta không phải wait for response.

Socat cũng works:

echo "my.metric:1|c" | socat -u STDIN UDP-DATAGRAM:127.0.0.1:8125

Điều này làm cho việc instrument bất kỳ batch hoặc cron jobs trở nên dễ dàng. Ví dụ, các dòng sau dùng gauge để log data lượng backup job generated:

#!/bin/bash
BACKUP_SIZE=$(du -sb /backup | cut -f1)
echo "backup.size:$BACKUP_SIZE|g" | nc -u -w0 127.0.0.1 8125

Chúng ta không limited to integers. StatsD cũng works với floating-point numbers:

echo "response.time:123.45|t" | nc -u -w0 127.0.0.1 8125

Bạn có thể add tags vào metrics. StatsD server support optional tags ở cuối message:

KEY:VALUE|TYPE|#TAG_KEY:TAG_VALUE

Chúng ta có thể apply several tags trong same message và assign values cho filtering sau:

echo "http.requests:1|c|#endpoint:users,method:GET" | nc -u -w0 127.0.0.1 8125

Chúng ta sẽ học cách xem data trong AppSignal trong chút.

Instrumenting Languages (Các Ngôn Ngữ)

StatsD server compatible với bất kỳ language nào có thể send UDP packets. Hãy xem vài ví dụ.

Python là popular language cho data mining và machine learning. Chúng ta có thể add instrumentation vào Python applications dùng built-in socket library:

import socket
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(b"python.job.duration:1.23|t", ("127.0.0.1", 8125))

Chúng ta có thể achieve similar results trong Java với java.net library:

DatagramSocket socket = new DatagramSocket();
byte[] buf = "java.job.duration:1.23|t".getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 8125);
socket.send(packet);

PHP là example khác của incredibly popular language. Chúng ta có thể send UDP packets với socket_sendto:

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, "php.job.duration:1.23|t", strlen("php.job.duration:1.23|t"), 0, "127.0.0.1", 8125);

StatsD Clients

Cho đến nay, chúng ta dùng built-in networking capabilities trong mọi language. Nhưng còn nhiều hơn. Nhiều languages và products include third-party StatsD clients hoặc addons. Bất kỳ StatsD-compliant client nào nên work, ít nhất cho supported data types. Bạn có thể tìm list of clients tại StatsD project wiki.

Xem Host Data Trong AppSignal

Host Metrics dashboard hiển thị resource utilization của machine:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

AppSignal thêm entry cho every machine chạy standalone agent.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Dashboard hiển thị load và CPU averages, disk, network, memory, và swap usage:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Tạo Dashboards

AppSignal không automatically generate dashboards cho StatsD values bạn gửi — bạn cần create custom dashboards cho việc này.

Đầu tiên, click Add dashboard dưới dashboard menu:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Đặt tên cho dashboard:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Click Add graph button hiển thị graph builder:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Đặt tên cho graph, và click Add metric:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Chọn metric bạn quan tâm từ menu.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Tùy chọn, dùng tags cho filtering:

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Tiếp, click Back to overview. Bạn có thể thử different graph types và value units để tìm cái best suit data bạn muốn represent.

Timing data trông tốt hơn với area graphs vì mean và percentiles hiển thị rõ ràng hơn.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Line graphs work great cho counters và gauges.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Khi hài lòng với kết quả, click Create graph.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Lưu ý bạn cũng có thể add dashboard cho bất kỳ automatically instrumented hoặc measured metrics nào nếu bạn có AppSignal setup as APM cho Node.js, Ruby (on Rails) APM, hoặc Elixir APM.

Tóm Lại

Chúng ta đã học cách dùng AppSignal's standalone server để watch machines và satellite code. Với built-in StatsD server, bạn có thể record arbitrary performance data và instrument bất kỳ process nào.

Check các links sau để learn more về dùng standalone agent:

  • StatsD reference.
  • How to send custom metrics.
  • Creating dashboards in AppSignal.
  • Identifying and Resolving a Kafka Issue the Standalone Agent.

Nâng Cao Infrastructure Visibility: Monitoring Bất Kỳ Hệ Thống Nào Với StatsD & Standalone Agent Của AppSignal

Tomas Fernandez

Tác giả khách Tomas spent 10 years làm việc tại IBM, nơi anh làm bit of everything - development, service delivery, database administration, và cloud engineering. Giờ anh là independent consultant và technical writer.

Tất cả bài viết của Tomas Fernandez