From 890c77e78edd5b869df52dfa3b9853aaccca2d2e Mon Sep 17 00:00:00 2001 From: Sourav Badami Date: Tue, 9 Jul 2024 16:25:32 +0530 Subject: [PATCH] Configurable app name for tracing, defaults to gorse. --- config/config.go | 9 +++++- config/config.toml | 3 ++ master/metrics.go | 77 +++++++++++++++++++++++----------------------- server/metrics.go | 3 +- server/rest.go | 2 +- worker/metrics.go | 11 ++++--- 6 files changed, 59 insertions(+), 46 deletions(-) diff --git a/config/config.go b/config/config.go index 5a44e2f70..d694b781f 100644 --- a/config/config.go +++ b/config/config.go @@ -167,6 +167,7 @@ type OnlineConfig struct { } type TracingConfig struct { + AppName string `mapstructure:"app_name"` EnableTracing bool `mapstructure:"enable_tracing"` Exporter string `mapstructure:"exporter" validate:"oneof=jaeger zipkin otlp otlphttp"` CollectorEndpoint string `mapstructure:"collector_endpoint"` @@ -246,6 +247,7 @@ func GetDefaultConfig() *Config { }, }, Tracing: TracingConfig{ + AppName: "gorse", Exporter: "jaeger", Sampler: "always", }, @@ -259,6 +261,10 @@ func (config *Config) Now() *time.Time { return lo.ToPtr(time.Now().Add(config.Server.ClockError)) } +func (config TracingConfig) GetAppName() string { + return config.AppName +} + func (config *Config) UserNeighborDigest() string { var builder strings.Builder builder.WriteString(fmt.Sprintf("%v-%v", config.Recommend.UserNeighbors.NeighborType, config.Recommend.UserNeighbors.EnableIndex)) @@ -450,7 +456,7 @@ func (config *TracingConfig) NewTracerProvider() (trace.TracerProvider, error) { tracesdk.WithBatcher(exporter), tracesdk.WithResource(resource.NewWithAttributes( semconv.SchemaURL, - semconv.ServiceNameKey.String("gorse"), + semconv.ServiceNameKey.String(config.AppName), )), ), nil } @@ -524,6 +530,7 @@ func setDefault() { viper.SetDefault("recommend.online.fallback_recommend", defaultConfig.Recommend.Online.FallbackRecommend) viper.SetDefault("recommend.online.num_feedback_fallback_item_based", defaultConfig.Recommend.Online.NumFeedbackFallbackItemBased) // [tracing] + viper.SetDefault("tracing.app_name", defaultConfig.Tracing.AppName) viper.SetDefault("tracing.exporter", defaultConfig.Tracing.Exporter) viper.SetDefault("tracing.sampler", defaultConfig.Tracing.Sampler) // [experimental] diff --git a/config/config.toml b/config/config.toml index e0dd91ee0..b15d27b44 100644 --- a/config/config.toml +++ b/config/config.toml @@ -235,6 +235,9 @@ num_feedback_fallback_item_based = 10 [tracing] +# App name +app_name = "gorse" + # Enable tracing for REST APIs. The default value is false. enable_tracing = false diff --git a/master/metrics.go b/master/metrics.go index 48902d0b4..746908a3e 100644 --- a/master/metrics.go +++ b/master/metrics.go @@ -21,6 +21,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/samber/lo" + "github.com/zhenghaoz/gorse/config" "github.com/zhenghaoz/gorse/storage/cache" ) @@ -32,194 +33,194 @@ const ( var ( LoadDatasetStepSecondsVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "load_dataset_step_seconds", }, []string{LabelStep}) LoadDatasetTotalSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "load_dataset_total_seconds", }) FindUserNeighborsSecondsVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "find_user_neighbors_seconds", }, []string{LabelStep}) FindUserNeighborsTotalSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "find_user_neighbors_total_seconds", }) FindItemNeighborsSecondsVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "find_item_neighbors_seconds", }, []string{"step"}) FindItemNeighborsTotalSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "find_item_neighbors_total_seconds", }) UpdateUserNeighborsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "update_user_neighbors_total", }) UpdateItemNeighborsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "update_item_neighbors_total", }) CacheScannedTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "cache_scanned_total", }) CacheReclaimedTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "cache_reclaimed_total", }) CacheScannedSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "cache_scanned_seconds", }) CollaborativeFilteringFitSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_fit_seconds", }) CollaborativeFilteringSearchSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_search_seconds", }) CollaborativeFilteringNDCG10 = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_ndcg_10", }) CollaborativeFilteringPrecision10 = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_precision_10", }) CollaborativeFilteringRecall10 = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_recall_10", }) CollaborativeFilteringSearchPrecision10 = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "collaborative_filtering_search_precision_10", }) RankingFitSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_fit_seconds", }) RankingSearchSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_search_seconds", }) RankingPrecision = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_model_precision", }) RankingRecall = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_model_recall", }) RankingAUC = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_model_auc", }) RankingSearchPrecision = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "ranking_search_precision", }) UserNeighborIndexRecall = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "user_neighbor_index_recall", }) ItemNeighborIndexRecall = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "item_neighbor_index_recall", }) UsersTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "users_total", }) ActiveUsersTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "active_users_total", }) InactiveUsersTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "inactive_users_total", }) ItemsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "items_total", }) ActiveItemsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "active_items_total", }) InactiveItemsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "inactive_items_total", }) UserLabelsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "user_labels_total", }) ItemLabelsTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "item_labels_total", }) FeedbacksTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "feedbacks_total", }) ImplicitFeedbacksTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "implicit_feedbacks_total", }) PositiveFeedbacksTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "positive_feedbacks_total", }) NegativeFeedbackTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "negative_feedbacks_total", }) MemoryInUseBytesVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "master", Name: "memory_inuse_bytes", }, []string{LabelData}) diff --git a/server/metrics.go b/server/metrics.go index c711112dc..7dc38a1c1 100644 --- a/server/metrics.go +++ b/server/metrics.go @@ -17,11 +17,12 @@ package server import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/zhenghaoz/gorse/config" ) var ( RestAPIRequestSecondsVec = promauto.NewHistogramVec(prometheus.HistogramOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "server", Name: "rest_api_request_seconds", }, []string{"api"}) diff --git a/server/rest.go b/server/rest.go index 98ae7c40c..82507130e 100644 --- a/server/rest.go +++ b/server/rest.go @@ -177,7 +177,7 @@ func (s *RestServer) CreateWebService() { Filter(s.LogFilter). Filter(s.AuthFilter). Filter(s.MetricsFilter). - Filter(otelrestful.OTelFilter("gorse")) + Filter(otelrestful.OTelFilter(s.Config.Tracing.AppName)) /* Health check */ ws.Route(ws.GET("/health/live").To(s.checkLive). diff --git a/worker/metrics.go b/worker/metrics.go index 1a1beec16..c17873917 100644 --- a/worker/metrics.go +++ b/worker/metrics.go @@ -17,6 +17,7 @@ package worker import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/zhenghaoz/gorse/config" ) const ( @@ -26,27 +27,27 @@ const ( var ( UpdateUserRecommendTotal = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "worker", Name: "update_user_recommend_total", }) OfflineRecommendStepSecondsVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "worker", Name: "offline_recommend_step_seconds", }, []string{LabelStep}) OfflineRecommendTotalSeconds = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "worker", Name: "offline_recommend_total_seconds", }) CollaborativeFilteringIndexRecall = promauto.NewGauge(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "worker", Name: "collaborative_filtering_index_recall", }) MemoryInuseBytesVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "gorse", + Namespace: config.TracingConfig.GetAppName(), Subsystem: "worker", Name: "memory_inuse_bytes", }, []string{LabelData})