mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	telemetry: Add CLI option to selectively disable some metrics
Also fix a couple metrics that were named wrong or reported in excess.
This commit is contained in:
		
							parent
							
								
									8bdd13b594
								
							
						
					
					
						commit
						33aeb1cb5c
					
				
							
								
								
									
										2
									
								
								caddy.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								caddy.go
									
									
									
									
									
								
							@ -617,7 +617,7 @@ func ValidateAndExecuteDirectives(cdyfile Input, inst *Instance, justValidate bo
 | 
				
			|||||||
		return fmt.Errorf("error inspecting server blocks: %v", err)
 | 
							return fmt.Errorf("error inspecting server blocks: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	telemetry.Set("http_num_server_blocks", len(sblocks))
 | 
						telemetry.Set("num_server_blocks", len(sblocks))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate)
 | 
						return executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -46,6 +46,7 @@ func init() {
 | 
				
			|||||||
	flag.StringVar(&caddytls.DefaultCAUrl, "ca", "https://acme-v01.api.letsencrypt.org/directory", "URL to certificate authority's ACME server directory")
 | 
						flag.StringVar(&caddytls.DefaultCAUrl, "ca", "https://acme-v01.api.letsencrypt.org/directory", "URL to certificate authority's ACME server directory")
 | 
				
			||||||
	flag.BoolVar(&caddytls.DisableHTTPChallenge, "disable-http-challenge", caddytls.DisableHTTPChallenge, "Disable the ACME HTTP challenge")
 | 
						flag.BoolVar(&caddytls.DisableHTTPChallenge, "disable-http-challenge", caddytls.DisableHTTPChallenge, "Disable the ACME HTTP challenge")
 | 
				
			||||||
	flag.BoolVar(&caddytls.DisableTLSSNIChallenge, "disable-tls-sni-challenge", caddytls.DisableTLSSNIChallenge, "Disable the ACME TLS-SNI challenge")
 | 
						flag.BoolVar(&caddytls.DisableTLSSNIChallenge, "disable-tls-sni-challenge", caddytls.DisableTLSSNIChallenge, "Disable the ACME TLS-SNI challenge")
 | 
				
			||||||
 | 
						flag.StringVar(&disabledMetrics, "disabled-metrics", "", "Comma-separated list of telemetry metrics to disable")
 | 
				
			||||||
	flag.StringVar(&conf, "conf", "", "Caddyfile to load (default \""+caddy.DefaultConfigFile+"\")")
 | 
						flag.StringVar(&conf, "conf", "", "Caddyfile to load (default \""+caddy.DefaultConfigFile+"\")")
 | 
				
			||||||
	flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
 | 
						flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
 | 
				
			||||||
	flag.BoolVar(&plugins, "plugins", false, "List installed plugins")
 | 
						flag.BoolVar(&plugins, "plugins", false, "List installed plugins")
 | 
				
			||||||
@ -91,6 +92,8 @@ func Run() {
 | 
				
			|||||||
	// initialize telemetry client
 | 
						// initialize telemetry client
 | 
				
			||||||
	if enableTelemetry {
 | 
						if enableTelemetry {
 | 
				
			||||||
		initTelemetry()
 | 
							initTelemetry()
 | 
				
			||||||
 | 
						} else if disabledMetrics != "" {
 | 
				
			||||||
 | 
							mustLogFatalf("[ERROR] Cannot disable specific metrics because telemetry is disabled")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check for one-time actions
 | 
						// Check for one-time actions
 | 
				
			||||||
@ -326,7 +329,7 @@ func initTelemetry() {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	telemetry.Init(id)
 | 
						telemetry.Init(id, strings.Split(disabledMetrics, ","))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const appName = "Caddy"
 | 
					const appName = "Caddy"
 | 
				
			||||||
@ -341,6 +344,7 @@ var (
 | 
				
			|||||||
	version         bool
 | 
						version         bool
 | 
				
			||||||
	plugins         bool
 | 
						plugins         bool
 | 
				
			||||||
	validate        bool
 | 
						validate        bool
 | 
				
			||||||
 | 
						disabledMetrics string
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Build information obtained with the help of -ldflags
 | 
					// Build information obtained with the help of -ldflags
 | 
				
			||||||
 | 
				
			|||||||
@ -254,7 +254,6 @@ func setupTLS(c *caddy.Controller) error {
 | 
				
			|||||||
				return c.Errf("Unable to load certificate and key files for '%s': %v", c.Key, err)
 | 
									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)
 | 
								log.Printf("[INFO] Successfully loaded TLS assets from %s and %s", certificateFile, keyFile)
 | 
				
			||||||
			telemetry.Increment("tls_manual_cert_count")
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// load a directory of certificates, if specified
 | 
							// load a directory of certificates, if specified
 | 
				
			||||||
@ -355,7 +354,6 @@ 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)
 | 
									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)
 | 
								log.Printf("[INFO] Successfully loaded TLS assets from %s", path)
 | 
				
			||||||
			telemetry.Increment("tls_manual_cert_count")
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
				
			|||||||
@ -28,7 +28,11 @@ import (
 | 
				
			|||||||
// may safely be used. If this function is not
 | 
					// may safely be used. If this function is not
 | 
				
			||||||
// called, the collector functions may still be
 | 
					// called, the collector functions may still be
 | 
				
			||||||
// invoked, but they will be no-ops.
 | 
					// invoked, but they will be no-ops.
 | 
				
			||||||
func Init(instanceID uuid.UUID) {
 | 
					//
 | 
				
			||||||
 | 
					// Any metrics keys that are passed in the second
 | 
				
			||||||
 | 
					// argument will be permanently disabled for the
 | 
				
			||||||
 | 
					// lifetime of the process.
 | 
				
			||||||
 | 
					func Init(instanceID uuid.UUID, disabledMetricsKeys []string) {
 | 
				
			||||||
	if enabled {
 | 
						if enabled {
 | 
				
			||||||
		panic("already initialized")
 | 
							panic("already initialized")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -37,6 +41,11 @@ func Init(instanceID uuid.UUID) {
 | 
				
			|||||||
		panic("empty UUID")
 | 
							panic("empty UUID")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	instanceUUID = instanceID
 | 
						instanceUUID = instanceID
 | 
				
			||||||
 | 
						disabledMetricsMu.Lock()
 | 
				
			||||||
 | 
						for _, key := range disabledMetricsKeys {
 | 
				
			||||||
 | 
							disabledMetrics[key] = false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						disabledMetricsMu.Unlock()
 | 
				
			||||||
	enabled = true
 | 
						enabled = true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -158,12 +158,15 @@ func emit(final bool) error {
 | 
				
			|||||||
		// update the list of enabled/disabled keys, if any
 | 
							// update the list of enabled/disabled keys, if any
 | 
				
			||||||
		for _, key := range reply.EnableKeys {
 | 
							for _, key := range reply.EnableKeys {
 | 
				
			||||||
			disabledMetricsMu.Lock()
 | 
								disabledMetricsMu.Lock()
 | 
				
			||||||
 | 
								// only re-enable this metric if it is temporarily disabled
 | 
				
			||||||
 | 
								if temp, ok := disabledMetrics[key]; ok && temp {
 | 
				
			||||||
				delete(disabledMetrics, key)
 | 
									delete(disabledMetrics, key)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			disabledMetricsMu.Unlock()
 | 
								disabledMetricsMu.Unlock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		for _, key := range reply.DisableKeys {
 | 
							for _, key := range reply.DisableKeys {
 | 
				
			||||||
			disabledMetricsMu.Lock()
 | 
								disabledMetricsMu.Lock()
 | 
				
			||||||
			disabledMetrics[key] = struct{}{}
 | 
								disabledMetrics[key] = true // all remotely-disabled keys are "temporarily" disabled
 | 
				
			||||||
			disabledMetricsMu.Unlock()
 | 
								disabledMetricsMu.Unlock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -359,10 +362,17 @@ var (
 | 
				
			|||||||
	updateTimer   *time.Timer
 | 
						updateTimer   *time.Timer
 | 
				
			||||||
	updateTimerMu sync.Mutex
 | 
						updateTimerMu sync.Mutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// disabledMetrics is a list of metric keys
 | 
						// disabledMetrics is a set of metric keys
 | 
				
			||||||
	// that should NOT be saved to the buffer
 | 
						// that should NOT be saved to the buffer
 | 
				
			||||||
	// or sent to the telemetry server.
 | 
						// or sent to the telemetry server. The value
 | 
				
			||||||
	disabledMetrics   = make(map[string]struct{})
 | 
						// indicates whether the entry is temporary.
 | 
				
			||||||
 | 
						// If the value is true, it may be removed if
 | 
				
			||||||
 | 
						// the metric is re-enabled remotely later. If
 | 
				
			||||||
 | 
						// the value is false, it is permanent
 | 
				
			||||||
 | 
						// (presumably becaues the user explicitly
 | 
				
			||||||
 | 
						// disabled it) and can only be re-enabled
 | 
				
			||||||
 | 
						// with user consent.
 | 
				
			||||||
 | 
						disabledMetrics   = make(map[string]bool)
 | 
				
			||||||
	disabledMetricsMu sync.RWMutex
 | 
						disabledMetricsMu sync.RWMutex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// instanceUUID is the ID of the current instance.
 | 
						// instanceUUID is the ID of the current instance.
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user