Skip to content

Commit

Permalink
i/querylog: use median instead of average
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw committed Dec 15, 2024
1 parent 16a36cb commit af65053
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ ENV CGO_ENABLED=1

WORKDIR /app

RUN apk add --no-cache gcc musl-dev
RUN apk add --no-cache gcc musl-dev wget unzip

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN wget https://github.com/nalgeon/sqlean/releases/download/0.27.1/sqlean-linux-x86.zip -O /tmp/sqlean.zip \
&& unzip /tmp/sqlean.zip -d ./sqlean

RUN go build \
-ldflags="-w -s -X main.Version=${VERSION}" \
-o beacon ./cmd/beacon
Expand All @@ -40,6 +43,7 @@ FROM alpine:latest
RUN apk add --no-cache libc6-compat tzdata

COPY --from=builder /app/beacon /beacon
COPY --from=builder /app/sqlean/stats.so /extensions/stats.so

EXPOSE 80
EXPOSE 53
Expand Down
11 changes: 8 additions & 3 deletions cmd/beacon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ func main() {

// Query log
slog.Info("Setting up query logger...")
dataDir, err := mustGetEnv("DATA_DIR")
querylog.DataDir, err = mustGetEnv("DATA_DIR")
if err != nil {
slog.Error(err.Error())
return
}

querylog.ExtensionsDir, err = mustGetEnv("EXTENSIONS_DIR")
if err != nil {
slog.Error(err.Error())
return
}

querylog.DataDir = dataDir
err = querylog.NewDB()
if err != nil {
slog.Error(err.Error())
Expand Down Expand Up @@ -100,7 +105,7 @@ func main() {

// Lists
slog.Info("Setting up lists sync job...")
lists.Dir = fmt.Sprintf("%s/%s", dataDir, "lists")
lists.Dir = fmt.Sprintf("%s/%s", querylog.DataDir, "lists")
os.MkdirAll(lists.Dir, 0755)

sourcesUpdateDays := int(config.All.Sources.UpdateInterval.Seconds()) / 86400
Expand Down
1 change: 1 addition & 0 deletions deploy/ansible/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
content: |
BEACON_CONFIG_FILE=/var/lib/beacon/config.yml
BEACON_DATA_DIR=/var/lib/beacon
BEACON_EXTENSIONS_DIR=/var/lib/beacon/extensions
dest: /var/lib/beacon/.env
mode: "0600"

Expand Down
1 change: 1 addition & 0 deletions deploy/docker-compose/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
environment:
- BEACON_CONFIG_FILE=/data/config.yml
- BEACON_DATA_DIR=/data
- BEACON_EXTENSIONS_DIR=/extensions
restart: unless-stopped
privileged: true
network_mode: host
19 changes: 13 additions & 6 deletions internal/querylog/querylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"sync"
"time"

_ "github.com/mattn/go-sqlite3"
"github.com/mattn/go-sqlite3"
"github.com/st3v3nmw/beacon/internal/config"
"github.com/st3v3nmw/beacon/internal/types"
)

var (
DataDir string
DB *sql.DB
QL *QueryLogger
DataDir string
ExtensionsDir string
DB *sql.DB
QL *QueryLogger
)

const schema = `
Expand All @@ -30,7 +31,7 @@ CREATE TABLE IF NOT EXISTS queries (
block_reason VARCHAR(50) NULL,
upstream VARCHAR(50) NULL,
response_code VARCHAR(255) NOT NULL,
response_time_ms INTEGER NOT NULL,
response_time INTEGER NOT NULL,
prefetched BOOLEAN NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
Expand All @@ -43,7 +44,13 @@ CREATE TABLE IF NOT EXISTS query_patterns (
`

func NewDB() (err error) {
DB, err = sql.Open("sqlite3", DataDir+"/querylog.db")
sql.Register("sqlite_ext",
&sqlite3.SQLiteDriver{
Extensions: []string{ExtensionsDir + "/stats"},
},
)

DB, err = sql.Open("sqlite_ext", DataDir+"/querylog.db")
if err != nil {
return err
}
Expand Down
56 changes: 28 additions & 28 deletions internal/querylog/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ SELECT
END as prefetched_ratio,
-- Performance
ROUND(AVG(response_time_ms), 2) as avg_response_time_ms,
ROUND(COALESCE(AVG(CASE WHEN upstream THEN response_time_ms END), 0), 2) as avg_forwarded_response_time_ms,
MIN(response_time_ms) as min_response_time_ms,
MAX(response_time_ms) as max_response_time_ms,
MEDIAN(response_time) as typical_response_time,
COALESCE(MEDIAN(CASE WHEN upstream THEN response_time END), 0) as typical_forwarded_response_time,
MIN(response_time) as min_response_time,
MAX(response_time) as max_response_time,
-- Query types distribution
(
Expand Down Expand Up @@ -133,26 +133,26 @@ ORDER BY total_queries DESC;
`

type DeviceStats struct {
Client string `json:"client"`
TotalQueries int `json:"total_queries"`
UniqueDomains int `json:"unique_domains"`
CachedQueries int `json:"cached_queries"`
CacheHitRatio float64 `json:"cache_hit_ratio"`
BlockedQueries int `json:"blocked_queries"`
BlockRatio float64 `json:"block_ratio"`
PrefetchedQueries int `json:"prefetched_queries"`
PrefetchedRatio float64 `json:"prefetched_ratio"`
AvgResponseTimeMs float64 `json:"avg_response_time_ms"`
AvgForwardedResponseTimeMs float64 `json:"avg_forwarded_response_time_ms"`
MinResponseTimeMs int `json:"min_response_time_ms"`
MaxResponseTimeMs int `json:"max_response_time_ms"`
QueryTypes map[string]int `json:"query_types"`
BlockReasons map[string]int `json:"block_reasons"`
Upstreams map[string]int `json:"upstreams"`
ResolvedDomains map[string]int `json:"resolved_domains"`
BlockedDomains map[string]int `json:"blocked_domains"`
ResponseCodes map[string]int `json:"response_codes"`
IPs map[string]int `json:"ips"`
Client string `json:"client"`
TotalQueries int `json:"total_queries"`
UniqueDomains int `json:"unique_domains"`
CachedQueries int `json:"cached_queries"`
CacheHitRatio float64 `json:"cache_hit_ratio"`
BlockedQueries int `json:"blocked_queries"`
BlockRatio float64 `json:"block_ratio"`
PrefetchedQueries int `json:"prefetched_queries"`
PrefetchedRatio float64 `json:"prefetched_ratio"`
TypicalResponseTime float64 `json:"typical_response_time"`
TypicalForwardedResponseTime float64 `json:"typical_forwarded_response_time"`
MinResponseTime int `json:"min_response_time"`
MaxResponseTime int `json:"max_response_time"`
QueryTypes map[string]int `json:"query_types"`
BlockReasons map[string]int `json:"block_reasons"`
Upstreams map[string]int `json:"upstreams"`
ResolvedDomains map[string]int `json:"resolved_domains"`
BlockedDomains map[string]int `json:"blocked_domains"`
ResponseCodes map[string]int `json:"response_codes"`
IPs map[string]int `json:"ips"`
}

func GetDeviceStats(last time.Duration) ([]DeviceStats, error) {
Expand All @@ -178,10 +178,10 @@ func GetDeviceStats(last time.Duration) ([]DeviceStats, error) {
&s.BlockRatio,
&s.PrefetchedQueries,
&s.PrefetchedRatio,
&s.AvgResponseTimeMs,
&s.AvgForwardedResponseTimeMs,
&s.MinResponseTimeMs,
&s.MaxResponseTimeMs,
&s.TypicalResponseTime,
&s.TypicalForwardedResponseTime,
&s.MinResponseTime,
&s.MaxResponseTime,
&query_types,
&block_reasons,
&upstreams,
Expand Down

0 comments on commit af65053

Please sign in to comment.