Skip to content

Commit

Permalink
Refactor diagnostics -> telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Mar 23, 2018
1 parent 7c868af commit 5231695
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 97 deletions.
4 changes: 2 additions & 2 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"time"

"github.com/mholt/caddy/caddyfile"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// Configurable application parameters
Expand Down Expand Up @@ -617,7 +617,7 @@ func ValidateAndExecuteDirectives(cdyfile Input, inst *Instance, justValidate bo
return fmt.Errorf("error inspecting server blocks: %v", err)
}

diagnostics.Set("http_num_server_blocks", len(sblocks))
telemetry.Set("http_num_server_blocks", len(sblocks))

return executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate)
}
Expand Down
34 changes: 17 additions & 17 deletions caddy/caddymain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/klauspost/cpuid"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddytls"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
"github.com/xenolf/lego/acme"
"gopkg.in/natefinch/lumberjack.v2"

Expand All @@ -52,7 +52,6 @@ func init() {
flag.StringVar(&caddytls.DefaultEmail, "email", "", "Default ACME CA account email address")
flag.DurationVar(&acme.HTTPClient.Timeout, "catimeout", acme.HTTPClient.Timeout, "Default ACME CA HTTP timeout")
flag.StringVar(&logfile, "log", "", "Process log file")
flag.BoolVar(&noDiag, "no-diagnostics", false, "Disable diagnostic reporting")
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
flag.BoolVar(&caddy.Quiet, "quiet", false, "Quiet mode (no initialization output)")
flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate")
Expand Down Expand Up @@ -89,9 +88,9 @@ func Run() {
})
}

