Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

Track Every System with StatsD & AppSignal's Standalone Agent

Monitor แค่แอปพลิเคชันอาจไม่พอให้ภาพเต็มของระบบ บ่อยครั้ง services ที่รันใน satellite apps (หรือ supporting apps) สามารถกระทบ acute impact ต่อ operations ประจำวัน Database servers เป็นตัวอย่างที่รู้จักดี Backup scripts และ background jobs อื่นๆ ก็ชะลอระบบและมักถูกมองข้าม

AppSignal APM สำหรับ Node.js, Ruby APM และ Elixir APM instrument แอปตัวเองอัตโนมัติ แต่ AppSignal ไม่ watch satellite processes นี้โดยค่าเริ่มต้น เพื่อขยาย monitoring ทุกที่และรวมข้อมูลทั้งหมดใน single app ติดตั้ง AppSignal's standalone agent

AppSignal's Standalone Agent

Standalone agent พัฒนาจากซอฟต์แวร์เดียวกับที่เรา instrument แอป Ruby, Elixir หรือ JavaScript ปกติ ซอฟต์แวร์นี้รันใน standalone mode ได้

Standalone agent ใช้ monitor:

  • Infrastructure: เครื่องจักรที่เป็นส่วนของระบบแต่อยู่ไม่รัน application code
  • Background jobs: เช่น intensive cron jobs หรือ long-running data-processing scripts ถ้า background jobs เขียนด้วยภาษาที่รองรับ (Ruby, Elixir, Node.js) ใช้ standard integration
  • More languages: โปรแกรมที่เขียนด้วยภาษาอื่นนอกเหนือจากที่รองรับ out of the box

เช่น ด้วย standalone agent track machine learning model ที่เขียน Python, instrument backup scripts, monitor Kafka brokers, หรือ collect host metrics ใน web farms มองข้อมูลทั้งหมดใน AppSignal เพื่อ complement metrics ที่มีอยู่สำหรับ main applications

วิธีการทำงาน

Agent ส่งมอบเป็น deb หรือ rpm package ไม่มี language dependencies รันบน Debian/Ubuntu หรือ Red Hat-based systems สำหรับ installation instructions 詳細 เช็ค agent documentation

ติดตั้งแล้ว agent config ใช้ได้ในไม่กี่นาที และรันตลอดไปเป็น daemon เงียบๆ monitor infrastructure ยิ่งไปกว่านั้น agent รวม StatsD server ที่ส่ง custom data ที่คุณ log ไปยัง AppSignal dashboard

StatsD คืออะไร?

StatsD เป็น standard สำหรับ collect และ aggregate arbitrary data มุ่งเน้น logging metric และ performance information ใช้ lightweight text protocol ผ่าน UDP connections ที่มี footprint เล็กในเครื่อง

StatsD message 长这样:

KEY:VALUE|TYPE 

KEY คือ string ใดๆ VALUE คือตัวเลข Type value กำหนดการประมวลผลตัวเลข

รองรับสามประเภท metrics:

  • c: counter เพิ่มทุกครั้งที่เรียก เช่น active_users:1|c เพิ่ม 1 ให้ counter active_users
  • g: gauge รับค่าตัวเลขและรักษาไว้จนกว่าจะอัปเดต เหมาะสำหรับบันทึกค่าที่เปลี่ยนขึ้นลงตามเวลา เช่น throughput, จำนวน active users, หรือ pending tasks ใน queue
  • t: เก็บค่า timing เหมาะสำหรับ track durations AppSignal คำนวณ means, count, และ percentiles สำหรับ timings ที่ล็อกทั้งหมด

Metric types อื่นที่ไม่รองรับจะถูก silently ignored

ส่งข้อมูลไป StatsD

Standalone agent listen UDP packets บน port 8125 ส่ง StatsD-formatted strings จาก command line ด้วย netcat:

echo -n "myscript.myevent.counter:1|c" | nc -4u -w0 localhost 8125

ใช้ UDP ไม่ต้องรอ response

Socat ทำงานได้:

echo -n "myscript.myevent.counter:1|c" | socat - udp:localhost:8125

