mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 10:37:24 -04:00 
			
		
		
		
	telemetry: Add memory and goroutine metrics, rename container
And fix a typo in a comment, sigh
This commit is contained in:
		
							parent
							
								
									86fd2f22fb
								
							
						
					
					
						commit
						df7cdc3fae
					
				| @ -172,7 +172,7 @@ func Run() { | |||||||
| 		AESNI:      cpuid.CPU.AesNi(), | 		AESNI:      cpuid.CPU.AesNi(), | ||||||
| 	}) | 	}) | ||||||
| 	if containerized := detectContainer(); containerized { | 	if containerized := detectContainer(); containerized { | ||||||
| 		telemetry.Set("in_container", containerized) | 		telemetry.Set("container", containerized) | ||||||
| 	} | 	} | ||||||
| 	telemetry.StartEmitting() | 	telemetry.StartEmitting() | ||||||
| 
 | 
 | ||||||
| @ -299,7 +299,7 @@ func setCPU(cpu string) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // detectContainer attemps to determine whether the process is | // detectContainer attempts to determine whether the process is | ||||||
| // being run inside a container. References: | // being run inside a container. References: | ||||||
| // https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/ | // https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/ | ||||||
| // https://stackoverflow.com/a/20012536/1048862 | // https://stackoverflow.com/a/20012536/1048862 | ||||||
| @ -397,7 +397,7 @@ func initTelemetry() error { | |||||||
| 	// initialize telemetry | 	// initialize telemetry | ||||||
| 	telemetry.Init(id, disabledMetricsSlice) | 	telemetry.Init(id, disabledMetricsSlice) | ||||||
| 
 | 
 | ||||||
| 	// if any metrics were disabled, report it | 	// if any metrics were disabled, report which ones (so we know how representative the data is) | ||||||
| 	if len(disabledMetricsSlice) > 0 { | 	if len(disabledMetricsSlice) > 0 { | ||||||
| 		telemetry.Set("disabled_metrics", disabledMetricsSlice) | 		telemetry.Set("disabled_metrics", disabledMetricsSlice) | ||||||
| 		log.Printf("[NOTICE] The following telemetry metrics are disabled: %s", disabledMetrics) | 		log.Printf("[NOTICE] The following telemetry metrics are disabled: %s", disabledMetrics) | ||||||
|  | |||||||
| @ -40,6 +40,7 @@ import ( | |||||||
| 	"log" | 	"log" | ||||||
| 	"math/rand" | 	"math/rand" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | 	"runtime" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"sync" | 	"sync" | ||||||
| @ -66,6 +67,9 @@ func emit(final bool) error { | |||||||
| 		return fmt.Errorf("telemetry not enabled") | 		return fmt.Errorf("telemetry not enabled") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// some metrics are updated/set at time of emission | ||||||
|  | 	setEmitTimeMetrics() | ||||||
|  | 
 | ||||||
| 	// ensure only one update happens at a time; | 	// ensure only one update happens at a time; | ||||||
| 	// skip update if previous one still in progress | 	// skip update if previous one still in progress | ||||||
| 	updateMu.Lock() | 	updateMu.Lock() | ||||||
| @ -228,6 +232,17 @@ func stopUpdateTimer() { | |||||||
| 	updateTimerMu.Unlock() | 	updateTimerMu.Unlock() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // setEmitTimeMetrics sets some metrics that should | ||||||
|  | // be recorded just before emitting. | ||||||
|  | func setEmitTimeMetrics() { | ||||||
|  | 	Set("goroutines", runtime.NumGoroutine()) | ||||||
|  | 
 | ||||||
|  | 	var mem runtime.MemStats | ||||||
|  | 	runtime.ReadMemStats(&mem) | ||||||
|  | 	SetNested("memory", "heap_alloc", mem.HeapAlloc) | ||||||
|  | 	SetNested("memory", "sys", mem.Sys) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // makePayloadAndResetBuffer prepares a payload | // makePayloadAndResetBuffer prepares a payload | ||||||
| // by emptying the collection buffer. It returns | // by emptying the collection buffer. It returns | ||||||
| // the bytes of the payload to send to the server. | // the bytes of the payload to send to the server. | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user