logtail: add Logger.SetEnabled to toggle uploads at runtime

Callers that need to turn logtail uploads on and off in response to
user preference or policy changes previously had no choice: the
package-level Disable is a one-way kill switch intended for the
controlplane DisableLogTail debug message, and requires a process
restart to undo.

Add a per-Logger disabled flag, toggled via SetEnabled, that drops
incoming entries without buffering while disabled. The process-wide
Disable still takes precedence, so a controlplane-issued kill switch
cannot be overridden by a client setting it back on.

To simplify https://github.com/tailscale/tailscale-android/pull/695

Updates #13174

Change-Id: I06e75bd719c851f5f837ca5b2d1e17f7c68355f0
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2026-04-17 18:12:08 +00:00
committed by Brad Fitzpatrick
parent 8dda62cc24
commit 1fbb834dc3
3 changed files with 48 additions and 1 deletions
+15 -1
View File
@@ -172,6 +172,11 @@ type Logger struct {
procID uint32
includeProcSequence bool
// disabled, when true, causes this logger to drop incoming log entries
// without buffering or uploading. It is independent of the process-wide
// Disable kill switch, which takes precedence. Toggled by SetEnabled.
disabled atomic.Bool
writeLock sync.Mutex // guards procSequence, flushTimer, buffer.Write calls
procSequence uint64
flushTimer tstime.TimerController // used when flushDelay is >0
@@ -594,6 +599,15 @@ func Disable() {
logtailDisabled.Store(true)
}
// SetEnabled enables or disables log uploading by lg. When disabled, log
// entries passed to lg are dropped rather than buffered or uploaded; already
// buffered entries may still drain. The process-wide [Disable] kill switch
// takes precedence: if Disable has been called, SetEnabled(true) does not
// re-enable uploads.
func (lg *Logger) SetEnabled(enabled bool) {
lg.disabled.Store(!enabled)
}
var debugWakesAndUploads = envknob.RegisterBool("TS_DEBUG_LOGTAIL_WAKES")
// tryDrainWake tries to send to lg.drainWake, to cause an uploading wakeup.
@@ -613,7 +627,7 @@ func (lg *Logger) tryDrainWake() {
func (lg *Logger) sendLocked(jsonBlob []byte) (int, error) {
tapSend(jsonBlob)
if logtailDisabled.Load() {
if logtailDisabled.Load() || lg.disabled.Load() {
return len(jsonBlob), nil
}