utils

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultServerURL = "https://app.dbtune.com"
)

Variables

This section is empty.

Functions

func CalculateDockerCPUPercent

func CalculateDockerCPUPercent(previousCPU, previousSystem uint64, v *container.StatsResponse) float64

CalculateDockerCPUPercent calculates the CPU usage percentage for a Docker container Implementation based on: https://github.com/docker/cli/blob/master/cli/command/container/stats_helpers.go

func CalculateDockerMemoryUsed

func CalculateDockerMemoryUsed(mem container.MemoryStats) float64

CalculateDockerMemoryUsed is mirroring the official way to calculate: https://github.com/docker/cli/blob/master/cli/command/container/stats_helpers.go#L239

func CalculateQueryRuntime

func CalculateQueryRuntime(prev, curr map[string]CachedPGStatStatement) float64

CalculateQueryRuntime calculates the runtime of a query based on two consecutive snapshots of the pg_stat_statements table and returns the runtime in milliseconds.

func ValidateStruct

func ValidateStruct(s interface{}) error

ValidateStruct validates a struct using the validator package It returns a single error with all validation errors combined Used to validate configs when we start the agent

Types

type CachedPGStatStatement

type CachedPGStatStatement struct {
	QueryID       string  `json:"query_id"`
	Calls         int     `json:"calls"`
	TotalExecTime float64 `json:"total_exec_time"`
}

func CalculateQueryRuntimeDelta added in v0.2.0

func CalculateQueryRuntimeDelta(prev, curr map[string]CachedPGStatStatement) ([]CachedPGStatStatement, int)

CalculateQueryRuntimeDelta calculates the delta between two consecutive snapshots of the pg_stat_statements and returns: [{ query_id: "query_id", calls: 10, total_exec_time: 1000 }, ...] The diff will be limited to only 100 different queries changed by default. Also, it will return the total number of diffs found to give us an idea if a lot of information is not captured The cap will be based upon their average execution time (descending)

type FlatValue

type FlatValue struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
	Type  MetricType  `json:"type"`
}

FlatValue is a struct that represents a flat metric value.

func NewMetric

func NewMetric(key string, value interface{}, typeStr MetricType) (FlatValue, error)

NewMetric creates a new Metric object based on the provided key, value, and type.

type FormattedMetrics

type FormattedMetrics struct {
	Metrics   map[string]MetricData `json:"metrics"`
	Timestamp string                `json:"timestamp"`
}

FormattedMetrics matches the expected payload of DBtune backend

func FormatMetrics

func FormatMetrics(metrics []FlatValue) FormattedMetrics

TODO: write util tests for this FormatMetrics converts the MetricsState object into a FormattedMetrics object to be used as a metrics payload

type FormattedSystemInfo

type FormattedSystemInfo struct {
	SystemInfo map[string]MetricData `json:"system_info"`
	Timestamp  string                `json:"timestamp"`
}

func FormatSystemInfo

func FormatSystemInfo(metrics []FlatValue) FormattedSystemInfo

type LeveledLogrus

type LeveledLogrus struct {
	*logrus.Logger
}

LeveledLogrus is a wrapper around logrus that provides leveled logging This is needed to be compatible with retryablehttp: https://github.com/hashicorp/go-retryablehttp/pull/101#issuecomment-735206810

func (*LeveledLogrus) Debug

func (l *LeveledLogrus) Debug(msg string, keysAndValues ...interface{})

func (*LeveledLogrus) Error

func (l *LeveledLogrus) Error(msg string, keysAndValues ...interface{})

func (*LeveledLogrus) Info

func (l *LeveledLogrus) Info(msg string, keysAndValues ...interface{})

func (*LeveledLogrus) Warn

func (l *LeveledLogrus) Warn(msg string, keysAndValues ...interface{})

type MetricData

type MetricData struct {
	Type  string      `json:"type"`
	Value interface{} `json:"value"`
}

type MetricType

type MetricType string
const (
	Int        MetricType = "int"
	Float      MetricType = "float"
	String     MetricType = "string"
	Bytes      MetricType = "bytes"
	Boolean    MetricType = "boolean"
	Time       MetricType = "time"
	Percentage MetricType = "percentage"
	PgssDelta  MetricType = "pgss_delta"
)

type ServerURLs

type ServerURLs struct {
	ServerUrl string `mapstructure:"server_url" validate:"required"`
	ApiKey    string `mapstructure:"api_key" validate:"required"`
	DbID      string `mapstructure:"database_id" validate:"required"`
}

func CreateServerURLs

func CreateServerURLs() (ServerURLs, error)

func (ServerURLs) GetKnobRecommendations

func (s ServerURLs) GetKnobRecommendations() string

GetKnobRecommendations generates the URL for getting knob recommendations.

func (ServerURLs) PostActiveConfig

func (s ServerURLs) PostActiveConfig() string

PostActiveConfig generates the URL for posting active configurations.

func (ServerURLs) PostGuardrailSignal

func (s ServerURLs) PostGuardrailSignal() string

PostGuardrailSignal generates the URL for posting a guardrail signal.

func (ServerURLs) PostHeartbeat

func (s ServerURLs) PostHeartbeat() string

PostHeartbeat generates the URL for posting a heartbeat.

func (ServerURLs) PostMetrics

func (s ServerURLs) PostMetrics() string

PostMetrics generates the URL for posting metrics.

func (ServerURLs) PostSystemInfo

func (s ServerURLs) PostSystemInfo() string

PostSystemInfo generates the URL for posting system info.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL