func writeHistogramForDuration(expvarMap *expvar.Map, duration time.Duration, prefix string) {

	if base.LogEnabled("PerfStats") {
		var durationMs int
		if duration < 1*time.Second {
			durationMs = int(duration/(100*time.Millisecond)) * 100
		} else {
			durationMs = int(duration/(1000*time.Millisecond)) * 1000
		}
		expvarMap.Add(fmt.Sprintf("%s-%06dms", prefix, durationMs), 1)
	}
}
func (h *handler) logRequestLine() {
	if !base.LogEnabled("HTTP") {
		return
	}
	as := ""
	if h.privs == adminPrivs {
		as = "  (ADMIN)"
	} else if h.user != nil && h.user.Name() != "" {
		as = fmt.Sprintf("  (as %s)", h.user.Name())
	}
	base.LogTo("HTTP", " #%03d: %s %s%s", h.serialNumber, h.rq.Method, h.rq.URL, as)
}
Exemple #3
0
func (h *handler) logRequestLine() {
	if !base.LogEnabled("HTTP") {
		return
	}
	as := ""
	if h.privs == adminPrivs {
		as = "  (ADMIN)"
	} else if h.user != nil && h.user.Name() != "" {
		as = fmt.Sprintf("  (as %s)", h.user.Name())
	}
	proto := ""
	if h.rq.ProtoMajor >= 2 {
		proto = " HTTP/2"
	}

	base.LogTo("HTTP", " #%03d: %s %s%s%s", h.serialNumber, h.rq.Method, sanitizeRequestURL(h.rq.URL), proto, as)
}
// Performs an HTTP POST to the url defined for the handler.  If a filter function is defined,
// calls it to determine whether to POST.  The payload for the POST is depends
// on the event type.
func (wh *Webhook) HandleEvent(event Event) {

	var payload *bytes.Buffer
	var contentType string
	if wh.filter != nil {
		// If filter function is defined, use it to determine whether to post
		success, err := wh.filter.CallValidateFunction(event)
		if err != nil {
			base.Warn("Error calling webhook filter function: %v", err)
		}

		// If filter returns false, cancel webhook post
		if !success {
			return
		}
	}

	// Different events post different content by default
	switch event := event.(type) {
	case *DocumentChangeEvent:
		// for DocumentChangeEvent, post document body
		jsonOut, err := json.Marshal(event.Doc)
		if err != nil {
			base.Warn("Error marshalling doc for webhook post")
			return
		}
		contentType = "application/json"
		payload = bytes.NewBuffer(jsonOut)
	case *DBStateChangeEvent:
		// for DBStateChangeEvent, post JSON document with the following format
		//{
		//	“admininterface":"127.0.0.1:4985",
		//	“dbname":"db",
		//	“localtime":"2015-10-07T11:20:29.138+01:00",
		//	"reason":"DB started from config”,
		//	“state”:"online"
		//}
		jsonOut, err := json.Marshal(event.Doc)
		if err != nil {
			base.Warn("Error marshalling doc for webhook post")
			return
		}
		contentType = "application/json"
		payload = bytes.NewBuffer(jsonOut)
	default:
		base.Warn("Webhook invoked for unsupported event type.")
		return
	}
	func() {
		resp, err := wh.client.Post(wh.url, contentType, payload)
		defer func() {
			// Ensure we're closing the response, so it can be reused
			if resp != nil && resp.Body != nil {
				io.Copy(ioutil.Discard, resp.Body)
				resp.Body.Close()
			}
		}()

		if err != nil {
			base.Warn("Error attempting to post %s to url %s: %s -- %+v", event.String(), wh.SanitizedUrl(), err)
			return
		}

		if base.LogEnabled("Events+") {
			base.LogTo("Events+", "Webhook handler ran for event.  Payload %s posted to URL %s, got status %s",
				payload, wh.SanitizedUrl(), resp.Status)
		}
	}()

}