@@ -10,7 +10,6 @@ import (
10
10
"time"
11
11
12
12
"github.com/go-chi/chi"
13
- "github.com/prometheus/client_golang/prometheus/promhttp"
14
13
log "github.com/sirupsen/logrus"
15
14
uuid "gopkg.in/satori/go.uuid.v1"
16
15
@@ -45,7 +44,9 @@ type ShellOperator struct {
45
44
TempDir string
46
45
47
46
MetricStorage * metric_storage.MetricStorage
48
- KubeClient kube.KubernetesClient
47
+ // separate metric storage for hook metrics if separate listen port is configured
48
+ HookMetricStorage * metric_storage.MetricStorage
49
+ KubeClient kube.KubernetesClient
49
50
50
51
ScheduleManager schedule_manager.ScheduleManager
51
52
KubeEventsManager kube_events_manager.KubeEventsManager
@@ -90,11 +91,11 @@ func (op *ShellOperator) WithMetricStorage(metricStorage *metric_storage.MetricS
90
91
op .MetricStorage = metricStorage
91
92
}
92
93
94
+ // InitMetricStorage creates default MetricStorage object if not set earlier.
93
95
func (op * ShellOperator ) InitMetricStorage () {
94
96
if op .MetricStorage != nil {
95
97
return
96
98
}
97
- // Metric storage.
98
99
metricStorage := metric_storage .NewMetricStorage ()
99
100
metricStorage .WithContext (op .ctx )
100
101
metricStorage .WithPrefix (app .PrometheusMetricsPrefix )
@@ -103,6 +104,20 @@ func (op *ShellOperator) InitMetricStorage() {
103
104
op .MetricStorage = metricStorage
104
105
}
105
106
107
+ // InitHookMetricStorage creates MetricStorage object
108
+ // with new registry to scrape hook metrics on separate port.
109
+ func (op * ShellOperator ) InitHookMetricStorage () {
110
+ if op .HookMetricStorage != nil {
111
+ return
112
+ }
113
+ metricStorage := metric_storage .NewMetricStorage ()
114
+ metricStorage .WithContext (op .ctx )
115
+ metricStorage .WithPrefix (app .PrometheusMetricsPrefix )
116
+ metricStorage .WithNewRegistry ()
117
+ metricStorage .Start ()
118
+ op .HookMetricStorage = metricStorage
119
+ }
120
+
106
121
// Init does some basic checks and instantiate dependencies
107
122
//
108
123
// - check directories
@@ -416,7 +431,7 @@ func (op *ShellOperator) TaskHandleHookRun(t task.Task) queue.TaskResult {
416
431
metrics , err := taskHook .Run (hookMeta .BindingType , hookMeta .BindingContext , hookLogLabels )
417
432
418
433
if err == nil {
419
- err = op .MetricStorage .SendBatch (metrics , map [string ]string {
434
+ err = op .HookMetricStorage .SendBatch (metrics , map [string ]string {
420
435
"hook" : hookMeta .HookName ,
421
436
})
422
437
}
@@ -674,10 +689,10 @@ func (op *ShellOperator) SetupHttpServerHandles() {
674
689
</html>` , app .ListenPort )
675
690
})
676
691
677
- http .Handle ("/metrics" , promhttp .Handler ())
692
+ http .Handle ("/metrics" , op . MetricStorage .Handler ())
678
693
}
679
694
680
- func (op * ShellOperator ) StartHttpServer (ip string , port string ) error {
695
+ func (op * ShellOperator ) StartHttpServer (ip string , port string , mux * http. ServeMux ) error {
681
696
address := fmt .Sprintf ("%s:%s" , ip , port )
682
697
683
698
// Check if port is available
@@ -690,7 +705,7 @@ func (op *ShellOperator) StartHttpServer(ip string, port string) error {
690
705
log .Infof ("Listen on %s" , address )
691
706
692
707
go func () {
693
- if err := http .Serve (listener , nil ); err != nil {
708
+ if err := http .Serve (listener , mux ); err != nil {
694
709
log .Errorf ("Error starting HTTP server: %s" , err )
695
710
os .Exit (1 )
696
711
}
@@ -699,22 +714,48 @@ func (op *ShellOperator) StartHttpServer(ip string, port string) error {
699
714
return nil
700
715
}
701
716
717
+ func (op * ShellOperator ) SetupHookMetricStorageAndServer () error {
718
+ if op .HookMetricStorage != nil {
719
+ return nil
720
+ }
721
+ if app .HookMetricsListenPort == "" || app .HookMetricsListenPort == app .ListenPort {
722
+ // register default prom handler in DefaultServeMux
723
+ op .HookMetricStorage = op .MetricStorage
724
+ } else {
725
+ // create new metric storage for hooks
726
+ op .InitHookMetricStorage ()
727
+ // Create new ServeMux, serve on custom port
728
+ mux := http .NewServeMux ()
729
+ err := op .StartHttpServer (app .ListenAddress , app .HookMetricsListenPort , mux )
730
+ if err != nil {
731
+ return err
732
+ }
733
+ // register scrape handler
734
+ mux .Handle ("/metrics" , op .HookMetricStorage .Handler ())
735
+ }
736
+ return nil
737
+ }
738
+
702
739
func DefaultOperator () * ShellOperator {
703
740
operator := NewShellOperator ()
704
741
operator .WithContext (context .Background ())
705
742
return operator
706
743
}
707
744
708
745
func InitAndStart (operator * ShellOperator ) error {
709
- operator .SetupHttpServerHandles ()
710
-
711
- err := operator .StartHttpServer (app .ListenAddress , app .ListenPort )
746
+ err := operator .StartHttpServer (app .ListenAddress , app .ListenPort , http .DefaultServeMux )
712
747
if err != nil {
713
748
log .Errorf ("HTTP SERVER start failed: %v" , err )
714
749
return err
715
750
}
716
-
717
751
operator .InitMetricStorage ()
752
+ operator .SetupHttpServerHandles ()
753
+
754
+ err = operator .SetupHookMetricStorageAndServer ()
755
+ if err != nil {
756
+ log .Errorf ("HTTP SERVER for hook metrics start failed: %v" , err )
757
+ return err
758
+ }
718
759
719
760
err = operator .Init ()
720
761
if err != nil {
0 commit comments