-
Notifications
You must be signed in to change notification settings - Fork 1
61 prometheus integration #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avii778
wants to merge
4
commits into
main
Choose a base branch
from
61-prometheus-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88233e5
Added prometheus integration and monitoring
avii778 e8ee411
Added prometheuss integration and monitoring
avii778 4c87de2
Fixed issues and added prometheus and grafana setup
avii778 8f1e33b
Merge branch 'main' into 61-prometheus-integration
blobcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
cpuinfo "github.com/shirou/gopsutil/v3/cpu" | ||
diskinfo "github.com/shirou/gopsutil/v3/disk" | ||
meminfo "github.com/shirou/gopsutil/v3/mem" | ||
) | ||
|
||
func handle() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
http.ListenAndServe(":2112", nil) | ||
} | ||
|
||
// http metrics | ||
var ( | ||
httpInFlight = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "http_in_flight_requests", | ||
Help: "Current number of in-flight HTTP requests.", | ||
}) | ||
httpRequestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "http_requests_total", | ||
Help: "Total HTTP requests by handler/method/status.", | ||
}, []string{"handler", "method", "code"}) | ||
httpRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "HTTP request latency in seconds.", | ||
Buckets: prometheus.DefBuckets, | ||
}, []string{"handler", "method", "code"}) | ||
) | ||
|
||
// job metrics | ||
var ( | ||
jobsStarted = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "jobs_started_total", | ||
Help: "Total number of jobs started.", | ||
}, []string{"job_type", "gpu"}) | ||
jobsCompleted = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "jobs_completed_total", | ||
Help: "Total number of jobs completed successfully.", | ||
}, []string{"job_type", "gpu"}) | ||
jobsFailed = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "jobs_failed_total", | ||
Help: "Total number of jobs that failed.", | ||
}, []string{"job_type", "gpu"}) | ||
runningJobs = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "running_jobs", | ||
Help: "Total jobs currently running", | ||
}, []string{"gpu"}) | ||
) | ||
|
||
// system metrics (cpu, memory, disk, etc.) | ||
var ( | ||
// cpu (percent of total) | ||
systemCPUPercent = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "system_cpu_usage_percent", | ||
Help: "Host CPU usage percentage (all cores averaged).", | ||
}) | ||
|
||
// memory | ||
systemMemTotalBytes = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "system_memory_total_bytes", | ||
Help: "Host total memory bytes.", | ||
}) | ||
systemMemUsedBytes = promauto.NewGauge(prometheus.GaugeOpts{ | ||
Name: "system_memory_used_bytes", | ||
Help: "Host used memory bytes.", | ||
}) | ||
|
||
// disk per mountpoint | ||
systemDiskTotalBytes = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "system_disk_total_bytes", | ||
Help: "Total disk bytes for a mountpoint.", | ||
}, []string{"mountpoint"}) | ||
|
||
systemDiskUsedBytes = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "system_disk_used_bytes", | ||
Help: "Used disk bytes for a mountpoint.", | ||
}, []string{"mountpoint"}) | ||
) | ||
|
||
// this function may need to run per server to capture local system metrics | ||
|
||
func startSystemCollector(ctx context.Context) { | ||
blobcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
go func() { | ||
ticker := time.NewTicker(5 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
||
case <-ticker.C: | ||
|
||
// cpu | ||
pct, err := cpuinfo.Percent(0, false) | ||
|
||
if err == nil && len(pct) > 0 { | ||
systemCPUPercent.Set(pct[0]) | ||
} else { | ||
// TODO: log err | ||
} | ||
|
||
// memory | ||
m, err := meminfo.VirtualMemory() | ||
if err == nil { | ||
systemMemTotalBytes.Set(float64(m.Total)) | ||
systemMemUsedBytes.Set(float64(m.Used)) | ||
} else { | ||
// TODO: log err | ||
} | ||
|
||
// disk capture | ||
parts, err := diskinfo.Partitions(false) | ||
if err == nil { | ||
for _, p := range parts { | ||
if u, err := diskinfo.Usage(p.Mountpoint); err == nil { | ||
// Use a consistent label key (e.g., mountpoint) | ||
systemDiskTotalBytes.WithLabelValues(u.Path).Set(float64(u.Total)) | ||
systemDiskUsedBytes.WithLabelValues(u.Path).Set(float64(u.Used)) | ||
} else { | ||
// TODO: log err | ||
blobcode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} else { | ||
// TODO: log err | ||
} | ||
} | ||
} | ||
}() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.