ในบทช่วยสอนนี้ คุณจะได้เรียนรู้วิธีเชื่อมต่อกับฐานข้อมูล Upstash Redis โดยใช้ Go-Redis Client และตรวจสอบประสิทธิภาพแอปพลิเคชันด้วย Distributed Tracing
Go-Redis คืออะไร?
Go-Redis เป็น Redis Client ยอดนิยมสำหรับภาษา Go รองรับ Redis Server, Sentinel และ Cluster ทันทีหลังติดตั้ง
เพื่อเชื่อมต่อกับ Upstash Redis Database ให้ใช้โค้ดดังนี้:
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
func main() {
ctx := context.Background()
opt, _ := redis.ParseURL("<connection-string-from-Upstash>")
client := redis.NewClient(opt)
if err := client.Set(ctx, "foo", "bar", 0); err != nil {
panic(err)
}
fmt.Println(client.Get(ctx, "foo").Result())
}
หากต้องการรันคำสั่งแบบกำหนดเอง สามารถใช้ Alternative API ได้:
val, err := rdb.Do(ctx, "get", "key").Result()
if err != nil {
if err == redis.Nil {
fmt.Println("key does not exists")
return
}
panic(err)
}
คุณสามารถใช้ Go-Redis เพื่อ Cache ข้อมูล หรือ Rate-limit Requests ไปยัง API ของคุณ ดูรายละเอียดเพิ่มเติมได้ที่ Redis Golang Documentation
Distributed Tracing คืออะไร?
Distributed Tracing ช่วยให้สังเกต Requests ขณะที่พวกมันแพร่กระจายผ่าน Distributed Systems โดยเฉพาะที่สร้างด้วย Microservices Architecture
Tracing ช่วยให้ติดตาม Requests ขณะเดินทางผ่านระบบกระจาย คุณจะได้ Context เต็มรูปแบบว่าอะไรแตกต่าง อะไรเสียหาย และ Logs & Errors ใดที่เกี่ยวข้อง

OpenTelemetry คืออะไร?
OpenTelemetry เป็นมาตรฐาน Vendor-Neutral ที่ช่วยให้คุณเก็บและส่งออก Traces, Logs และ Metrics ได้
Otel ช่วยให้นักพัฒนาเก็บและส่งออก Telemetry Data แบบ Vendor Agnostic ด้วย OpenTelemetry คุณสามารถ Instrument แอปพลิเคชันครั้งเดียว แล้วเพิ่มหรือเปลี่ยน Vendors โดยไม่ต้องเปลี่ยน Instrumentation เช่น รายการ DataDog Alternatives ยอดนิยมที่รองรับ OpenTelemetry
OpenTelemetry พร้อมใช้งานสำหรับภาษาโปรแกรมส่วนใหญ่ และให้ Interoperability ข้ามภาษาและสภาพแวดล้อมที่แตกต่างกัน
Tracing และ Go-Redis
Go-Redis มาพร้อมกับ OpenTelemetry Instrumentation ที่ชื่อว่า RedisOtel ซึ่งเผยแพร่เป็นโมดูลแยกต่างหาก:
go get github.com/go-redis/redis/extra/redisotel/v8
เพื่อ Instrument Redis Client คุณต้องเพิ่ม Hook ที่ RedisOtel จัดเตรียมไว้:
import (
"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/extra/redisotel/v8"
)
rdb := redis.NewClient(&redis.Options{...})
rdb.AddHook(redisotel.NewTracingHook())
เพื่อให้ Tracing ทำงาน คุณต้องส่ง Active Trace Context ไปกับคำสั่ง Go-Redis ตัวอย่างเช่น:
ctx := req.Context()
val, err := rdb.Get(ctx, "key").Result()
เรียนรู้เพิ่มเติมเกี่ยวกับ RedisOtel ได้ที่ Monitoring Go Redis Performance and Errors
Uptrace
Uptrace เป็น DataDog Competitor แบบ Open Source ที่มี Query Builder ที่ใช้งานง่าย, Rich Dashboards, Automatic Alerts และ Integrations สำหรับภาษาและเฟรมเวิร์กส่วนใหญ่
คุณสามารถ ติดตั้ง Uptrace โดยดาวน์โหลด DEB/RPM Package หรือ Pre-compiled Binary
ตามคาด RedisOtel จะสร้าง Spans สำหรับ Redis Commands ที่ประมวลผล และบันทึก Errors ที่เกิดขึ้น นี่คือวิธีที่ข้อมูลที่รวบรวมได้แสดงใน Uptrace:

คุณสามารถหาตัวอย่างที่รันได้ที่ GitHub
ขั้นตอนต่อไปคืออะไร?
ต่อไป คุณสามารถติดตั้ง OpenTelemetry Instrumentations เพิ่มเติม เพื่อติดตามด้านอื่นๆ ของแอป เช่น Gin หรือ Go gRPC
คุณยังสามารถเรียนรู้เกี่ยวกับ OpenTelemetry Tracing API และ Metrics API เพื่อสร้าง Instrumentation ของตัวเองได้