From 85db6721deb53acc6604b24985bc3f263efe599a Mon Sep 17 00:00:00 2001 From: boojack Date: Sat, 26 Nov 2022 09:23:38 +0800 Subject: [PATCH] chore: disable metrics collector (#580) --- bin/server/main.go | 2 ++ server/metric_collector.go | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bin/server/main.go b/bin/server/main.go index b7fffd1a..5157fc3a 100644 --- a/bin/server/main.go +++ b/bin/server/main.go @@ -40,6 +40,8 @@ func run(profile *profile.Profile) error { serverInstance.Store = storeInstance metricCollector := server.NewMetricCollector(profile, storeInstance) + // Disable metrics collector. + metricCollector.Enabled = false serverInstance.Collector = &metricCollector println(greetingBanner) diff --git a/server/metric_collector.go b/server/metric_collector.go index 550db88c..660625d4 100644 --- a/server/metric_collector.go +++ b/server/metric_collector.go @@ -13,9 +13,10 @@ import ( // MetricCollector is the metric collector. type MetricCollector struct { - collector metric.Collector - profile *profile.Profile - store *store.Store + Collector metric.Collector + Enabled bool + Profile *profile.Profile + Store *store.Store } const ( @@ -26,23 +27,28 @@ func NewMetricCollector(profile *profile.Profile, store *store.Store) MetricColl c := segment.NewCollector(segmentMetricWriteKey) return MetricCollector{ - collector: c, - profile: profile, - store: store, + Collector: c, + Enabled: true, + Profile: profile, + Store: store, } } func (mc *MetricCollector) Collect(_ context.Context, metric *metric.Metric) { - if mc.profile.Mode == "dev" { + if !mc.Enabled { + return + } + + if mc.Profile.Mode == "dev" { return } if metric.Labels == nil { metric.Labels = map[string]string{} } - metric.Labels["version"] = version.GetCurrentVersion(mc.profile.Mode) + metric.Labels["version"] = version.GetCurrentVersion(mc.Profile.Mode) - err := mc.collector.Collect(metric) + err := mc.Collector.Collect(metric) if err != nil { fmt.Printf("Failed to request segment, error: %+v\n", err) }