ทำให้ instrument batch หรือ cron jobs ง่าย เช่น บรรทัดต่อไปใช้ gauge log ขนาดข้อมูลที่ backup job generate:

backup_size=$(du -m /backups | cut -f1)
echo -n "backup.data:$backup_size|g" | nc -4u -w0 localhost 8125

ไม่จำกัดแค่ integers StatsD ทำงานกับ floating-point numbers ได้:

echo -n "network.latency:0.2|g" | nc -4u -w0 localhost 8125

ใช้ Tags

เพิ่ม tags ให้ metrics StatsD server รองรับ optional tags ท้าย message:

KEY:VALUE|TYPE|#TAGS 

ใช้หลาย tags ใน message เดียวและ assign values สำหรับ filter ภายหลัง:

echo -n "backup.data:$backup_size|g|#backups,env:production" | nc -4u -w0 localhost 8125

จะเรียนรู้การดูข้อมูลใน AppSignal ต่อไป

Instrument ภาษาต่างๆ

StatsD server compatible กับภาษาใดๆ ที่ส่ง UDP packets ได้ มาดูตัวอย่าง

Python ยอดนิยมสำหรับ data mining และ machine learning เพิ่ม instrumentation ใน Python apps ด้วย built-in socket library:

import time
# measure time taken for function
start = time.process_time()
train_my_model()
training_time = time.process_time() - start

# send value to statsd
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 8125
MESSAGE = b"model.training.time:" + bytes(str(training_time), 'utf-8') + b"|t"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

ทำได้คล้ายกันใน Java ด้วย java.net library:

byte[] buffer = "mybatch.exception.counter:1|c".getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(
  buffer, buffer.length, address, 8125 );
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);

PHP อีกภาษายอดนิยม ส่ง UDP packets ด้วย socket_sendto:

$msg = "mywebsite.active_users:$ACTIVE_USERS|g";
$len = strlen($msg);
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 8125);
socket_close($sock);

StatsD Clients

จนถึงตอนนี้ใช้ built-in networking capabilities ในแต่ละภาษา แต่ยังมีมากกว่านี้ หลายภาษาและผลิตภัณฑ์มี third-party StatsD clients หรือ addons StatsD-compliant client ใดๆ ควรทำงานได้สำหรับ supported data types หารายชื่อ clients ได้ที่ StatsD project wiki

ดู Host Data ใน AppSignal

Host Metrics dashboard แสดง resource utilization ของเครื่อง:

AppSignal เพิ่ม entry สำหรับทุกเครื่องที่รัน standalone agent

Dashboard แสดง load และ CPU averages disk, network, memory, และ swap usage:

สร้าง Dashboards

AppSignal ไม่ auto-generate dashboards สำหรับ StatsD values ที่ส่ง ต้องสร้าง custom dashboards เอง

ก่อนอื่น คลิก Add dashboard ภายใต้ dashboard menu:

ตั้งชื่อ dashboard:

คลิก Add graph แสดง graph builder:

ตั้งชื่อ graph คลิก Add metric:

เลือก metric ที่สนใจจาก menu

ใช้ tags สำหรับ filtering (ถ้าต้องการ):

คลิก Back to overview ลอง graph types และ value units ต่างๆ หาตัวที่เหมาะสมที่สุดสำหรับ data ที่อยาก represent

Timing data ดูดีกับ area graphs เพราะ mean และ percentiles แสดงชัดเจนกว่า

Line graphs เหมาะกับ counters และ gauges

พอใจกับผลลัพธ์ คลิก Create graph

หมายเหตุ: เพิ่ม dashboard สำหรับ metrics อื่นๆ ที่ instrumented หรือ measured อัตโนมัติได้ หากตั้ง AppSignal เป็น APM สำหรับ Node.js, Ruby (on Rails) หรือ Elixir

สรุป

เรียนรู้การใช้ AppSignal's standalone server watch machines และ satellite code ด้วย built-in StatsD server บันทึก performance data ตามใจชอบและ instrument กระบวนการใดๆ

เช็คลิงก์ต่อไปเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ standalone agent:

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