Documentation
¶
Overview ¶
Package logtail sends logs to log.tailscale.com.
Index ¶
- Constants
- func Disable()
- func RegisterLogTap(dst chan<- string) (unregister func())
- func UploadLogs[T any](ctx context.Context, conf Config, entries iter.Seq[LogEntry[T]]) (int, error)
- type Buffer
- type Config
- type LogEntry
- type Logger
- func (lg *Logger) Close()deprecated
- func (lg *Logger) ExpVar() expvar.Var
- func (lg *Logger) Flush() error
- func (lg *Logger) Logf(format string, args ...any)
- func (lg *Logger) PrivateID() logid.PrivateID
- func (lg *Logger) SetEnabled(enabled bool)
- func (lg *Logger) SetNetMon(lm *netmon.Monitor)
- func (lg *Logger) SetSockstatsLabel(label sockstats.Label)
- func (lg *Logger) SetVerbosityLevel(level int)
- func (lg *Logger) Shutdown(ctx context.Context) error
- func (lg *Logger) StartFlush()
- func (lg *Logger) Write(buf []byte) (int, error)
- type Logtail
Constants ¶
const ( // CollectionNode is the name of a logtail Config.Collection // for tailscaled (or equivalent: IPNExtension, Android app). CollectionNode = "tailnode.log.tailscale.io" )
const DefaultHost = "log.tailscale.com"
DefaultHost is the default host name to upload logs to when Config.BaseURL isn't provided.
Variables ¶
This section is empty.
Functions ¶
func Disable ¶ added in v1.24.0
func Disable()
Disable disables logtail uploads for the lifetime of the process.
func RegisterLogTap ¶ added in v1.36.0
func RegisterLogTap(dst chan<- string) (unregister func())
RegisterLogTap registers dst to get a copy of every log write. The caller must call unregister when done watching.
This would ideally be a method on Logger, but Logger isn't really available in most places; many writes go via stderr which filch redirects to the singleton Logger set up early. For better or worse, there's basically only one Logger within the program. This mechanism at least works well for tailscaled. It works less well for a binary with multiple tsnet.Servers. Oh well. This then subscribes to all of them.
func UploadLogs ¶ added in v1.102.0
func UploadLogs[T any](ctx context.Context, conf Config, entries iter.Seq[LogEntry[T]]) (int, error)
UploadLogs uploads entries to the log server described by conf and returns once they have all been sent (or an upload fails). It returns the number of entries that were successfully uploaded.
It is a stateless alternative to NewLogger for callers that just want to push a batch of log entries without the background uploader, ring buffer, stderr echoing, or network-up gating that a Logger provides. Each entry is marshaled to JSON, its Logtail metadata is filled in from conf where the caller left it zero (honoring conf.SkipClientTime, conf.IncludeProcID, and conf.IncludeProcSequence), and entries are batched up to the server's maximum upload size and POSTed synchronously (compressed when conf.CompressLogs is set).
Unlike Logger, UploadLogs does not retry: if a batch fails to upload it returns the count of entries sent in prior batches and the error immediately, and any remaining entries are not sent. The conf.Stderr and conf.Bus fields are ignored.
Types ¶
type Buffer ¶
type Buffer interface {
// TryReadLine tries to read a log line from the ring buffer.
// If no line is available it returns a nil slice.
// If the ring buffer is closed it returns io.EOF.
//
// The returned slice may point to data that will be overwritten
// by a subsequent call to TryReadLine.
TryReadLine() ([]byte, error)
// Write writes a log line into the ring buffer.
// Implementations must not retain the provided buffer.
Write([]byte) (int, error)
}
func NewMemoryBuffer ¶
type Config ¶
type Config struct {
Collection string // collection name, a domain name
PrivateID logid.PrivateID // private ID for the primary log stream
CopyPrivateID logid.PrivateID // private ID for a log stream that is a superset of this log stream
BaseURL string // if empty defaults to "https://log.tailscale.com"
HTTPC *http.Client // if empty defaults to http.DefaultClient
SkipClientTime bool // if true, client_time is not written to logs
LowMemory bool // if true, logtail minimizes memory use
Clock tstime.Clock // if set, Clock.Now substitutes uses of time.Now
Stderr io.Writer // if set, logs are sent here instead of os.Stderr
Bus *eventbus.Bus // if set, uses the eventbus for awaitInternetUp instead of callback
StderrLevel int // max verbosity level to write to stderr; 0 means the non-verbose messages only
Buffer Buffer // temp storage, if nil a MemoryBuffer
CompressLogs bool // whether to compress the log uploads
MaxUploadSize int // maximum upload size; 0 means using the default
// MetricsDelta, if non-nil, is a func that returns an encoding
// delta in clientmetrics to upload alongside existing logs.
// It can return either an empty string (for nothing) or a string
// that's safe to embed in a JSON string literal without further escaping.
MetricsDelta func() string
// FlushDelayFn, if non-nil is a func that returns how long to wait to
// accumulate logs before uploading them. 0 or negative means to upload
// immediately.
//
// If nil, a default value is used. (currently 2 seconds)
FlushDelayFn func() time.Duration
// IncludeProcID, if true, results in an ephemeral process identifier being
// included in logs. The ID is random and not guaranteed to be globally
// unique, but it can be used to distinguish between different instances
// running with same PrivateID.
IncludeProcID bool
// IncludeProcSequence, if true, results in an ephemeral sequence number
// being included in the logs. The sequence number is incremented for each
// log message sent, but is not persisted across process restarts.
IncludeProcSequence bool
// Disabled, if true, causes the returned [Logger] to start in the
// disabled state, dropping entries without buffering or uploading
// (equivalent to calling [Logger.SetEnabled] with false immediately).
// It applies before the internal startup banner is written, so no
// log entries are emitted until [Logger.SetEnabled] is called with
// true. The process-wide [Disable] kill switch still takes precedence.
Disabled bool
}
type LogEntry ¶ added in v1.102.0
type LogEntry[T any] struct { Logtail Logtail `json:"logtail,omitzero"` // The `inline` tag option was renamed to `embed` in Go 1.27's // encoding/json/v2, but the pinned go-json-experiment module // (used on older Go versions) only knows `inline`. Each // implementation ignores the option it doesn't know, so specify // both until we require Go 1.27 and drop `inline`. // //lint:ignore SA5008 staticcheck doesn't know Go 1.27's `embed` option yet Value T `json:",inline,embed"` }
LogEntry is a single log entry to be uploaded via UploadLogs.
It marshals to a JSON object whose reserved "logtail" member carries the metadata in Logtail and whose remaining members are taken from Value, which is inlined at the top level alongside "logtail".
Value must marshal to a JSON object: T must be a Go struct (or pointer to one), a Go map with a string key, or a jsontext.Value holding an object (for example jsontext.Value(`{"text":"hello"}`)). This is enforced when the entry is uploaded, not at compile time. Use T = jsontext.Value to mix differently-shaped payloads in a single upload.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger writes logs, splitting them as configured between local logging facilities and uploading to a log server.
func NewLogger ¶ added in v1.4.0
NewLogger returns a new Logger that splits logs as configured between local logging facilities and uploading to a log server in the background.
func (*Logger) ExpVar ¶ added in v1.94.0
ExpVar report metrics about the logger.
counter_upload_calls: Total number of upload attempts.
counter_upload_errors: Total number of upload attempts that failed.
counter_uploaded_bytes: Total number of bytes successfully uploaded (which is calculated after compression is applied).
counter_uploading_nsecs: Total number of nanoseconds spent uploading.
buffer: An optional metrics.Set with metrics for the Buffer.
func (*Logger) Flush ¶
Flush uploads all logs to the server. It blocks until complete or there is an unrecoverable error.
TODO(bradfitz): this apparently just returns nil, as of tailscale/corp@9c2ec35. Finish cleaning this up.
func (*Logger) Logf ¶ added in v1.20.0
Logf logs to l using the provided fmt-style format and optional arguments.
func (*Logger) PrivateID ¶ added in v1.24.0
PrivateID returns the logger's private log ID.
It exists for internal use only.
func (*Logger) SetEnabled ¶ added in v1.98.0
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 (*Logger) SetNetMon ¶ added in v1.40.0
SetNetMon sets the network monitor.
It should not be changed concurrently with log writes and should only be set once.
func (*Logger) SetSockstatsLabel ¶ added in v1.40.0
SetSockstatsLabel sets the label used in sockstat logs to identify network traffic from this logger.
func (*Logger) SetVerbosityLevel ¶ added in v1.4.0
SetVerbosityLevel controls the verbosity level that should be written to stderr. 0 is the default (not verbose). Levels 1 or higher are increasingly verbose.
func (*Logger) Shutdown ¶
Shutdown gracefully shuts down the logger while completing any remaining uploads.
It will block, continuing to try and upload unless the passed context object interrupts it by being done. If the shutdown is interrupted, an error is returned.
func (*Logger) StartFlush ¶ added in v1.36.0
func (lg *Logger) StartFlush()
StartFlush starts a log upload, if anything is pending.
If l is nil, StartFlush is a no-op.
type Logtail ¶ added in v1.102.0
type Logtail struct {
// ClientTime is the time the entry was generated. If zero, [UploadLogs]
// fills it with the current time unless Config.SkipClientTime is set.
ClientTime time.Time `json:"client_time,omitzero"`
// ProcID is an ephemeral process identifier; see Config.IncludeProcID.
ProcID uint32 `json:"proc_id,omitzero"`
// ProcSeq is an ephemeral per-process sequence number; see
// Config.IncludeProcSequence.
ProcSeq uint64 `json:"proc_seq,omitzero"`
}
Logtail is the reserved "logtail" metadata member of a LogEntry.
Zero-valued fields are omitted when an entry is uploaded. UploadLogs fills in any fields the caller leaves zero from its Config (e.g. ClientTime and ProcID), so most callers can leave this empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
logadopt
command
Command logadopt is a CLI tool to adopt a machine into a logtail collection.
|
Command logadopt is a CLI tool to adopt a machine into a logtail collection. |
|
logreprocess
command
The logreprocess program tails a log and reprocesses it.
|
The logreprocess program tails a log and reprocesses it. |
|
logtail
command
The logtail program logs stdin.
|
The logtail program logs stdin. |
|
Package filch is a file system queue that pilfers your stderr.
|
Package filch is a file system queue that pilfers your stderr. |