Skip to content

Commit

Permalink
test sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 15, 2024
1 parent 3fc0676 commit 83858f3
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 146 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ RUN go build -o /app/bin/sentinel ./
FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
ENV GIN_MODE=release
COPY --from=build /app/ /app
COPY --from=build /app/bin/sentinel /app/sentinel
CMD ["/app/sentinel"]
CMD ["/app/sentinel"]
1 change: 1 addition & 0 deletions Dockerfile.arm64
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ RUN go build -o /app/bin/sentinel ./
FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
ENV GIN_MODE=release
COPY --from=build /app/ /app
COPY --from=build /app/bin/sentinel /app/sentinel
CMD ["/app/sentinel"]
42 changes: 7 additions & 35 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"database/sql"
"fmt"
"io"
"log"
Expand All @@ -17,11 +16,6 @@ import (
"github.com/shirou/gopsutil/mem"
)

var (
stmtInsertCPU *sql.Stmt
stmtInsertMemory *sql.Stmt
)

type ContainerMetrics struct {
Name string `json:"name"`
Time string `json:"time"`
Expand All @@ -31,33 +25,9 @@ type ContainerMetrics struct {
MemoryAvailable uint64 `json:"available_memory"`
}

func prepareStatements() error {
var err error
stmtInsertCPU, err = db.Prepare(`INSERT INTO container_cpu_usage (time, container_id, percent) VALUES (?, ?, ?)`)
if err != nil {
return fmt.Errorf("error preparing CPU insert statement: %v", err)
}
stmtInsertMemory, err = db.Prepare(`INSERT INTO container_memory_usage (time, container_id, total, available, used, usedPercent, free) VALUES (?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return fmt.Errorf("error preparing memory insert statement: %v", err)
}
return nil
}

func collector() {
fmt.Printf("[%s] Starting metrics recorder with refresh rate of %d seconds and retention period of %d days.\n", time.Now().Format("2006-01-02 15:04:05"), refreshRateSeconds, collectorRetentionPeriodDays)

if err := prepareStatements(); err != nil {
log.Fatalf("Failed to prepare statements: %v", err)
}
defer func() {
if stmtInsertCPU != nil {
stmtInsertCPU.Close()
}
if stmtInsertMemory != nil {
stmtInsertMemory.Close()
}
}()
go func() {
ticker := time.NewTicker(time.Duration(refreshRateSeconds) * time.Second)
defer ticker.Stop()
Expand Down Expand Up @@ -122,7 +92,7 @@ func collector() {
defer stats.Body.Close()

var v types.StatsJSON
dec := json.NewDecoder(stats.Body)
dec := JSON.NewDecoder(stats.Body)
if err := dec.Decode(&v); err != nil {
if err != io.EOF {
errChannel <- fmt.Errorf("Error decoding container stats for %s: %v", containerNameFromLabel, err)
Expand Down Expand Up @@ -153,12 +123,14 @@ func collector() {
}

for metrics := range metricsChannel {
_, err = stmtInsertCPU.Exec(queryTimeInUnixString, metrics.Name, fmt.Sprintf("%.2f", metrics.CPUUsagePercentage))
_, err = db.Exec(`INSERT INTO container_cpu_usage (time, container_id, percent) VALUES (?, ?, ?)`,
queryTimeInUnixString, metrics.Name, fmt.Sprintf("%.2f", metrics.CPUUsagePercentage))
if err != nil {
log.Printf("Error inserting container CPU usage into database: %v", err)
}

_, err = stmtInsertMemory.Exec(queryTimeInUnixString, metrics.Name, metrics.MemoryAvailable, metrics.MemoryAvailable, metrics.MemoryUsed, metrics.MemoryUsagePercentage, metrics.MemoryAvailable-metrics.MemoryUsed)
_, err = db.Exec(`INSERT INTO container_memory_usage (time, container_id, total, available, used, usedPercent, free) VALUES (?, ?, ?, ?, ?, ?, ?)`,
queryTimeInUnixString, metrics.Name, metrics.MemoryAvailable, metrics.MemoryAvailable, metrics.MemoryUsed, metrics.MemoryUsagePercentage, metrics.MemoryAvailable-metrics.MemoryUsed)
if err != nil {
log.Printf("Error inserting container memory usage into database: %v", err)
}
Expand Down Expand Up @@ -199,8 +171,8 @@ func collector() {
}
}

checkpoint()
vacuum()
// checkpoint()
// vacuum()

cleanupTable("cpu_usage")
cleanupTable("memory_usage")
Expand Down
19 changes: 5 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ go 1.23.1
require (
github.com/docker/docker v27.3.1+incompatible
github.com/gin-gonic/gin v1.10.0
github.com/json-iterator/go v1.1.12
github.com/marcboeker/go-duckdb v1.8.2
github.com/goccy/go-json v0.10.3
github.com/mattn/go-sqlite3 v1.14.24
github.com/shirou/gopsutil v3.21.11+incompatible
)

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/apache/arrow/go/v17 v17.0.0 // indirect
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand All @@ -30,14 +29,12 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -46,14 +43,12 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 // indirect
Expand All @@ -62,16 +57,12 @@ require (
go.opentelemetry.io/otel/trace v1.31.0 // indirect
golang.org/x/arch v0.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.26.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
Loading

0 comments on commit 83858f3

Please sign in to comment.