Skip to content

Commit

Permalink
fixing rebase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Mar 7, 2024
1 parent a5da5bf commit a4331ed
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 207 deletions.
5 changes: 2 additions & 3 deletions adapter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
Expand Down Expand Up @@ -214,7 +214,6 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
6 changes: 0 additions & 6 deletions common-controller/commoncontroller/common_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,6 @@ func InitCommonControllerServer(conf *config.Config) {

loggers.LoggerAPKOperator.Info("Starting common controller ....")

// Start the metrics server
if conf.CommonController.Metrics.Enabled && strings.EqualFold(conf.CommonController.Metrics.Type, metrics.PrometheusMetricType) {
loggers.LoggerAPKOperator.Info("Starting Prometheus Metrics Server ....")
go metrics.StartPrometheusMetricsServer(conf.CommonController.Metrics.Port, conf.CommonController.Metrics.CollectionInterval)
}

rateLimiterCache := xds.GetRateLimiterCache()
rlsSrv := xdsv3.NewServer(ctx, rateLimiterCache, &xds.Callbacks{})

Expand Down
3 changes: 1 addition & 2 deletions common-controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/procfs v0.12.0
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sirupsen/logrus v1.9.0
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
Expand All @@ -23,7 +23,6 @@ require (
github.com/redis/go-redis/v9 v9.2.1
github.com/wso2/apk/adapter v0.0.0-20231214082511-af2c8b8a19f1
github.com/wso2/apk/common-go-libs v0.0.0-20240304050809-a382bc6b0d82
github.com/shirou/gopsutil/v3 v3.24.1
google.golang.org/grpc v1.62.0
)

Expand Down
1 change: 0 additions & 1 deletion common-controller/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
191 changes: 12 additions & 179 deletions common-controller/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,200 +16,33 @@
* under the License.
*/

// Package metrics holds the implementation for exposing common controller metrics to prometheus
// Package metrics holds the implementation for exposing adapter metrics to prometheus
package metrics

import (
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/procfs"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/wso2/apk/adapter/pkg/logging"
logger "github.com/wso2/apk/common-controller/internal/loggers"
metrics "github.com/wso2/apk/common-go-libs/pkg/metrics"
)

var (
prometheusMetricRegistry = prometheus.NewRegistry()

hostInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "host_info",
Help: "Host Info",
}, []string{"os"})

availableCPUs = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "os_available_cpu_total",
Help: "Number of available CPUs.",
})

freePhysicalMemory = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "os_free_physical_memory_bytes",
Help: "Amount of free physical memory.",
})

totalVirtualMemory = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "os_total_virtual_memory_bytes",
Help: "Amount of total virtual memory.",
})
usedVirtualMemory = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "os_used_virtual_memory_bytes",
Help: "Amount of used virtual memory.",
})

systemCPULoad = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "os_system_cpu_load_percentage",
Help: "System-wide CPU usage as a percentage.",
})

loadAvg = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "os_system_load_average",
Help: "Current load of CPU in the host system for the last {x} minutes",
}, []string{"duration"})

processStartTime = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "process_start_time_seconds",
Help: "Start time of the process since unix epoch in seconds.",
})

processOpenFDs = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "process_open_fds",
Help: "Number of open file descriptors.",
})
)

func init() {
// Register the Go collector with the registry
goCollector := collectors.NewGoCollector()
prometheusMetricRegistry.MustRegister(goCollector)

// Register other metrics
prometheusMetricRegistry.MustRegister(hostInfo, availableCPUs, freePhysicalMemory, usedVirtualMemory, totalVirtualMemory,
systemCPULoad, loadAvg, processStartTime, processOpenFDs)
}

