-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add meta server for handling prometheus metrics and pprof by…
… yurttunnel (#253)
- Loading branch information
1 parent
6672287
commit 02aa604
Showing
14 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2020 The OpenYurt Authors. | ||
Licensed 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 ( | ||
"strings" | ||
|
||
"github.com/openyurtio/openyurt/pkg/projectinfo" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
namespace = strings.ReplaceAll(projectinfo.GetTunnelName(), "-", "_") | ||
subsystem = "server" | ||
) | ||
|
||
var ( | ||
// Metrics provides access to all tunnel server metrics. | ||
Metrics = newTunnelServerMetrics() | ||
) | ||
|
||
type TunnelServerMetrics struct { | ||
proxyingRequestsCollector *prometheus.GaugeVec | ||
proxyingRequestsGauge prometheus.Gauge | ||
cloudNodeGauge prometheus.Gauge | ||
} | ||
|
||
func newTunnelServerMetrics() *TunnelServerMetrics { | ||
proxyingRequestsCollector := prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "in_proxy_requests", | ||
Help: "how many http requests are proxying by tunnel server", | ||
}, | ||
[]string{"verb", "path"}) | ||
proxyingRequestsGauge := prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "total_in_proxy_requests", | ||
Help: "the number of http requests are proxying by tunnel server", | ||
}) | ||
cloudNodeGauge := prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: "cloud_nodes_counter", | ||
Help: "counter of cloud nodes that do not run tunnel agent", | ||
}) | ||
|
||
prometheus.MustRegister(proxyingRequestsCollector) | ||
prometheus.MustRegister(proxyingRequestsGauge) | ||
prometheus.MustRegister(cloudNodeGauge) | ||
return &TunnelServerMetrics{ | ||
proxyingRequestsCollector: proxyingRequestsCollector, | ||
proxyingRequestsGauge: proxyingRequestsGauge, | ||
cloudNodeGauge: cloudNodeGauge, | ||
} | ||
} | ||
|
||
func (tsm *TunnelServerMetrics) Reset() { | ||
tsm.proxyingRequestsCollector.Reset() | ||
tsm.proxyingRequestsGauge.Set(float64(0)) | ||
tsm.cloudNodeGauge.Set(float64(0)) | ||
} | ||
|
||
func (tsm *TunnelServerMetrics) IncInFlightRequests(verb, path string) { | ||
tsm.proxyingRequestsCollector.WithLabelValues(verb, path).Inc() | ||
tsm.proxyingRequestsGauge.Inc() | ||
} | ||
|
||
func (tsm *TunnelServerMetrics) DecInFlightRequests(verb, path string) { | ||
tsm.proxyingRequestsCollector.WithLabelValues(verb, path).Dec() | ||
tsm.proxyingRequestsGauge.Dec() | ||
} | ||
|
||
func (tsm *TunnelServerMetrics) ObserveCloudNodes(cnt int) { | ||
tsm.cloudNodeGauge.Set(float64(cnt)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/openyurtio/openyurt/pkg/profile" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
// RunMetaServer start a http server for serving metrics and pprof requests. | ||
func RunMetaServer(addr string) { | ||
muxHandler := mux.NewRouter() | ||
muxHandler.Handle("/metrics", promhttp.Handler()) | ||
|
||
// register handler for pprof | ||
profile.Install(muxHandler) | ||
|
||
metaServer := &http.Server{ | ||
Addr: addr, | ||
Handler: muxHandler, | ||
MaxHeaderBytes: 1 << 20, | ||
} | ||
|
||
klog.InfoS("start handling meta requests(metrics/pprof)", "server endpoint", addr) | ||
go func() { | ||
err := metaServer.ListenAndServe() | ||
if err != nil { | ||
klog.ErrorS(err, "meta server could not listen") | ||
} | ||
klog.InfoS("meta server stopped listening", "server endpoint", addr) | ||
}() | ||
} |