github.com/blend/go-sdk@v1.20220411.3/stats/cronstats/listeners.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package cronstats 9 10 import ( 11 "context" 12 13 "github.com/blend/go-sdk/cron" 14 "github.com/blend/go-sdk/logger" 15 "github.com/blend/go-sdk/stats" 16 "github.com/blend/go-sdk/timeutil" 17 ) 18 19 // AddListeners adds web listeners. 20 func AddListeners(log logger.Listenable, collector stats.Collector, opts ...stats.AddListenerOption) { 21 if log == nil || collector == nil { 22 return 23 } 24 25 flags := []string{ 26 cron.FlagBegin, 27 cron.FlagComplete, 28 cron.FlagCanceled, 29 cron.FlagErrored, 30 cron.FlagSuccess, 31 cron.FlagBroken, 32 cron.FlagFixed, 33 } 34 35 options := stats.NewAddListenerOptions(opts...) 36 37 for _, flag := range flags { 38 log.Listen(flag, stats.ListenerNameStats, 39 cron.NewEventListener(func(ctx context.Context, ce cron.Event) { 40 var tags []string 41 tags = append(tags, stats.Tag(TagJob, ce.JobName)) 42 tags = append(tags, stats.Tag(TagJobStatus, ce.Flag)) 43 44 tags = append(tags, options.GetLoggerLabelsAsTags(ctx)...) 45 46 _ = collector.Increment(MetricNameCron, tags...) 47 if ce.Elapsed > 0 { 48 _ = collector.Gauge(MetricNameCronElapsedLast, timeutil.Milliseconds(ce.Elapsed), tags...) 49 _ = collector.Histogram(MetricNameCronElapsed, timeutil.Milliseconds(ce.Elapsed), tags...) 50 } 51 }), 52 ) 53 } 54 }