// recordMetrics record custom golang metrics
var recordMetrics = func(collectionInterval int32) {
for {
host, err := host.Info()
if handleError(err, "Failed to get host info") {
return
}
hostInfo.WithLabelValues(host.OS).Set(1)
availableCPUs.Set(float64(runtime.NumCPU()))

v, err := mem.VirtualMemory()
if handleError(err, "Failed to read virtual memory metrics") {
return
}
freePhysicalMemory.Set(float64(v.Free))
usedVirtualMemory.Set(float64(v.Used))
totalVirtualMemory.Set(float64(v.Total))

percentages, err := cpu.Percent(0, false)
if handleError(err, "Failed to read cpu usage metrics") || len(percentages) == 0 {
return
}
totalPercentage := 0.0
for _, p := range percentages {
totalPercentage += p
}
averagePercentage := totalPercentage / float64(len(percentages))
systemCPULoad.Set(averagePercentage)

pid := os.Getpid()
p, err := procfs.NewProc(pid)
if handleError(err, "Failed to get current process") {
return
}
stat, err := p.Stat()
if handleError(err, "Failed to get process stats") {
return
}
t, err := stat.StartTime()
if handleError(err, "Failed to read process start time") {
return
}
processStartTime.Set(t)
fds, err := p.FileDescriptorsLen()
if handleError(err, "Failed to read file descriptor count") {
return
}
processOpenFDs.Set(float64(fds))

avg, err := load.Avg()
if handleError(err, "Failed to read cpu load averages") {
return
}
loadAvg.WithLabelValues("1m").Set(avg.Load1)
loadAvg.WithLabelValues("5m").Set(avg.Load5)
loadAvg.WithLabelValues("15m").Set(avg.Load15)

// Sleep before the next measurement
time.Sleep(time.Duration(collectionInterval) * time.Second)
}

}
// StartPrometheusMetricsServer initializes and starts the metrics server to expose metrics to prometheus.
func StartPrometheusMetricsServer(port int32) {

func handleError(err error, message string) bool {
collector := metrics.CustomMetricsCollector()
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":"+strconv.Itoa(int(port)), nil)
if err != nil {
logger.LoggerAPKOperator.ErrorC(logging.ErrorDetails{
Message: fmt.Sprintf(message, err.Error()),
Severity: logging.MINOR,
ErrorCode: 1109,
logger.LoggerAPK.ErrorC(logging.ErrorDetails{
Message: fmt.Sprintln("Prometheus metrics server error:", err),
Severity: logging.MAJOR,
ErrorCode: 1110,
})
return true
}
return false
}

// StartPrometheusMetricsServer initializes and starts the metrics server to expose metrics to prometheus.
func StartPrometheusMetricsServer(port int32, collectionInterval int32) {
done := make(chan struct{}) // Channel to indicate recordMetrics routine exit

// Start the Prometheus metrics server
go func() {
http.Handle("/metrics", promhttp.HandlerFor(prometheusMetricRegistry, promhttp.HandlerOpts{}))
err := http.ListenAndServe(":"+strconv.Itoa(int(port)), nil)
if err != nil {
logger.LoggerAPKOperator.ErrorC(logging.ErrorDetails{
Message: fmt.Sprintln("Prometheus metrics server error:", err),
Severity: logging.MAJOR,
ErrorCode: 1110,
})
}
}()

for {
// Start the recordMetrics goroutine
go func() {
defer func() {
if r := recover(); r != nil {
logger.LoggerAPKOperator.ErrorC(logging.ErrorDetails{
Message: fmt.Sprintln("recordMetrics goroutine exited with error:", r),
Severity: logging.MAJOR,
ErrorCode: 1111,
})
}
}()
recordMetrics(collectionInterval)
done <- struct{}{} // Signal that the goroutine has completed
}()

// Wait for the previous recordMetrics goroutine to complete
<-done

// Log and restart the goroutine
logger.LoggerAPKOperator.Info("Restarting recordMetrics goroutine...")
time.Sleep(3 * time.Second)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ spec:
{{- else }}
subPath: tls.key
{{- end }}
- name: prometheus-jmx-config-volume
mountPath: /tmp/metrics/prometheus-jmx-config-configds.yml
subPath: prometheus-jmx-config-configds.yml
- name: config-ds-tls-volume
mountPath: /home/wso2apk/config-deployer/security/config.pem
{{- if and .Values.wso2.apk.dp.configdeployer.configs .Values.wso2.apk.dp.configdeployer.configs.tls }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,4 @@ spec:
- name: servlet-https
protocol: TCP
port: 9443
{{ if and .Values.wso2.apk.metrics .Values.wso2.apk.metrics.enabled }}
- name: prometheus-config-ds-port-lang
protocol: TCP
port: 18008
- name: prometheus-config-ds-port-system
protocol: TCP
port: 18010
{{ end }}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,4 @@ spec:
- name: https-internal-api
protocol: TCP
port: 18003
{{ if and .Values.wso2.apk.metrics .Values.wso2.apk.metrics.enabled }}
- name: prometheus-common-controller-port
protocol: TCP
port: 18009
{{ end }}
{{- end -}}

0 comments on commit a4331ed

Please sign in to comment.