// initialize diagnostics client
if !noDiag {
initDiagnostics()
// initialize telemetry client
if enableTelemetry {
initTelemetry()
}

// Check for one-time actions
Expand Down Expand Up @@ -150,13 +149,13 @@ func Run() {
// Execute instantiation events
caddy.EmitEvent(caddy.InstanceStartupEvent, instance)

// Begin diagnostics (these are no-ops if diagnostics disabled)
diagnostics.Set("caddy_version", appVersion)
diagnostics.Set("num_listeners", len(instance.Servers()))
diagnostics.Set("server_type", serverType)
diagnostics.Set("os", runtime.GOOS)
diagnostics.Set("arch", runtime.GOARCH)
diagnostics.Set("cpu", struct {
// Begin telemetry (these are no-ops if telemetry disabled)
telemetry.Set("caddy_version", appVersion)
telemetry.Set("num_listeners", len(instance.Servers()))
telemetry.Set("server_type", serverType)
telemetry.Set("os", runtime.GOOS)
telemetry.Set("arch", runtime.GOARCH)
telemetry.Set("cpu", struct {
BrandName string `json:"brand_name,omitempty"`
NumLogical int `json:"num_logical,omitempty"`
AESNI bool `json:"aes_ni,omitempty"`
Expand All @@ -165,7 +164,7 @@ func Run() {
NumLogical: runtime.NumCPU(),
AESNI: cpuid.CPU.AesNi(),
})
diagnostics.StartEmitting()
telemetry.StartEmitting()

// Twiddle your thumbs
instance.Wait()
Expand Down Expand Up @@ -290,8 +289,8 @@ func setCPU(cpu string) error {
return nil
}

// initDiagnostics initializes the diagnostics engine.
func initDiagnostics() {
// initTelemetry initializes the telemetry engine.
func initTelemetry() {
uuidFilename := filepath.Join(caddy.AssetsPath(), "uuid")

newUUID := func() uuid.UUID {
Expand Down Expand Up @@ -327,7 +326,7 @@ func initDiagnostics() {
}
}

diagnostics.Init(id)
telemetry.Init(id)
}

const appName = "Caddy"
Expand All @@ -342,7 +341,6 @@ var (
version bool
plugins bool
validate bool
noDiag bool
)

// Build information obtained with the help of -ldflags
Expand All @@ -356,4 +354,6 @@ var (
gitCommit string // git rev-parse HEAD
gitShortStat string // git diff-index --shortstat
gitFilesModified string // git diff-index --name-only HEAD

enableTelemetry = true
)
4 changes: 2 additions & 2 deletions caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"path/filepath"
"strings"

"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// Parse parses the input just enough to group tokens, in
Expand Down Expand Up @@ -371,7 +371,7 @@ func (p *parser) directive() error {

// The directive itself is appended as a relevant token
p.block.Tokens[dir] = append(p.block.Tokens[dir], p.tokens[p.cursor])
diagnostics.AppendUnique("directives", dir)
telemetry.AppendUnique("directives", dir)

for p.Next() {
if p.Val() == "{" {
Expand Down
8 changes: 4 additions & 4 deletions caddyhttp/httpserver/mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"strings"
"sync"

"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// tlsHandler is a http.Handler that will inject a value
Expand Down Expand Up @@ -103,12 +103,12 @@ func (h *tlsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if checked {
r = r.WithContext(context.WithValue(r.Context(), MitmCtxKey, mitm))
if mitm {
go diagnostics.AppendUnique("http_mitm", "likely")
go telemetry.AppendUnique("http_mitm", "likely")
} else {
go diagnostics.AppendUnique("http_mitm", "unlikely")
go telemetry.AppendUnique("http_mitm", "unlikely")
}
} else {
go diagnostics.AppendUnique("http_mitm", "unknown")
go telemetry.AppendUnique("http_mitm", "unknown")
}

if mitm && h.closeOnMITM {
Expand Down
16 changes: 8 additions & 8 deletions caddyhttp/httpserver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/mholt/caddy/caddyfile"
"github.com/mholt/caddy/caddyhttp/staticfiles"
"github.com/mholt/caddy/caddytls"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

const serverType = "http"
Expand Down Expand Up @@ -220,9 +220,9 @@ func (h *httpContext) MakeServers() ([]caddy.Server, error) {

var atLeastOneSiteLooksLikeProduction bool
for _, cfg := range h.siteConfigs {
// if we aren't sure yet whether it's a "production" server,
// continue to see if all the addresses (both sites and
// listeners) are loopback
// see if all the addresses (both sites and
// listeners) are loopback to help us determine
// if this is a "production" instance or not
if !atLeastOneSiteLooksLikeProduction {
if !caddy.IsLoopback(cfg.Addr.Host) &&
!caddy.IsLoopback(cfg.ListenHost) &&
Expand Down Expand Up @@ -272,17 +272,17 @@ func (h *httpContext) MakeServers() ([]caddy.Server, error) {
servers = append(servers, s)
}

// NOTE: This value is only a "good" guess. Quite often, development
// NOTE: This value is only a "good guess". Quite often, development
// environments will use internal DNS or a local hosts file to serve
// real-looking domains in local development. We can't easily tell
// which without doing a DNS lookup, so this guess is definitely naive,
// and if we ever want a better guess, we will have to do DNS lookups.
deploymentGuess := "dev"
if looksLikeProductionCA && atLeastOneSiteLooksLikeProduction {
deploymentGuess = "production"
deploymentGuess = "prod"
}
diagnostics.Set("http_deployment_guess", deploymentGuess)
diagnostics.Set("http_num_sites", len(h.siteConfigs))
telemetry.Set("http_deployment_guess", deploymentGuess)
telemetry.Set("http_num_sites", len(h.siteConfigs))

return servers, nil
}
Expand Down
6 changes: 3 additions & 3 deletions caddyhttp/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/staticfiles"
"github.com/mholt/caddy/caddytls"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// Server is the HTTP server implementation.
Expand Down Expand Up @@ -347,8 +347,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}()

// TODO: Somehow report UA string in conjunction with TLS handshake, if any (and just once per connection)
go diagnostics.AppendUnique("http_user_agent", r.Header.Get("User-Agent"))
go diagnostics.Increment("http_request_count")
go telemetry.AppendUnique("http_user_agent", r.Header.Get("User-Agent"))
go telemetry.Increment("http_request_count")

// copy the original, unchanged URL into the context
// so it can be referenced by middlewares
Expand Down
8 changes: 4 additions & 4 deletions caddytls/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sync"
"time"

"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
"golang.org/x/crypto/ocsp"
)

Expand Down Expand Up @@ -166,7 +166,7 @@ func (cfg *Config) CacheManagedCertificate(domain string) (Certificate, error) {
if err != nil {
return cert, err
}
diagnostics.Increment("tls_managed_cert_count")
telemetry.Increment("tls_managed_cert_count")
return cfg.cacheCertificate(cert), nil
}

Expand All @@ -181,7 +181,7 @@ func (cfg *Config) cacheUnmanagedCertificatePEMFile(certFile, keyFile string) er
return err
}
cfg.cacheCertificate(cert)
diagnostics.Increment("tls_manual_cert_count")
telemetry.Increment("tls_manual_cert_count")
return nil
}

Expand All @@ -195,7 +195,7 @@ func (cfg *Config) cacheUnmanagedCertificatePEMBytes(certBytes, keyBytes []byte)
return err
}
cfg.cacheCertificate(cert)
diagnostics.Increment("tls_manual_cert_count")
telemetry.Increment("tls_manual_cert_count")
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions caddytls/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"time"

"github.com/mholt/caddy"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
"github.com/xenolf/lego/acme"
)

Expand Down Expand Up @@ -268,7 +268,7 @@ Attempts:
break
}

go diagnostics.Increment("tls_acme_certs_obtained")
go telemetry.Increment("tls_acme_certs_obtained")

return nil
}
Expand Down Expand Up @@ -340,7 +340,7 @@ func (c *ACMEClient) Renew(name string) error {
}

caddy.EmitEvent(caddy.CertRenewEvent, name)
go diagnostics.Increment("tls_acme_certs_renewed")
go telemetry.Increment("tls_acme_certs_renewed")

return saveCertResource(c.storage, newCertMeta)
}
Expand All @@ -367,7 +367,7 @@ func (c *ACMEClient) Revoke(name string) error {
return err
}

go diagnostics.Increment("tls_acme_certs_revoked")
go telemetry.Increment("tls_acme_certs_revoked")

err = c.storage.DeleteSite(name)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions caddytls/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sync/atomic"
"time"

"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// configGroup is a type that keys configs by their hostname
Expand Down Expand Up @@ -102,7 +102,7 @@ func (cg configGroup) GetConfigForClient(clientHello *tls.ClientHelloInfo) (*tls
func (cfg *Config) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
// TODO: We need to collect this in a heavily de-duplicating way
// It would also be nice to associate a handshake with the UA string (but that is only for HTTP server type)
// go diagnostics.Append("tls_client_hello", struct {
// go telemetry.Append("tls_client_hello", struct {
// NoSNI bool `json:"no_sni,omitempty"`
// CipherSuites []uint16 `json:"cipher_suites,omitempty"`
// SupportedCurves []tls.CurveID `json:"curves,omitempty"`
Expand All @@ -121,9 +121,9 @@ func (cfg *Config) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certif
// })
cert, err := cfg.getCertDuringHandshake(strings.ToLower(clientHello.ServerName), true, true)
if err == nil {
go diagnostics.Increment("tls_handshake_count")
go telemetry.Increment("tls_handshake_count")
} else {
go diagnostics.Append("tls_handshake_error", err.Error())
go telemetry.Append("tls_handshake_error", err.Error())
}
return &cert.Certificate, err
}
Expand Down
12 changes: 6 additions & 6 deletions caddytls/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"strings"

"github.com/mholt/caddy"
"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

func init() {
Expand Down Expand Up @@ -175,11 +175,11 @@ func setupTLS(c *caddy.Controller) error {
case "max_certs":
c.Args(&maxCerts)
config.OnDemand = true
diagnostics.Increment("tls_on_demand_count")
telemetry.Increment("tls_on_demand_count")
case "ask":
c.Args(&askURL)
config.OnDemand = true
diagnostics.Increment("tls_on_demand_count")
telemetry.Increment("tls_on_demand_count")
case "dns":
args := c.RemainingArgs()
if len(args) != 1 {
Expand Down Expand Up @@ -254,7 +254,7 @@ func setupTLS(c *caddy.Controller) error {
return c.Errf("Unable to load certificate and key files for '%s': %v", c.Key, err)
}
log.Printf("[INFO] Successfully loaded TLS assets from %s and %s", certificateFile, keyFile)
diagnostics.Increment("tls_manual_cert_count")
telemetry.Increment("tls_manual_cert_count")
}

// load a directory of certificates, if specified
Expand All @@ -274,7 +274,7 @@ func setupTLS(c *caddy.Controller) error {
if err != nil {
return fmt.Errorf("self-signed: %v", err)
}
diagnostics.Increment("tls_self_signed_count")
telemetry.Increment("tls_self_signed_count")
}

return nil
Expand Down Expand Up @@ -355,7 +355,7 @@ func loadCertsInDir(cfg *Config, c *caddy.Controller, dir string) error {
return c.Errf("%s: failed to load cert and key for '%s': %v", path, c.Key, err)
}
log.Printf("[INFO] Successfully loaded TLS assets from %s", path)
diagnostics.Increment("tls_manual_cert_count")
telemetry.Increment("tls_manual_cert_count")
}
return nil
})
Expand Down
6 changes: 3 additions & 3 deletions sigtrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"os/signal"
"sync"

"github.com/mholt/caddy/diagnostics"
"github.com/mholt/caddy/telemetry"
)

// TrapSignals create signal handlers for all applicable signals for this
Expand Down Expand Up @@ -54,8 +54,8 @@ func trapSignalsCrossPlatform() {

log.Println("[INFO] SIGINT: Shutting down")

diagnostics.AppendUnique("sigtrap", "SIGINT")
go diagnostics.StopEmitting() // not guaranteed to finish in time; that's OK (just don't block!)
telemetry.AppendUnique("sigtrap", "SIGINT")
go telemetry.StopEmitting() // not guaranteed to finish in time; that's OK (just don't block!)

// important cleanup actions before shutdown callbacks
for _, f := range OnProcessExit {
Expand Down
Loading

0 comments on commit 5231695

Please sign in to comment.