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

เชี่ยวชาญ Microservice Monitoring: จัดโครงสร้างข้อมูลด้วย Namespaces

Microservice Architecture คืออะไร?

Microservice Architecture เป็น Software Design Pattern ที่เราเขียน Applications โดยการ Combine Several Small Programs โปรแกรมเหล่านี้ ซึ่งเรียกว่า Microservices ทำงานร่วมกันเพื่อ Common Goal สำหรับบาง Teams ใช้เวลาและความพยายามน้อยกว่ามากในการเขียน Several Small Applications เทียบกับ Single Large One

Microservice-oriented Project ประกอบด้วย Constellation ของ Small Applications แต่ละตัวทำ Part ของมัน Run ใน Separate Process และ Communicate กับส่วนที่เหลือผ่าน Standardized Interfaces Approach นี้ให้ Teams เลือก Best Tool สำหรับแต่ละ Problem โดยไม่ Commit ถึง One Language หรือ Framework มันยัง Allow ให้เรา Split Work ระหว่าง Groups ที่ Specialized มากขึ้น

จาก Monolith ไป Microservice

ใน Part แรกของ Series นี้ เราพูดถึง Monoliths Monoliths เริ่มต้นง่าย Development Pace แต่ Scale Poorly 主要因为所有东西都紧密耦合 即使是小的代码更改也迫使我们重建和测试整个项目 这导致令人沮丧的漫长发布周期

เราไปจาก Monolith ไป Microservice ยังไง? ใช้ Case ของ Amazon ไปนานมาแล้ว พวกเขาเริ่มเป็น Monolith และเวลาผ่านไป Switch ไป Microservices Initial Design ของพวกเขาอาจมีลักษณะแบบนี้:

แน่นอน ฉัน Oversimplify ที่นี่ แต่ฉันเชื่อว่าฉันครอบคลุม Basics ส่วนใหญ่แล้ว ถ้าพวกเขาเลือก Follow Microservice Pattern ตั้งแต่ต้น พวกเขาจะ Split Application by Function มีแต่ละ Component Focus บน One Problem

พวกเขาก็จะต้อง Define Interfaces และ Protocols สำหรับ Inter-service Communication โดยปกติด้วย Lightweight Mechanisms เหมือน RESTful APIs

Namespaces คืออะไร

Microservice Design มี Unique Set ของ Challenges สิ่งหลักอาจคือ Instrumentation และ Error Reporting คิดดูสิ เรา Monitor Tens หรือ Hundreds ของ Components แพร่กระจาย Across Platforms และ Languages ที่ต่างกัน และเราต้อง Somehow Keep an Eye บนทั้งหมด และในเวลาเดียวกัน หลีกเลี่ยงการสูญเสีย Big Picture Namespaces ช่วยเรา Focus Group ของ Loosely-coupled Microservices เข้า Coherent Picture

ใน AppSignal Namespaces เป็น Containers สำหรับ Collected Metrics AppSignal ใช้ 3 Namespaces โดย Default (web, background, และ frontend) แต่เราสามารถสร้างของเราเองได้โดยเพิ่ม Code ไปซัก几行 เราจะดูว่ามันทำงานอย่างไร

One Application to Rule Them All (หนึ่งแอปพลิเคชันควบคุมทั้งหมด)

เมื่อ Setting Up Microservice Component สิ่งแรกที่ทำคือ Configure Common Application Name และ Environment ดังนั้น AppSignal จะ Present Collected Metrics และ Alerts บน Dashboard เดียวกัน

Specific Details ของวิธี Configure Values เหล่านี้ Depend บน Language และ Integration ตัวอย่าง เพื่อ Configure Ruby on Rails Application ให้ใช้ชื่อ "Nozama":

# config/appsignal.yml production: active: true push_api_key: "YOUR APPSIGNAL API KEY" name: "Nozama"

ซึ่ง Very Similar ถึงวิธีที่เรา Configure Elixir Integrations:

# config/config.exs config :appsignal, :config, active: true, name: "Nozama", push_api_key: "YOUR APPSIGNAL API KEY", env: "production"

ใน Node.js เราใช้:

const { Appsignal } = require("@appsignal/nodejs"); const appsignal = new Appsignal({ active: true, name: "Nozama", apiKey: "YOUR APPSIGNAL API KEY", });

สำหรับ Frontend JavaScript Integration เราใช้ @appsignal/javascript แทน:

import Appsignal from "@appsignal/javascript"; export default new Appsignal({ name: "Nozama", key: "YOUR FRONTEND API KEY", });

คุณสามารถหาข้อมูลการ Install และ Configure AppSignal ที่นี่:

  • Adding new applications
  • Configuring applications

Using Namespaces ใน Microservices

มา看看เราจะ Code แต่ละ Microservice ยังไง เริ่มกับ Billing System เราจะใช้ Elixir และ Phoenix สำหรับ Part นี้

เมื่อเรา Follow Phoenix Integration Setup เราสามารถเริ่มทำงานบน Controllers ได้ Snippet ต่อไปนี้ Set Namespace เป็น billing:

# in a Phoenix controller, we use plug to run the namespace initialization defmodule BillingPageController.PageController do use BillingPageController, :controller plug :set_appsignal_namespace defp set_appsignal_namespace(conn, _params) do # Sets all actions in this controller to report in the "billing" namespace Appsignal.Transaction.set_namespace(:billing) conn end # rest of the controller ... end

Data ควรเริ่มปรากฏใน Dashboard เมื่อ Microservice Running และ Controller เห็น Activity

แน่นอน Billing จะไม่พาเราไปไกลหากคนไม่ซื้ออะไร นี่คือ Problem ที่เราสามารถ Tackle ใน Microservice แยก ตาม Pattern เดียวกัน เราจะเขียน Phoenix Application ใหม่กับ PayButtonController Controller ที่เริ่มต้นแบบนี้:

