@@ -1,10 +1,22 @@
|
||||
package drivers
|
||||
|
||||
import "github.com/meilisearch/meilisearch-go"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/meilisearch/meilisearch-go"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
)
|
||||
|
||||
func MeiliSearch(dsn MeiliDSN) meilisearch.ServiceManager {
|
||||
otelTransport := otelhttp.NewTransport(http.DefaultTransport)
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: otelTransport,
|
||||
}
|
||||
|
||||
return meilisearch.New(dsn.Host,
|
||||
meilisearch.WithAPIKey(dsn.ApiKey),
|
||||
meilisearch.WithCustomClient(httpClient),
|
||||
meilisearch.WithContentEncoding(
|
||||
meilisearch.GzipEncoding,
|
||||
meilisearch.BestCompression,
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"nixcn-cms/config"
|
||||
"nixcn-cms/logger"
|
||||
"strings"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/plugin/opentelemetry/tracing"
|
||||
)
|
||||
|
||||
func SplitHostPort(url string) (host, port string) {
|
||||
@@ -19,6 +22,11 @@ func SplitHostPort(url string) (host, port string) {
|
||||
func Postgres(dsn ExternalDSN) (*gorm.DB, error) {
|
||||
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{})
|
||||
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)
|
||||
}
|
||||
return db, err
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package drivers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/redis/go-redis/extra/redisotel/v9"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -15,7 +17,17 @@ func Redis(dsn RedisDSN) (redis.UniversalClient, error) {
|
||||
Password: dsn.Password,
|
||||
DB: dsn.DB,
|
||||
})
|
||||
|
||||
if err := redisotel.InstrumentMetrics(rdb); err != nil {
|
||||
slog.Error("[Redis] Error starting otel metrics plugin!", "err", err)
|
||||
}
|
||||
|
||||
if err := redisotel.InstrumentTracing(rdb); err != nil {
|
||||
slog.Error("[Redis] Error starting otel tracing plugin!", "err", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Ping Redis
|
||||
_, err := rdb.Ping(ctx).Result()
|
||||
return rdb, err
|
||||
|
||||
Reference in New Issue
Block a user