-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add telemetry #144
Merged
Merged
add telemetry #144
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ldmonster
reviewed
Mar 11, 2025
ldmonster
reviewed
Mar 11, 2025
…rrors diff --git c/internal/flags/flags.go i/internal/flags/flags.go index 5abec33..fc43c3b 100644 --- c/internal/flags/flags.go +++ i/internal/flags/flags.go @@ -34,6 +34,7 @@ var ( PrintVersion bool Version string ValuesFile string + MetricsFile string ) func InitDefaultFlagSet() *pflag.FlagSet { @@ -52,6 +53,7 @@ func InitLintFlagSet() *pflag.FlagSet { lint.StringVar(&LinterName, "linter", "", "linter name to run") lint.StringVarP(&LogLevel, "log-level", "l", "INFO", "log-level [DEBUG | INFO | WARN | ERROR]") lint.StringVarP(&ValuesFile, "values-file", "f", "", "path to values.yaml file with override values") + lint.StringVarP(&MetricsFile, "metrics-file", "m", "", "path to metrics.txt file") return lint } diff --git c/internal/manager/manager.go i/internal/manager/manager.go index ac21c43..f129709 100644 --- c/internal/manager/manager.go +++ i/internal/manager/manager.go @@ -250,6 +250,50 @@ func (m *Manager) PrintResult() { fmt.Println(buf.String()) } +func (m *Manager) PrintMetrics() { + if flags.MetricsFile == "" { + return + } + + const ( + metricsFilePermission = 0644 + metricsDirPermission = 0755 + ) + + metricsFile, err := fsutils.ExpandDir(flags.MetricsFile) + if err != nil { + logger.ErrorF("Failed to expand metrics file: %v", err) + return + } + if err = os.MkdirAll(filepath.Dir(metricsFile), metricsDirPermission); err != nil { + logger.ErrorF("Failed to create metrics file directory: %v", err) + return + } + f, err := os.OpenFile(metricsFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, metricsFilePermission) + if err != nil { + logger.ErrorF("Failed to open metrics file: %v", err) + return + } + defer f.Close() + for labels, count := range m.errors.GetMetrics() { + t := strings.Split(labels, ":") + if len(t) != 2 { + continue + } + linterID := t[0] + ruleID := t[1] + fmt.Fprintf(f, `dmt_linter_warnings_count{"version": "%s", "linter": "%s", "rule": "%s"} %d\n`, flags.Version, linterID, ruleID, count) + } + if err := f.Sync(); err != nil { + logger.ErrorF("Failed to sync metrics file: %v", err) + return + } + if err := f.Close(); err != nil { + logger.ErrorF("Failed to close metrics file: %v", err) + return + } +} + func (m *Manager) HasCriticalErrors() bool { return m.errors.ContainsErrors() }
diff --git c/cmd/dmt/main.go i/cmd/dmt/main.go index b4275de..fa4d581 100644 --- c/cmd/dmt/main.go +++ i/cmd/dmt/main.go @@ -43,6 +43,7 @@ func runLint(dir string) { mng := manager.NewManager(dir, cfg) mng.Run() mng.PrintResult() + mng.PrintMetrics() metricsClient, err := metrics.NewPrometheusMetricsService(os.Getenv("DMT_METRICS_URL"), os.Getenv("DMT_METRICS_TOKEN")) if err != nil { diff --git c/internal/manager/manager.go i/internal/manager/manager.go index f129709..b5b683d 100644 --- c/internal/manager/manager.go +++ i/internal/manager/manager.go @@ -282,16 +282,12 @@ func (m *Manager) PrintMetrics() { } linterID := t[0] ruleID := t[1] - fmt.Fprintf(f, `dmt_linter_warnings_count{"version": "%s", "linter": "%s", "rule": "%s"} %d\n`, flags.Version, linterID, ruleID, count) + fmt.Fprintf(f, "dmt_linter_warnings_count{\"version\": \"%s\", \"linter\": \"%s\", \"rule\": \"%s\"} %d\n", flags.Version, linterID, ruleID, count) } if err := f.Sync(); err != nil { logger.ErrorF("Failed to sync metrics file: %v", err) return } - if err := f.Close(); err != nil { - logger.ErrorF("Failed to close metrics file: %v", err) - return - } } func (m *Manager) HasCriticalErrors() bool {
Signed-off-by: Evsyukov Denis <[email protected]>
…linter warning tracking Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
…ed functions Signed-off-by: Evsyukov Denis <[email protected]>
…n up metrics logging Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
…unction Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
ldmonster
reviewed
Mar 25, 2025
ldmonster
reviewed
Mar 25, 2025
ldmonster
reviewed
Mar 25, 2025
Co-authored-by: Pavel Okhlopkov <[email protected]> Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
ldmonster
reviewed
Mar 26, 2025
We can parse metric name from Desc() by regex Output example:
In original prometheus code this field is private, but we still can parse it from description |
Signed-off-by: Evsyukov Denis <[email protected]>
…ization Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
…ing conventions Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
ldmonster
approved these changes
Mar 26, 2025
…o handling Signed-off-by: Evsyukov Denis <[email protected]>
Signed-off-by: Evsyukov Denis <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixed: #58