defmodule PayButtonController.PageController do use PayButtonController, :controller plug :set_appsignal_namespace defp set_appsignal_namespace(conn, _params) do Appsignal.Span.set_namespace(Appsignal.Tracer.root_span(), "pay_button") conn end # rest of the controller ... end

ตอนนี้เรามี 2 Namespaces ใน Dashboard การใช้ Name และ Environment เดียวกัน Guarantee ว่า Data จาก PayButtonController แสดงร่วมกับ BillingPageController แม้จะเป็น Separate Applications Running บน Machines ต่างกัน

Component ถัดไปคือ Recommendation Engine เราจะ Implement API Endpoint ที่แสดง Product Suggestions ด้วย Express เรา Set Namespace ใน Node.js เหมือนแสดง:

app.get("/", (req, res) => { const tracer = appsignal.tracer(); tracer.withSpan( tracer.createSpan({ namespace: "recommendations" }), (span) => { // code to measure goes here span.close(); } ); });

เราขึ้นไป 3 Namespaces แล้ว

Mobile และ Frontend Teams อาจต้องการ Log Errors ใน Dashboard AppSignal's JavaScript Integration Automatically Assign Incoming Data ด้วย Namespace frontend แต่เราสามารถเปลี่ยนได้แบบนี้:

try { // code that might fail } catch (error) { // handle the error // send error to AppSignal appsignal.sendError(error, {}, "Mobile"); }

หลังจากพักใหญ่ Data ควรเริ่มปรากฏใน Namespace Mobile

Example แสดง Plain JavaScript แต่อาจมี Additional Setup Steps Required หากคุณใช้ Frontend Framework เหมือน React หรือ Angular

สำหรับ Website เราลอง Ruby on Rails MVC Framework ที่รู้จักดีซึ่ง AppSignal Integrates Out-of-the-box เราจะเริ่ม Rails Controllers ด้วย Snippet ต่อไปนี้เพื่อ Set Namespace เป็น homepage:

# in Rails we use before_action callback to change # the namespace before the request starts class HomepageController < ApplicationController before_action :set_appsignal_namespace def set_appsignal_namespace Appsignal.set_namespace("homepage") end # controller actions ... end

ต่อไป เราอาจใช้ API Endpoints เพื่อ Serve Data ให้ Website และ Mobile Applications สำหรับนี้ เราอาจใช้ Grape Lightweight REST API Framework สำหรับ Ruby คราวนี้ การ Configure AppSignal ใช้ Work นิดหน่อย

หลังจาก Configure Ruby Integration ใน ´config/appsignal.yml´ ตามที่เราทำก่อนหน้านี้ คุณสามารถเริ่ม Logging Events และ Metrics ด้วย:

Appsignal.start_logger Appsignal.start

จากนั้น Insert AppSignal Middleware ใน Error Handler Chain:

require "appsignal" require "appsignal/integrations/grape" class API < Grape::API insert_before Grape::Middleware::Error, Appsignal::Grape::Middleware resource :search do desc 'return a product search' before do Appsignal.set_namespace("search") end get :product do # product search logic end end end

สำหรับ Examples เพิ่มเติม Check Grape Integration Docs

เพื่อ Complete Picture เราจะจบด้วย Sidekiq Background Job Sidekiq เป็น Popular Job Processor สำหรับ Ruby และนี่คือวิธีที่เราสามารถ Start Up ใน Standalone Mode หลังจาก Configure ´config/appsignal.yml´:

# config.ru require 'appsignal' Sidekiq.on(:startup) do Appsignal.start end Sidekiq.on(:shutdown) do Appsignal.stop('Sidekiq shutdown') end

AppSignal Automatically Assign Data จาก Jobs ให้ Namespace background เราอาจต้องการเปลี่ยนเป็น Specific Namespace มากขึ้น

require 'sidekiq' require 'appsignal' class PlainOldRuby include Sidekiq::Worker def perform() Appsignal.set_namespace("urgent_background") # job logic end end

Collecting Metrics ด้วย Standalone Agent

Standalone Agent Collect Resource Utilization Metrics จาก Ubuntu, RedHat, หรือ CentOS Machine ใดๆ เราสามารถใช้ Agent เพื่อ Monitor Satellite Servers ที่ Provide Facilities เหมือน Databases, Gateways, หรือ Message Brokers ให้ Microservice Applications

ใน AppSignal เราใช้ Agent เพื่อ Monitor Kafka Servers ของเราเอง Agent ยัง Handy สำหรับสร้าง Custom Instrumentation บน Languages และ Frameworks ที่ไม่ Supported โดยตรง

เพื่อเริ่มต้นกับ Agent Download และ Install ตาม Installation Instructions

เมื่อมัน Running เราจะต้อง Edit Configuration File เพื่อ Set API Key, Application Name และ Environment ใช้ Values เดียวกับที่ใช้ใน Microservice ส่วนที่เหลือ เพื่อ Get Everything บน One Dashboard

# /etc/appsignal-agent.conf push_api_key = "YOUR APPSIGNAL API KEY" app_name = "Nozama" environment = "production" 

สรุป

Namespaces เป็นวิธียอดเยี่ยมในการจัดระเบียบ Data ที่กระจายอยู่ใน Multiple Systems พวกเขายัง Allow ให้เรา Configure Notifications และ Fine-tune Alert Handling เพื่อดูว่ามันทำงานอย่างไร Check Part แรกของ Series นี้

Additional Reads:

  • What can you do with namespaces?
  • Monitoring Any System with StatsD and AppSignal's Standalone Agent
  • Improved host metrics and alerts