Skip to content

The only tool you need to know what is happening and how to fix it.

feat connected be to the db + added client metrics

dduxd90ae903b4a4
diff --git a/backend/app/controllers/auth.controller.go b/backend/app/controllers/auth.controller.go
new file mode 100644
index 00000000..0170d07e
--- /dev/null
+++ b/backend/app/controllers/auth.controller.go
@@ -0,0 +1,39 @@
+package controllers
+
+import (
+ "net/http"
+ "os"
+
+ "github.com/gin-gonic/gin"
+)
+
+type authController struct{}
+
+type LoginRequest struct {
+ Token string `json:"token" binding:"required"`
+}
+
+func (a authController) Login(c *gin.Context) {
+ var request LoginRequest
+ if err := c.ShouldBindJSON(&request); err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
+ return
+ }
+
+ appToken := os.Getenv("APP_TOKEN")
+ if appToken == "" {
+ // Fallback or error if env not set, for safety.
+ // Assuming config is required.
+ c.JSON(http.StatusInternalServerError, gin.H{"error": "Server misconfiguration"})
+ return
+ }
+
+ if request.Token != appToken {
+ c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{"status": "ok"})
+}
+
+var AuthController = authController{}
diff --git a/backend/app/controllers/routes.go b/backend/app/controllers/routes.go
index 27291b57..80fc6962 100644
--- a/backend/app/controllers/routes.go
+++ b/backend/app/controllers/routes.go
@@ -31,4 +31,7 @@ func RegisterControllers(router *gin.RouterGroup) {
router.POST("/transactions", middleware.UseAppAuth, middleware.UseGzip, TransactionController.FindAllTransactions)
router.POST("/exception-stack-traces", middleware.UseAppAuth, middleware.UseGzip, ExceptionStackTraceController.FindGrouppedExceptionStackTraces)
+
+ // Auth
+ router.POST("/login", AuthController.Login)
}
diff --git a/backend/app/migrations/ch/0001_init.up.sql b/backend/app/migrations/ch/0001_created_transactions.up.sql
similarity index 100%
rename from backend/app/migrations/ch/0001_init.up.sql
rename to backend/app/migrations/ch/0001_created_transactions.up.sql
diff --git a/backend/app/migrations/ch/0002_init.up.sql b/backend/app/migrations/ch/0002_created_metric_records.up.sql
similarity index 75%
rename from backend/app/migrations/ch/0002_init.up.sql
rename to backend/app/migrations/ch/0002_created_metric_records.up.sql
index 3c1b81b1..a02674b3 100644
--- a/backend/app/migrations/ch/0002_init.up.sql
+++ b/backend/app/migrations/ch/0002_created_metric_records.up.sql
@@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS metric_records (
- name String,
- value Float32,
+ name LowCardinality(String),
+ value Float64,
recorded_at DateTime
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(recorded_at)
diff --git a/backend/app/migrations/ch/0003_init.up.sql b/backend/app/migrations/ch/0003_created_exception_stack_traces.up.sql
similarity index 100%
rename from backend/app/migrations/ch/0003_init.up.sql
rename to backend/app/migrations/ch/0003_created_exception_stack_traces.up.sql
diff --git a/backend/app/models/clientmodels/clientmodels.model.go b/backend/app/models/clientmodels/clientmodels.model.go
index 414511a6..3ab2d4d6 100644
--- a/backend/app/models/clientmodels/clientmodels.model.go
+++ b/backend/app/models/clientmodels/clientmodels.model.go
@@ -22,7 +22,7 @@ func (c *ClientExceptionStackTrace) ToExceptionStackTrace(exceptionHash string)
type ClientMetricRecord struct {
Name string `json:"name"`
- Value float32 `json:"value"`
+ Value float64 `json:"value"`
RecordedAt time.Time `json:"recordedAt"`
}
diff --git a/backend/app/models/metric_record.model.go b/backend/app/models/metric_record.model.go
index aa53282a..a68dd484 100644
--- a/backend/app/models/metric_record.model.go
+++ b/backend/app/models/metric_record.model.go
@@ -4,11 +4,17 @@ import "time"
type MetricRecord struct {
Name string `json:"name" ch:"name"`
- Value float32 `json:"value" ch:"value"`
+ Value float64 `json:"value" ch:"value"`
RecordedAt time.Time `json:"recordedAt" ch:"recorded_at"`
}
const (
- MetricNameMemoryUsage = "memory_usage"
- MetricNameCpuUsage = "cpu_usage"
+ MetricNameMemoryUsage = "mem.used"
+ MetricNameMemoryUsagePcnt = "mem.used_pcnt"
+ MetricNameCpuUsage = "cpu.used_pcnt"
+ MetricNameGoRoutines = "go.go_routines"
+ MetricNameHeapObjects = "go.heap_objects"
+ MetricNameNumGC = "go.num_gc"
+ MetricNameGCPauseTotal = "go.gc_pause"
+ // other metric names are custom and added by the clients
)
diff --git a/backend/go.mod b/backend/go.mod
index 4a33a233..776c9d31 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -2,8 +2,6 @@ module backend
go 1.25.1
-require github.com/jackc/pgx/v5 v5.7.6
-
require (
github.com/ClickHouse/ch-go v0.69.0 // indirect
github.com/andybalholm/brotli v1.2.0 // indirect
@@ -23,8 +21,8 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
+ github.com/kr/pretty v0.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
- github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
@@ -36,7 +34,6 @@ require (
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/segmentio/asm v1.2.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
- github.com/tracewayapp/go-lightning v0.1.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
go.opentelemetry.io/otel v1.39.0 // indirect
@@ -49,6 +46,7 @@ require (
golang.org/x/sys v0.39.0 // indirect
golang.org/x/tools v0.39.0 // indirect
google.golang.org/protobuf v1.36.9 // indirect
+ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
require (
@@ -56,11 +54,7 @@ require (
github.com/gin-gonic/gin v1.11.0
github.com/golang-migrate/migrate/v4 v4.19.1
github.com/google/uuid v1.6.0 // indirect
- github.com/jackc/pgpassfile v1.0.0 // indirect
- github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
- github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/joho/godotenv v1.5.1
- github.com/tracewayapp/go-lightning/lpg v0.1.2
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/text v0.32.0 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index 07d3c74c..1e3e7a21 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -6,8 +6,6 @@ github.com/ClickHouse/clickhouse-go v1.4.3 h1:iAFMa2UrQdR5bHJ2/yaSLffZkxpcOYQMCU
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/ClickHouse/clickhouse-go/v2 v2.42.0 h1:MdujEfIrpXesQUH0k0AnuVtJQXk6RZmxEhsKUCcv5xk=
github.com/ClickHouse/clickhouse-go/v2 v2.42.0/go.mod h1:riWnuo4YMVdajYll0q6FzRBomdyCrXyFY3VXeXczA8s=
-github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
-github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
@@ -26,6 +24,7 @@ github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -80,14 +79,6 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
-github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
-github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
-github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
-github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
-github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
-github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
-github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
-github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -100,6 +91,7 @@ github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYW
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -143,6 +135,7 @@ github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
+github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
@@ -154,17 +147,12 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
-github.com/tracewayapp/go-lightning v0.1.1 h1:3WEjXX6rpdrgMZ9O+xQukKFtE82/NeeHVer+7fiMqA8=
-github.com/tracewayapp/go-lightning v0.1.1/go.mod h1:1Dva5sVNLroXihuG+tzgz54KwuCUR0vu+bQUi/S8iVM=
-github.com/tracewayapp/go-lightning/lpg v0.1.2 h1:1Nj7vUAD0ycHmg8LNQlxmBERojfCFOO4BfDlank6Yzo=
-github.com/tracewayapp/go-lightning/lpg v0.1.2/go.mod h1:49WRH/8LKeXGZ2F40y1gR6ZCPQIeBjHL2rFrite0ACM=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
@@ -251,6 +239,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
+gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/clients/go-client/devtesting/devtesting.go b/clients/go-client/devtesting/devtesting.go
index ab372770..1b368116 100644
--- a/clients/go-client/devtesting/devtesting.go
+++ b/clients/go-client/devtesting/devtesting.go
@@ -12,6 +12,7 @@ func main() {
}
func testGin() {
+
router := gin.Default()
router.Use(tracewaygin.New(
@@ -24,6 +25,23 @@ func testGin() {
panic("Cool")
})
+ router.GET("/test-ok", func(ctx *gin.Context) {
+ ctx.JSON(200, gin.H{
+ "status": "ok",
+ })
+ })
+ router.GET("/test-not-found", func(ctx *gin.Context) {
+ ctx.JSON(404, gin.H{
+ "status": "not-found",
+ })
+ })
+
+ router.GET("/test-param/:param", func(ctx *gin.Context) {
+ ctx.JSON(200, gin.H{
+ "param": ctx.Param("param"),
+ })
+ })
+
router.GET("/metrics", func(ctx *gin.Context) {
traceway.PrintCollectionFrameMetrics()
})
diff --git a/clients/go-client/go.mod b/clients/go-client/go.mod
index 8499e47f..16725670 100644
--- a/clients/go-client/go.mod
+++ b/clients/go-client/go.mod
@@ -4,6 +4,16 @@ go 1.25.1
require github.com/gin-gonic/gin v1.11.0
+require (
+ github.com/go-ole/go-ole v1.2.6 // indirect
+ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
+ github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
+ github.com/shoenig/go-m1cpu v0.1.6 // indirect
+ github.com/tklauser/go-sysconf v0.3.12 // indirect
+ github.com/tklauser/numcpus v0.6.1 // indirect
+ github.com/yusufpapurcu/wmi v1.2.4 // indirect
+)
+
require (
github.com/bytedance/sonic v1.14.0 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect
@@ -25,6 +35,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect
+ github.com/shirou/gopsutil/v3 v3.24.5
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
go.uber.org/mock v0.5.0 // indirect
diff --git a/clients/go-client/go.sum b/clients/go-client/go.sum
index 42892893..b175be3c 100644
--- a/clients/go-client/go.sum
+++ b/clients/go-client/go.sum
@@ -12,6 +12,8 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
+github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
+github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@@ -22,6 +24,7 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
+github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -31,6 +34,8 @@ github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzh
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
+github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
+github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
@@ -40,10 +45,16 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
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/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
+github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
+github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
+github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
+github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -51,10 +62,16 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
+github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
+github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
+github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
+github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
+github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
@@ -67,13 +84,18 @@ golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
+golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
diff --git a/clients/go-client/metrics/cpu/cpu_darwin.go b/clients/go-client/metrics/cpu/cpu_darwin.go
new file mode 100644
index 00000000..de795fec
--- /dev/null
+++ b/clients/go-client/metrics/cpu/cpu_darwin.go
@@ -0,0 +1,106 @@
+//go:build darwin
+
+package cpu
+
+/*
+#include <mach/mach.h>
+#include <mach/processor_info.h>
+#include <mach/mach_host.h>
+
+typedef struct {
+ unsigned int user;
+ unsigned int system;
+ unsigned int idle;
+ unsigned int nice;
+} cpu_usage_t;
+
+int get_cpu_ticks(cpu_usage_t *usage) {
+ mach_msg_type_number_t count;
+ processor_cpu_load_info_t cpu_load;
+ natural_t num_cpus;
+
+ kern_return_t ret = host_processor_info(
+ mach_host_self(),
+ PROCESSOR_CPU_LOAD_INFO,
+ &num_cpus,
+ (processor_info_array_t *)&cpu_load,
+ &count
+ );
+
+ if (ret != KERN_SUCCESS) {
+ return -1;
+ }
+
+ usage->user = 0;
+ usage->system = 0;
+ usage->idle = 0;
+ usage->nice = 0;
+
+ for (natural_t i = 0; i < num_cpus; i++) {
+ usage->user += cpu_load[i].cpu_ticks[CPU_STATE_USER];
+ usage->system += cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM];
+ usage->idle += cpu_load[i].cpu_ticks[CPU_STATE_IDLE];
+ usage->nice += cpu_load[i].cpu_ticks[CPU_STATE_NICE];
+ }
+
+ vm_deallocate(mach_task_self(), (vm_address_t)cpu_load,
+ count * sizeof(*cpu_load));
+
+ return 0;
+}
+*/
+import "C"
+import (
+ "fmt"
+ "time"
+)
+
+type CPUTicks struct {
+ User uint32
+ System uint32
+ Idle uint32
+ Nice uint32
+}
+
+func getCPUTicks() (CPUTicks, error) {
+ var usage C.cpu_usage_t
+ ret := C.get_cpu_ticks(&usage)
+ if ret != 0 {
+ return CPUTicks{}, fmt.Errorf("failed to get CPU ticks")
+ }
+ return CPUTicks{
+ User: uint32(usage.user),
+ System: uint32(usage.system),
+ Idle: uint32(usage.idle),
+ Nice: uint32(usage.nice),
+ }, nil
+}
+
+func (t CPUTicks) Total() uint64 {
+ return uint64(t.User) + uint64(t.System) + uint64(t.Idle) + uint64(t.Nice)
+}
+
+// GetCPUUsage returns CPU usage percentage over the given duration
+func GetCpuPercent(interval time.Duration) (float64, error) {
+ ticks1, err := getCPUTicks()
+ if err != nil {
+ return 0, err
+ }
+
+ time.Sleep(interval)
+
+ ticks2, err := getCPUTicks()
+ if err != nil {
+ return 0, err
+ }
+
+ totalDiff := float64(ticks2.Total() - ticks1.Total())
+ if totalDiff == 0 {
+ return 0, nil
+ }
+
+ idleDiff := float64(ticks2.Idle - ticks1.Idle)
+ usage := 100.0 * (1.0 - idleDiff/totalDiff)
+
+ return usage, nil
+}
diff --git a/clients/go-client/metrics/cpu/cpu_linux.go b/clients/go-client/metrics/cpu/cpu_linux.go
new file mode 100644
index 00000000..9f4c4f35
--- /dev/null
+++ b/clients/go-client/metrics/cpu/cpu_linux.go
@@ -0,0 +1,72 @@
+//go:build linux
+
+package cpu
+
+import (
+ "bufio"
+ "os"
+ "strconv"
+ "strings"
+ "time"
+)
+
+// TODO: TEST LINUX CPU READING
+
+func GetCpuPercent(interval time.Duration) (float64, error) {
+ idle1, total1, err := readLinuxCPUStat()
+ if err != nil {
+ return 0, err
+ }
+
+ time.Sleep(interval)
+
+ idle2, total2, err := readLinuxCPUStat()
+ if err != nil {
+ return 0, err
+ }
+
+ idleDelta := float64(idle2 - idle1)
+ totalDelta := float64(total2 - total1)
+
+ if totalDelta == 0 {
+ return 0, nil
+ }
+
+ return (1.0 - idleDelta/totalDelta) * 100, nil
+}
+
+func readLinuxCPUStat() (idle, total uint64, err error) {
+ file, err := os.Open("/proc/stat")
+ if err != nil {
+ return 0, 0, err
+ }
+ defer file.Close()
+
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ line := scanner.Text()
+ if strings.HasPrefix(line, "cpu ") {
+ fields := strings.Fields(line)
+ if len(fields) < 5 {
+ continue
+ }
+ for i, field := range fields[1:] {
+ val, _ := strconv.ParseUint(field, 10, 64)
+ total += val
+ if i == 3 {
+ idle = val
+ }
+ }
+ break
+ }
+ }
+ return idle, total, scanner.Err()
+}
+
+func getDarwinCPUPercent(interval time.Duration) (float64, error) {
+ return 0, nil
+}
+
+func getWindowsCPUPercent(interval time.Duration) (float64, error) {
+ return 0, nil
+}
diff --git a/clients/go-client/metrics/cpu/cpu_windows.go b/clients/go-client/metrics/cpu/cpu_windows.go
new file mode 100644
index 00000000..f5ed8e60
--- /dev/null
+++ b/clients/go-client/metrics/cpu/cpu_windows.go
@@ -0,0 +1,62 @@
+//go:build windows
+
+package cpu
+
+import (
+ "time"
+ "unsafe"
+
+ "golang.org/x/sys/windows"
+)
+
+// TODO: TEST WINDOWS CPU READING
+
+var (
+ modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
+ procGetSystemTimes = modkernel32.NewProc("GetSystemTimes")
+)
+
+func GetCpuPercent(interval time.Duration) (float64, error) {
+ idle1, kernel1, user1, err := getSystemTimes()
+ if err != nil {
+ return 0, err
+ }
+
+ time.Sleep(interval)
+
+ idle2, kernel2, user2, err := getSystemTimes()
+ if err != nil {
+ return 0, err
+ }
+
+ idleDelta := idle2 - idle1
+ kernelDelta := kernel2 - kernel1
+ userDelta := user2 - user1
+
+ totalDelta := kernelDelta + userDelta
+ if totalDelta == 0 {
+ return 0, nil
+ }
+
+ // kernel time includes idle time
+ return float64(totalDelta-idleDelta) / float64(totalDelta) * 100, nil
+}
+
+func getSystemTimes() (idle, kernel, user uint64, err error) {
+ var idleTime, kernelTime, userTime windows.Filetime
+
+ ret, _, err := procGetSystemTimes.Call(
+ uintptr(unsafe.Pointer(&idleTime)),
+ uintptr(unsafe.Pointer(&kernelTime)),
+ uintptr(unsafe.Pointer(&userTime)),
+ )
+ if ret == 0 {
+ return 0, 0, 0, err
+ }
+
+ return fileTimeToUint64(idleTime), fileTimeToUint64(kernelTime), fileTimeToUint64(userTime), nil
+}
+
+func fileTimeToUint64(ft windows.Filetime) uint64 {
+ return uint64(ft.HighDateTime)<<32 | uint64(ft.LowDateTime)
+}
diff --git a/clients/go-client/metrics/mem/mem_darwin.go b/clients/go-client/metrics/mem/mem_darwin.go
new file mode 100644
index 00000000..137eb43a
--- /dev/null
+++ b/clients/go-client/metrics/mem/mem_darwin.go
@@ -0,0 +1,62 @@
+//go:build darwin
+
+package mem
+
+import (
+ "encoding/binary"
+ "fmt"
+ "syscall"
+ "unsafe"
+)
+
+func GetMemoryUsedPercent() (float64, error) {
+ mem, err := getTotalVirtualMemory()
+ if err != nil {
+ return 0, err
+ }
+
+ return float64(mem) / 1024 / 1024, nil
+}
+
+func getTotalVirtualMemory() (uint64, error) {
+ // Get physical memory size using sysctl
+ physMem, err := syscall.Sysctl("hw.memsize")
+ if err != nil {
+ return 0, fmt.Errorf("failed to get hw.memsize: %w", err)
+ }
+
+ // The result is a binary representation of uint64
+ // syscall.Sysctl returns a string, but the data is actually binary
+ if len(physMem) < 8 {
+ // Pad if necessary
+ padded := make([]byte, 8)
+ copy(padded, []byte(physMem))
+ return binary.LittleEndian.Uint64(padded), nil
+ }
+
+ return binary.LittleEndian.Uint64([]byte(physMem)[:8]), nil
+}
+
+// Alternative using SysctlUint64 (cleaner approach)
+func getTotalPhysicalMemory() (uint64, error) {
+ mib := []int32{6, 24} // CTL_HW, HW_MEMSIZE
+
+ var value uint64
+ size := uintptr(unsafe.Sizeof(value))
+
+ _, _, errno := syscall.Syscall6(
+ syscall.SYS___SYSCTL,
+ uintptr(unsafe.Pointer(&mib[0])),
+ uintptr(len(mib)),
+ uintptr(unsafe.Pointer(&value)),
+ uintptr(unsafe.Pointer(&size)),
+ 0,
+ 0,
+ )
+
+ if errno != 0 {
+ return 0, errno
+ }
+
+ return value, nil
+}
diff --git a/clients/go-client/metrics/mem/mem_linux.go b/clients/go-client/metrics/mem/mem_linux.go
new file mode 100644
index 00000000..5a1dc4e6
--- /dev/null
+++ b/clients/go-client/metrics/mem/mem_linux.go
@@ -0,0 +1,38 @@
+//go:build linux
+
+package mem
+
+import (
+ "bufio"
+ "os"
+ "strconv"
+ "strings"
+)
+
+// TODO: TEST LINUX MEMORY READING
+
+// Linux: read /proc/meminfo
+func GetMemoryUsedPercent() (float64, error) {
+ file, err := os.Open("/proc/meminfo")
+ if err != nil {
+ return 0, err
+ }
+ defer file.Close()
+
+ var total, available uint64
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ fields := strings.Fields(scanner.Text())
+ if len(fields) < 2 {
+ continue
+ }
+ val, _ := strconv.ParseUint(fields[1], 10, 64)
+ switch fields[0] {
+ case "MemTotal:":
+ total = val
+ case "MemAvailable:":
+ available = val
+ }
+ }
+ return float64(total-available) / float64(total) * 100, nil
+}
diff --git a/clients/go-client/metrics/mem/mem_windows.go b/clients/go-client/metrics/mem/mem_windows.go
new file mode 100644
index 00000000..592b8f37
--- /dev/null
+++ b/clients/go-client/metrics/mem/mem_windows.go
@@ -0,0 +1,29 @@
+package mem
+
+import (
+ "syscall"
+ "unsafe"
+)
+
+// TODO: TEST WINDOWS MEMORY READING
+
+// Windows: call GlobalMemoryStatusEx
+func GetMemoryUsedPercent() (float64, error) {
+ kernel32 := syscall.NewLazyDLL("kernel32.dll")
+ proc := kernel32.NewProc("GlobalMemoryStatusEx")
+
+ var mem struct {
+ dwLength uint32
+ dwMemoryLoad uint32
+ ullTotalPhys uint64
+ ullAvailPhys uint64
+ _ [4]uint64 // remaining fields we don't need
+ }
+ mem.dwLength = uint32(unsafe.Sizeof(mem))
+
+ ret, _, err := proc.Call(uintptr(unsafe.Pointer(&mem)))
+ if ret == 0 {
+ return 0, err
+ }
+ return float64(mem.dwMemoryLoad), nil // Windows gives us the percentage directly
+}
diff --git a/clients/go-client/traceway.go b/clients/go-client/traceway.go
index db00c9d1..34d5bfc1 100644
--- a/clients/go-client/traceway.go
+++ b/clients/go-client/traceway.go
@@ -13,6 +13,8 @@ import (
"strings"
"sync"
"time"
+ "traceway/metrics/cpu"
+ "traceway/metrics/mem"
"github.com/gin-gonic/gin"
)
@@ -81,9 +83,9 @@ type ExceptionStackTrace struct {
RecordedAt time.Time `json:"recordedAt"`
}
-type MetricsRecord struct {
+type MetricRecord struct {
Name string `json:"name"`
- Value float32 `json:"value"`
+ Value float64 `json:"value"`
RecordedAt time.Time `json:"recordedAt"`
}
@@ -99,7 +101,7 @@ type Transaction struct {
type CollectionFrame struct {
StackTraces []*ExceptionStackTrace `json:"stackTraces"`
- Metrics []*MetricsRecord `json:"metrics"`
+ Metrics []*MetricRecord `json:"metrics"`
Transactions []*Transaction `json:"transactions"`
}
@@ -115,7 +117,7 @@ const (
type CollectionFrameMessage struct {
msgType collectionFrameMessageType
exceptionStackTrace *ExceptionStackTrace
- metric *MetricsRecord
+ metric *MetricRecord
transaction *Transaction
collectionFramesToRemove []*CollectionFrame
}
@@ -142,6 +144,7 @@ type CollectionFrameStore struct {
maxCollectionFrames int
collectionInterval time.Duration
uploadTimeout time.Duration
+ metricsInterval time.Duration
}
func InitCollectionFrameStore(
@@ -152,6 +155,7 @@ func InitCollectionFrameStore(
maxCollectionFrames int,
collectionInterval time.Duration,
uploadTimeout time.Duration,
+ metricsInterval time.Duration,
) *CollectionFrameStore {
store := &CollectionFrameStore{
current: nil,
@@ -168,14 +172,72 @@ func InitCollectionFrameStore(
maxCollectionFrames: maxCollectionFrames,
collectionInterval: collectionInterval,
uploadTimeout: uploadTimeout,
+ metricsInterval: metricsInterval,
}
store.wg.Add(1)
go store.process()
+ go store.processMetrics()
return store
}
+const (
+ MetricNameMemoryUsage = "mem.used"
+ MetricNameMemoryUsagePcnt = "mem.used_pcnt"
+ MetricNameCpuUsage = "cpu.used_pcnt"
+ MetricNameGoRoutines = "go.go_routines"
+ MetricNameHeapObjects = "go.heap_objects"
+ MetricNameNumGC = "go.num_gc"
+ MetricNameGCPauseTotal = "go.gc_pause"
+)
+
+func (s *CollectionFrameStore) processMetrics() {
+ metricsTicker := time.NewTicker(s.metricsInterval)
+ defer metricsTicker.Stop()
+
+ for {
+ select {
+ case <-s.stopCh:
+ return
+ case <-metricsTicker.C:
+ s.safeProcessMetrics()
+ }
+ }
+}
+
+func (s *CollectionFrameStore) safeProcessMetrics() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Print("Traceway: failed to get metrics data")
+ }
+ }()
+ cpuPercent, err := cpu.GetCpuPercent(time.Second)
+ if err == nil {
+ CaptureMetric(MetricNameCpuUsage, cpuPercent)
+ } else {
+ if s.debug {
+ log.Println("Traceway cpu not read", err)
+ }
+ }
+
+ memPercent, err := mem.GetMemoryUsedPercent()
+ if err == nil {
+ CaptureMetric(MetricNameMemoryUsagePcnt, memPercent)
+ } else {
+ if s.debug {
+ log.Println("Traceway mem not read", err)
+ }
+ }
+
+ var m runtime.MemStats
+ runtime.ReadMemStats(&m)
+ CaptureMetric(MetricNameMemoryUsage, float64(m.Alloc)/1024/1024)
+ CaptureMetric(MetricNameGoRoutines, float64(runtime.NumGoroutine()))
+ CaptureMetric(MetricNameHeapObjects, float64(m.HeapObjects))
+ CaptureMetric(MetricNameNumGC, float64(m.NumGC))
+ CaptureMetric(MetricNameGCPauseTotal, float64(m.PauseTotalNs))
+}
func (s *CollectionFrameStore) process() {
defer s.wg.Done()
@@ -320,6 +382,7 @@ type TracewayOptions struct {
maxCollectionFrames int
collectionInterval time.Duration
uploadTimeout time.Duration
+ metricsInterval time.Duration
}
func NewTracewayOptions(options ...func(*TracewayOptions)) *TracewayOptions {
@@ -327,6 +390,7 @@ func NewTracewayOptions(options ...func(*TracewayOptions)) *TracewayOptions {
maxCollectionFrames: 12,
collectionInterval: 5 * time.Second,
uploadTimeout: 2 * time.Second,
+ metricsInterval: 30 * time.Second,
}
for _, o := range options {
o(svr)
@@ -353,6 +417,11 @@ func WithUploadTimeout(val time.Duration) func(*TracewayOptions) {
s.uploadTimeout = val
}
}
+func WithMetricsInterval(val time.Duration) func(*TracewayOptions) {
+ return func(s *TracewayOptions) {
+ s.collectionInterval = val
+ }
+}
func Init(app, connectionString string, options ...func(*TracewayOptions)) error {
if collectionFrameStore != nil {
@@ -373,14 +442,15 @@ func Init(app, connectionString string, options ...func(*TracewayOptions)) error
tracewayOptions.maxCollectionFrames,
tracewayOptions.collectionInterval,
tracewayOptions.uploadTimeout,
+ tracewayOptions.metricsInterval,
)
return nil
}
-func CaptureMetric(name string, value float32) {
+func CaptureMetric(name string, value float64) {
collectionFrameStore.messageQueue <- CollectionFrameMessage{
msgType: CollectionFrameMessageTypeMetric,
- metric: &MetricsRecord{
+ metric: &MetricRecord{
Name: name,
Value: value,
RecordedAt: time.Now(),