mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Merge pull request #1165 from aishraj/master
Have milliseconds as the latency log time unit
This commit is contained in:
		
						commit
						5d9989405a
					
				@ -286,11 +286,22 @@ func (r *replacer) getSubstitution(key string) string {
 | 
			
		||||
			return r.emptyValue
 | 
			
		||||
		}
 | 
			
		||||
		return roundDuration(time.Since(r.responseRecorder.start)).String()
 | 
			
		||||
	case "{latency_ms}":
 | 
			
		||||
		if r.responseRecorder == nil {
 | 
			
		||||
			return r.emptyValue
 | 
			
		||||
		}
 | 
			
		||||
		elapsedDuration := time.Since(r.responseRecorder.start)
 | 
			
		||||
		return strconv.FormatInt(convertToMilliseconds(elapsedDuration), 10)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r.emptyValue
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//convertToMilliseconds returns the number of milliseconds in the given duration
 | 
			
		||||
func convertToMilliseconds(d time.Duration) int64 {
 | 
			
		||||
	return d.Nanoseconds() / 1e6
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set sets key to value in the r.customReplacements map.
 | 
			
		||||
func (r *replacer) Set(key, value string) {
 | 
			
		||||
	r.customReplacements["{"+key+"}"] = value
 | 
			
		||||
 | 
			
		||||
@ -150,3 +150,21 @@ func TestRound(t *testing.T) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMillisecondConverstion(t *testing.T) {
 | 
			
		||||
	var testCases = map[time.Duration]int64{
 | 
			
		||||
		2 * time.Second:           2000,
 | 
			
		||||
		9039492 * time.Nanosecond: 9,
 | 
			
		||||
		1000 * time.Microsecond:   1,
 | 
			
		||||
		127 * time.Nanosecond:     0,
 | 
			
		||||
		0 * time.Millisecond:      0,
 | 
			
		||||
		255 * time.Millisecond:    255,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for dur, expected := range testCases {
 | 
			
		||||
		numMillisecond := convertToMilliseconds(dur)
 | 
			
		||||
		if numMillisecond != expected {
 | 
			
		||||
			t.Errorf("Expected %v. Got %v", expected, numMillisecond)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user