Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ jobs:
- name: Vet
run: go vet ./...

# Deliberately no CGO_ENABLED=0 here: the race detector needs cgo, and
# this step is about the code being right rather than about the artifact.
# The verdict is a rule the whole fleet's placement rests on, and it is
# pure, so there is no excuse for not exercising it on every push.
- name: Test
run: go test -race ./...

# Static linux binaries: workers run these directly, no libc assumptions.
# Built here only to prove they still build -- the binaries that get
# deployed are the ones the release workflow builds from a tag, so there
# is nothing to keep from this run.
- name: Build
env:
CGO_ENABLED: "0"
Expand All @@ -35,15 +45,3 @@ jobs:
run: |
go build -trimpath -o dist/telemetry .
go build -trimpath -o dist/telemetry-debug ./debug

- name: Upload telemetry binary
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: telemetry
path: dist/telemetry

- name: Upload telemetry-debug binary
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: telemetry-debug
path: dist/telemetry-debug
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ jobs:
- name: Vet
run: go vet ./...

# Before anything is built, tagged, checksummed and published. A release
# is the one run where a failing test must stop the job rather than
# annotate it. No CGO_ENABLED=0: the race detector needs cgo.
- name: Test
run: go test -race ./...

# Static linux binaries: workers run these directly, no libc assumptions.
- name: Build
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# compiled binaries, including the debug build's own output
/telemetry
/debug/debug
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,40 @@ To hit the endpoint do http://<ip_OR_URL>:2136/health

By default it runs on port 2136 so make sure cmgr doesn't assign this port for a challenge! This setting is configurable

It returns overloaded when cpu or ram >90%, it doesn't go back to fine until both drop below 80%
Until it has a verdict worth giving it answers 503 rather than guessing, so a
fresh agent is never mistaken for a healthy machine, and neither is a box the
agent cannot yet judge. cork counts that as a missed poll. An idle box settles
on its first sample; a box crossing the CPU high mark without holding it waits,
because the agent will not call a box healthy while it cannot tell a sustained
trip from a spike — so a worker hovering near the mark shows this now and
again, for the life of the process, not only at startup. Memory never waits —
one sample settles it.

It returns overloaded when either cpu or ram crosses 90%, and does not go back
to fine until that metric drops below 80%. The gap between the two marks is
what stops a server sitting on the threshold from flapping on and off the
scheduler.

The two metrics are judged differently, because they behave differently:

- **Memory trips at once**, in both directions. It is a level — it climbs as
containers start, does not come back down by itself, and a box over the mark
will still be over it a minute later. One sample is enough to act on, and
memory handed back is capacity returned.
- **CPU has to be sustained**, eight consecutive samples (four seconds) past a
mark before the verdict moves either way, and deliberately reluctant. On a
challenge host a busy processor is very often the workload doing exactly
what it is for, and it says much less about whether the box can take another
container than memory does. Waiting costs little: cork polls every few
seconds anyway, and real saturation holds for minutes. Set
`TELEMETRY_CPU_SUSTAIN` to a different number of samples to tune it.

Both figures are fractions of the **whole machine**, not of one core — the
aggregate `cpu` line of `/proc/stat` already sums every processor. So 0.90 on a
four-core server means roughly three and a half cores busy, and a single pinned
core reads 0.25. That is the right question to ask of a box being offered more
work: one saturated core says nothing about whether there is room for another
container.

The source code within the debug directory is for a custom debugger build that will expose more system health values and log them to stderr. By default telemetry only exposes whether or not the system is considered overloaded and does not log anything.

Expand Down
80 changes: 80 additions & 0 deletions debug/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"encoding/json"
"github.com/CyLabAcademy/cork-telemetry/internal/load"
"net/http"
"net/http/httptest"
"testing"
)

// The debug build is a superset: it must still answer the `overloaded` field
// cork reads, so it can be dropped in place of the production binary while
// someone watches the numbers, and it adds the raw percentages behind it.
func TestDebugHealthHandlerIsASupersetOfTheContract(t *testing.T) {
haveVerdict.Store(true)
state.Store(&snapshot{Overloaded: true, CPU: 12.5, Mem: 34.25})

rec := httptest.NewRecorder()
healthHandler(rec, httptest.NewRequest(http.MethodGet, "/health", nil))

if rec.Code != http.StatusOK {
t.Fatalf("status %d, want 200", rec.Code)
}
if ct := rec.Header().Get("Content-Type"); ct != "application/json" {
t.Fatalf("Content-Type %q, want application/json", ct)
}

var got map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatalf("body is not JSON: %s (%s)", err, rec.Body.String())
}
if got["overloaded"] != true {
t.Fatalf("overloaded = %v, want true: the debug build must still answer the field cork reads", got["overloaded"])
}
if got["cpu"] != 12.5 || got["mem"] != 34.25 {
t.Fatalf("cpu/mem = %v/%v, want 12.5/34.25", got["cpu"], got["mem"])
}
}

// The debug build refuses on the same terms, so that swapping it in does not
// change what cork sees during startup.
func TestDebugHealthIsRefusedUntilSomethingHasBeenSampled(t *testing.T) {
haveVerdict.Store(false)
t.Cleanup(func() { haveVerdict.Store(true) })

rec := httptest.NewRecorder()
healthHandler(rec, httptest.NewRequest(http.MethodGet, "/health", nil))

if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status %d before the first sample, want 503", rec.Code)
}
}

// Same wiring check as the production binary, because the two must not
// disagree about when a verdict is worth serving: a debug build that
// green-lights a box the real one refuses is worse than no debug build.
func TestDebugHandlerServesWhatTheSamplerPublished(t *testing.T) {
ls := load.State{Sustain: 4}
t.Cleanup(func() { haveVerdict.Store(true) })

status := func() int {
rec := httptest.NewRecorder()
healthHandler(rec, httptest.NewRequest(http.MethodGet, "/health", nil))
return rec.Code
}

publish(&ls, 0.99, 0.10)
if got := status(); got != http.StatusServiceUnavailable {
t.Fatalf("status %d one sample into a climb, want 503", got)
}
for i := 0; i < 3; i++ {
publish(&ls, 0.99, 0.10)
}
if got := status(); got != http.StatusOK {
t.Fatalf("status %d after the trip, want 200", got)
}
if snap := state.Load(); snap == nil || !snap.Overloaded {
t.Fatalf("snapshot %+v does not carry the trip", snap)
}
}
Loading
Loading