// processor tries to parse an http request from each packet, and if // successful it records metrics about it in the loghisto metric system. func processor(ms *loghisto.MetricSystem, packetsIn chan *pcap.Packet) { for pkt := range packetsIn { req, reqErr := http.ReadRequest(bufio.NewReader(bytes.NewReader(pkt.Payload))) if reqErr == nil { ms.Counter(req.Method+" "+req.URL.Path, 1) } } }
// processor tries to parse an http request or response from each packet, // and if successful it records metrics about it in the loghisto metric // system. On successful parse of a response, it looks up a corresponding // request so that it can record statistics about http verbs / individual // paths being hit. func processor(ms *loghisto.MetricSystem, packetsIn chan *pcap.Packet) { reqTimers := map[uint32]loghisto.TimerToken{} reqVerbTimers := map[uint32]loghisto.TimerToken{} globalTimers := map[uint32]loghisto.TimerToken{} reqVerb := map[uint32]string{} for pkt := range packetsIn { var token uint32 req, reqErr := http.ReadRequest(bufio.NewReader(bytes.NewReader(pkt.Payload))) if reqErr == nil { token = (uint32(pkt.TCP.SrcPort) << 8) + uint32(pkt.TCP.DestPort) reqTimers[token] = ms.StartTimer("timer " + req.Method + " " + req.URL.Path) reqVerbTimers[token] = ms.StartTimer("verbtimer " + req.Method) globalTimers[token] = ms.StartTimer("globaltimer") ms.Histogram("size "+req.Method+" "+req.URL.Path, float64(req.ContentLength)) ms.Counter(req.Method+" "+req.URL.Path, 1) reqVerb[token] = req.Method } else { res, resErr := http.ReadResponse(bufio.NewReader(bytes.NewReader(pkt.Payload)), nil) if resErr != nil { // not a valid request or response continue } ms.Histogram("globalsize", float64(res.ContentLength)) token = (uint32(pkt.TCP.DestPort) << 8) + uint32(pkt.TCP.SrcPort) reqTimer, present := reqTimers[token] if present { reqTimer.Stop() delete(reqTimers, token) } verbTimer, present := reqVerbTimers[token] if present { verbTimer.Stop() delete(reqVerbTimers, token) } globalTimer, present := globalTimers[token] if present { globalTimer.Stop() delete(globalTimers, token) } verb, present := reqVerb[token] if present { ms.Histogram("verbsize "+verb, float64(res.ContentLength)) delete(reqVerb, token) } } } }