Trace back everything (tested)
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/meilisearch/meilisearch-go"
|
||||
"github.com/spf13/viper"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
)
|
||||
|
||||
func MeiliSearch(dsn MeiliDSN) meilisearch.ServiceManager {
|
||||
otelTransport := otelhttp.NewTransport(http.DefaultTransport)
|
||||
serviceName := viper.GetString("search.service_name")
|
||||
|
||||
otelTransport := otelhttp.NewTransport(
|
||||
http.DefaultTransport,
|
||||
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
|
||||
return fmt.Sprintf("%s %s", serviceName, r.Method)
|
||||
}),
|
||||
)
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: otelTransport,
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"nixcn-cms/logger"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/plugin/opentelemetry/tracing"
|
||||
@@ -20,13 +22,28 @@ func SplitHostPort(url string) (host, port string) {
|
||||
}
|
||||
|
||||
func Postgres(dsn ExternalDSN) (*gorm.DB, error) {
|
||||
serviceName := viper.GetString("database.service_name")
|
||||
|
||||
host, port := SplitHostPort(dsn.Host)
|
||||
conn := "host=" + host + " user=" + dsn.Username + " password=" + dsn.Password + " dbname=" + dsn.Name + " port=" + port + " sslmode=disable TimeZone=" + config.TZ()
|
||||
db, err := gorm.Open(postgres.Open(conn), &gorm.Config{Logger: logger.GormLogger()})
|
||||
|
||||
// Use otel gorm plugin
|
||||
if err := db.Use(tracing.NewPlugin()); err != nil {
|
||||
slog.Error("[Database] Error starting otel plugin!", "err", err)
|
||||
db, err := gorm.Open(postgres.Open(conn), &gorm.Config{
|
||||
Logger: logger.GormLogger(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Use(tracing.NewPlugin(
|
||||
tracing.WithAttributes(
|
||||
attribute.String("peer.service", serviceName),
|
||||
attribute.String("db.instance", dsn.Name),
|
||||
),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
slog.Error("[Database] Error starting otel plugin!", "name", serviceName, "err", err)
|
||||
}
|
||||
|
||||
return db, err
|
||||
}
|
||||
|
||||
@@ -6,9 +6,14 @@ import (
|
||||
|
||||
"github.com/redis/go-redis/extra/redisotel/v9"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/spf13/viper"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
|
||||
)
|
||||
|
||||
func Redis(dsn RedisDSN) (redis.UniversalClient, error) {
|
||||
serviceName := viper.GetString("cache.service_name")
|
||||
|
||||
// Connect to Redis
|
||||
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||
Addrs: dsn.Hosts,
|
||||
@@ -18,17 +23,22 @@ func Redis(dsn RedisDSN) (redis.UniversalClient, error) {
|
||||
DB: dsn.DB,
|
||||
})
|
||||
|
||||
if err := redisotel.InstrumentMetrics(rdb); err != nil {
|
||||
slog.Error("[Redis] Error starting otel metrics plugin!", "err", err)
|
||||
attrs := []attribute.KeyValue{
|
||||
semconv.DBSystemRedis,
|
||||
attribute.String("db.instance", serviceName),
|
||||
}
|
||||
|
||||
if err := redisotel.InstrumentTracing(rdb); err != nil {
|
||||
slog.Error("[Redis] Error starting otel tracing plugin!", "err", err)
|
||||
if err := redisotel.InstrumentMetrics(rdb, redisotel.WithAttributes(attrs...)); err != nil {
|
||||
slog.Error("[Redis] Error starting otel metrics plugin!", "name", serviceName, "err", err)
|
||||
}
|
||||
|
||||
if err := redisotel.InstrumentTracing(rdb, redisotel.WithAttributes(attrs...)); err != nil {
|
||||
slog.Error("[Redis] Error starting otel tracing plugin!", "name", serviceName, "err", err)
|
||||
}
|
||||
|
||||
// Ping redis
|
||||
ctx := context.Background()
|
||||
|
||||
// Ping Redis
|
||||
_, err := rdb.Ping(ctx).Result()
|
||||
|
||||
return rdb, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user