You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2022/11/28 16:54:38 UTC

[GitHub] [incubator-eventmesh] horoc opened a new pull request, #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

horoc opened a new pull request, #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303

   Fixes #2196 
   
   ### Motivation
   
   #2196 
   
   ### Modifications
   
   #2196 
   
   ### Documentation
   
   None
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] codecov[bot] commented on pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
codecov[bot] commented on PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#issuecomment-1334929672

   # [Codecov](https://codecov.io/gh/apache/incubator-eventmesh/pull/2303?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#2303](https://codecov.io/gh/apache/incubator-eventmesh/pull/2303?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (4ba2b0c) into [master](https://codecov.io/gh/apache/incubator-eventmesh/commit/6bdd7a0d726150bb893c72205d384367ef8e4023?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (6bdd7a0) will **decrease** coverage by `0.03%`.
   > The diff coverage is `n/a`.
   
   ```diff
   @@             Coverage Diff              @@
   ##             master    #2303      +/-   ##
   ============================================
   - Coverage     11.78%   11.75%   -0.04%     
   + Complexity      903      899       -4     
   ============================================
     Files           473      473              
     Lines         27856    27856              
     Branches       3000     3000              
   ============================================
   - Hits           3284     3275       -9     
   - Misses        24276    24286      +10     
   + Partials        296      295       -1     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/incubator-eventmesh/pull/2303?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [...tandalone/broker/task/HistoryMessageClearTask.java](https://codecov.io/gh/apache/incubator-eventmesh/pull/2303/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-ZXZlbnRtZXNoLWNvbm5lY3Rvci1wbHVnaW4vZXZlbnRtZXNoLWNvbm5lY3Rvci1zdGFuZGFsb25lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9ldmVudG1lc2gvY29ubmVjdG9yL3N0YW5kYWxvbmUvYnJva2VyL3Rhc2svSGlzdG9yeU1lc3NhZ2VDbGVhclRhc2suamF2YQ==) | `29.41% <0.00%> (-17.65%)` | :arrow_down: |
   | [...mesh/connector/standalone/broker/MessageQueue.java](https://codecov.io/gh/apache/incubator-eventmesh/pull/2303/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-ZXZlbnRtZXNoLWNvbm5lY3Rvci1wbHVnaW4vZXZlbnRtZXNoLWNvbm5lY3Rvci1zdGFuZGFsb25lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9ldmVudG1lc2gvY29ubmVjdG9yL3N0YW5kYWxvbmUvYnJva2VyL01lc3NhZ2VRdWV1ZS5qYXZh) | `32.46% <0.00%> (-7.80%)` | :arrow_down: |
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] walterlife commented on a diff in pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
walterlife commented on code in PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#discussion_r1038671401


##########
eventmesh-workflow-go/internal/metrics/metrics.go:
##########
@@ -0,0 +1,177 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package metrics
+
+import (
+	"fmt"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/log"
+	conf "github.com/apache/incubator-eventmesh/eventmesh-workflow-go/config"
+	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
+	"net/http"
+	"sync"
+)
+
+func init() {
+	prometheusMetrics = getPrometheusMetricsByConfig()
+}
+
+var prometheusMetrics *Metrics
+
+func Inc(name string, label string) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Inc()
+}
+
+func Add(name string, label string, val float64) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Add(val)
+}
+
+func Sub(name string, label string, val float64) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Sub(val)
+}
+
+func Dec(name string, label string) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Dec()
+}
+
+func RecordLatency(name string, label string, latency float64) {
+	collector := prometheusMetrics.loadCollector(name, histogram).(*prometheus.HistogramVec)
+	collector.With(prometheus.Labels{"label": label}).Observe(latency)
+}
+
+func getPrometheusMetricsByConfig() *Metrics {
+	config := conf.Get()
+
+	port := ""
+	if !checkMetricsConfig(config) {
+		port = defaultEndPoint
+	} else {
+		port = config.Metrics.EndpointPort
+	}
+
+	m := &Metrics{
+		counters:   make(map[string]prometheus.Collector),
+		histograms: make(map[string]prometheus.Collector),
+		port:       port,
+	}
+	m.Init()
+	return m
+}
+
+func checkMetricsConfig(config *conf.Config) bool {
+	if config == nil || len(config.Metrics.EndpointPort) == 0 {
+		return false
+	}
+	return true
+}
+
+type Metrics struct {
+	counters   map[string]prometheus.Collector
+	histograms map[string]prometheus.Collector
+	gauges     map[string]prometheus.Collector
+	lock       sync.Mutex
+	once       sync.Once
+	port       string
+}
+
+const (
+	nameSpace       = "eventmesh"
+	subSystem       = "workflow"
+	defaultEndPoint = "19090"
+)
+
+const (
+	histogram = iota
+	gauge
+)
+
+// Init try to init metrics, include exposing http endpoint
+func (p *Metrics) Init() {
+	p.once.Do(func() {
+		p.exposeEndpoint()
+	})
+}
+
+// exposeEndpoint expose http endpoint
+func (p *Metrics) exposeEndpoint() {
+	go func() {
+		http.Handle("/metrics", promhttp.Handler())
+		err := http.ListenAndServe(fmt.Sprintf(":%s", p.port), nil)
+		if err != nil {
+			log.Errorf("fail to listen prometheus endpoint port %s, err=%v", p.port, err)
+		}
+	}()
+}
+
+// loadCollector load collector by name and collectorType
+func (p *Metrics) loadCollector(name string, collectorType int) prometheus.Collector {
+	if collector := p.getCollectorByNameAndType(name, collectorType); collector != nil {
+		return collector
+	}
+	return p.registerNewCollector(name, collectorType)
+}
+
+func (p *Metrics) getCollectorByNameAndType(name string, collectorType int) prometheus.Collector {
+	switch collectorType {
+	case histogram:
+		return p.histograms[name]
+	case gauge:
+		return p.histograms[name]
+	default:
+		panic("prometheus metrics get collector error, illegal collector type")

Review Comment:
   try to use error return instead of panic
   @horoc 



##########
eventmesh-workflow-go/internal/constants/constants.go:
##########
@@ -68,3 +68,23 @@ const (
 	EventPropsWorkflowInstanceID     = "workflowinstanceid"
 	EventPropsWorkflowTaskInstanceID = "workflowtaskinstanceid"
 )
+
+// Metrics Related constants
+const (
+	MetricsEventTask     = "event_task"
+	MetricsOperationTask = "operation_task"
+	MetricsSwitchTask    = "switch_task"
+	MetricsScheduler     = "scheduler"
+	MetricsEngine        = "engine"
+	MetricsTaskQueue     = "task_queue"
+
+	MetricsTotal        = "total"
+	MetricsSwitchReject = "reject"
+	MetricsSwitchPass   = "pass"
+
+	MetricsStartRequest      = "start_request"

Review Comment:
   Is it possible to initialize the metric collector first?
   @horoc 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] walterlife commented on a diff in pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
walterlife commented on code in PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#discussion_r1036936623


##########
eventmesh-workflow-go/internal/task/event_task.go:
##########
@@ -43,6 +44,7 @@ func NewEventTask(instance *model.WorkflowTaskInstance) Task {
 }
 
 func (t *eventTask) Run() error {
+	metrics.Inc("event_task", "total")

Review Comment:
   the metric type, the metric name is recommended to be maintained in the constants file
   @horoc 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] walterlife merged pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
walterlife merged PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] horoc commented on pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
horoc commented on PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#issuecomment-1329430513

   hi @walterlife, could you help to review this pr? 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] horoc commented on a diff in pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
horoc commented on code in PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#discussion_r1037896961


##########
eventmesh-workflow-go/internal/task/event_task.go:
##########
@@ -43,6 +44,7 @@ func NewEventTask(instance *model.WorkflowTaskInstance) Task {
 }
 
 func (t *eventTask) Run() error {
+	metrics.Inc("event_task", "total")

Review Comment:
   updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] horoc commented on a diff in pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
horoc commented on code in PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#discussion_r1038985938


##########
eventmesh-workflow-go/internal/metrics/metrics.go:
##########
@@ -0,0 +1,177 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package metrics
+
+import (
+	"fmt"
+	"github.com/apache/incubator-eventmesh/eventmesh-server-go/log"
+	conf "github.com/apache/incubator-eventmesh/eventmesh-workflow-go/config"
+	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
+	"net/http"
+	"sync"
+)
+
+func init() {
+	prometheusMetrics = getPrometheusMetricsByConfig()
+}
+
+var prometheusMetrics *Metrics
+
+func Inc(name string, label string) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Inc()
+}
+
+func Add(name string, label string, val float64) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Add(val)
+}
+
+func Sub(name string, label string, val float64) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Sub(val)
+}
+
+func Dec(name string, label string) {
+	collector := prometheusMetrics.loadCollector(name, gauge).(*prometheus.GaugeVec)
+	collector.With(prometheus.Labels{"label": label}).Dec()
+}
+
+func RecordLatency(name string, label string, latency float64) {
+	collector := prometheusMetrics.loadCollector(name, histogram).(*prometheus.HistogramVec)
+	collector.With(prometheus.Labels{"label": label}).Observe(latency)
+}
+
+func getPrometheusMetricsByConfig() *Metrics {
+	config := conf.Get()
+
+	port := ""
+	if !checkMetricsConfig(config) {
+		port = defaultEndPoint
+	} else {
+		port = config.Metrics.EndpointPort
+	}
+
+	m := &Metrics{
+		counters:   make(map[string]prometheus.Collector),
+		histograms: make(map[string]prometheus.Collector),
+		port:       port,
+	}
+	m.Init()
+	return m
+}
+
+func checkMetricsConfig(config *conf.Config) bool {
+	if config == nil || len(config.Metrics.EndpointPort) == 0 {
+		return false
+	}
+	return true
+}
+
+type Metrics struct {
+	counters   map[string]prometheus.Collector
+	histograms map[string]prometheus.Collector
+	gauges     map[string]prometheus.Collector
+	lock       sync.Mutex
+	once       sync.Once
+	port       string
+}
+
+const (
+	nameSpace       = "eventmesh"
+	subSystem       = "workflow"
+	defaultEndPoint = "19090"
+)
+
+const (
+	histogram = iota
+	gauge
+)
+
+// Init try to init metrics, include exposing http endpoint
+func (p *Metrics) Init() {
+	p.once.Do(func() {
+		p.exposeEndpoint()
+	})
+}
+
+// exposeEndpoint expose http endpoint
+func (p *Metrics) exposeEndpoint() {
+	go func() {
+		http.Handle("/metrics", promhttp.Handler())
+		err := http.ListenAndServe(fmt.Sprintf(":%s", p.port), nil)
+		if err != nil {
+			log.Errorf("fail to listen prometheus endpoint port %s, err=%v", p.port, err)
+		}
+	}()
+}
+
+// loadCollector load collector by name and collectorType
+func (p *Metrics) loadCollector(name string, collectorType int) prometheus.Collector {
+	if collector := p.getCollectorByNameAndType(name, collectorType); collector != nil {
+		return collector
+	}
+	return p.registerNewCollector(name, collectorType)
+}
+
+func (p *Metrics) getCollectorByNameAndType(name string, collectorType int) prometheus.Collector {
+	switch collectorType {
+	case histogram:
+		return p.histograms[name]
+	case gauge:
+		return p.histograms[name]
+	default:
+		panic("prometheus metrics get collector error, illegal collector type")

Review Comment:
   updated.



##########
eventmesh-workflow-go/internal/constants/constants.go:
##########
@@ -68,3 +68,23 @@ const (
 	EventPropsWorkflowInstanceID     = "workflowinstanceid"
 	EventPropsWorkflowTaskInstanceID = "workflowtaskinstanceid"
 )
+
+// Metrics Related constants
+const (
+	MetricsEventTask     = "event_task"
+	MetricsOperationTask = "operation_task"
+	MetricsSwitchTask    = "switch_task"
+	MetricsScheduler     = "scheduler"
+	MetricsEngine        = "engine"
+	MetricsTaskQueue     = "task_queue"
+
+	MetricsTotal        = "total"
+	MetricsSwitchReject = "reject"
+	MetricsSwitchPass   = "pass"
+
+	MetricsStartRequest      = "start_request"

Review Comment:
   updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


[GitHub] [incubator-eventmesh] horoc commented on a diff in pull request #2303: [ISSUE #2196] [Workflow] Add prometheus metrics

Posted by GitBox <gi...@apache.org>.
horoc commented on code in PR #2303:
URL: https://github.com/apache/incubator-eventmesh/pull/2303#discussion_r1037829255


##########
eventmesh-workflow-go/internal/task/event_task.go:
##########
@@ -43,6 +44,7 @@ func NewEventTask(instance *model.WorkflowTaskInstance) Task {
 }
 
 func (t *eventTask) Run() error {
+	metrics.Inc("event_task", "total")

Review Comment:
   ok, I will update it later.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org