querylog: imp code docs

This commit is contained in:
Dimitry Kolyshev 2023-06-02 12:09:50 +03:00
parent e9b17891bb
commit dafc785845
3 changed files with 17 additions and 19 deletions

View file

@ -10,7 +10,7 @@ import (
) )
// csvHeaderRow is a slice of strings with row names for CSV header. This // csvHeaderRow is a slice of strings with row names for CSV header. This
// const should correspond with [logEntry.toCSV] func. // slice should correspond with [logEntry.toCSV] func.
var csvHeaderRow = []string{ var csvHeaderRow = []string{
"ans_dnssec", "ans_dnssec",
"ans_rcode", "ans_rcode",
@ -46,7 +46,7 @@ func (e *logEntry) toCSV() (out []string) {
aData := ansData(e) aData := ansData(e)
return []string{ return []string{
aData.dnsSec, strconv.FormatBool(e.AuthenticatedData),
aData.rCode, aData.rCode,
aData.typ, aData.typ,
aData.value, aData.value,
@ -69,7 +69,6 @@ func (e *logEntry) toCSV() (out []string) {
// csvAnswer is a helper struct for csv row answer fields. // csvAnswer is a helper struct for csv row answer fields.
type csvAnswer struct { type csvAnswer struct {
dnsSec string
rCode string rCode string
typ string typ string
value string value string
@ -90,10 +89,6 @@ func ansData(entry *logEntry) (out csvAnswer) {
out.rCode = dns.RcodeToString[msg.Rcode] out.rCode = dns.RcodeToString[msg.Rcode]
// Old query logs may still keep AD flag value in the message. Try to get
// it from there as well.
out.dnsSec = strconv.FormatBool(entry.AuthenticatedData || msg.AuthenticatedData)
if len(msg.Answer) == 0 { if len(msg.Answer) == 0 {
return out return out
} }

View file

@ -99,6 +99,11 @@ func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponse(w, r, resp) _ = aghhttp.WriteJSONResponse(w, r, resp)
} }
// exportChunkSize is a size of one search-flush iteration for query log export.
//
// TODO(a.meshkov): Consider making configurable.
const exportChunkSize = 500
// handleQueryLogExport is the handler for the GET /control/querylog/export // handleQueryLogExport is the handler for the GET /control/querylog/export
// HTTP API. // HTTP API.
func (l *queryLog) handleQueryLogExport(w http.ResponseWriter, r *http.Request) { func (l *queryLog) handleQueryLogExport(w http.ResponseWriter, r *http.Request) {
@ -110,8 +115,7 @@ func (l *queryLog) handleQueryLogExport(w http.ResponseWriter, r *http.Request)
} }
params := &searchParams{ params := &searchParams{
// TODO(a.meshkov): Consider making configurable. limit: exportChunkSize,
limit: 500,
searchCriteria: searchCriteria, searchCriteria: searchCriteria,
} }
@ -119,12 +123,6 @@ func (l *queryLog) handleQueryLogExport(w http.ResponseWriter, r *http.Request)
w.Header().Set(httphdr.ContentDisposition, "attachment;filename=data.csv") w.Header().Set(httphdr.ContentDisposition, "attachment;filename=data.csv")
csvWriter := csv.NewWriter(w) csvWriter := csv.NewWriter(w)
defer func() {
if err = csvWriter.Error(); err != nil {
// TODO(a.garipov): Set Trailer X-Error header.
log.Error("%s %s %s: %s", r.Method, r.Host, r.URL, "writing csv")
}
}()
// Write header. // Write header.
if err = csvWriter.Write(csvHeaderRow); err != nil { if err = csvWriter.Write(csvHeaderRow); err != nil {
@ -160,6 +158,11 @@ func (l *queryLog) handleQueryLogExport(w http.ResponseWriter, r *http.Request)
csvWriter.Flush() csvWriter.Flush()
} }
if err = csvWriter.Error(); err != nil {
// TODO(a.garipov): Set Trailer X-Error header.
log.Error("%s %s %s: %s", r.Method, r.Host, r.URL, "writing csv")
}
} }
// handleQueryLogClear is the handler for the POST /control/querylog/clear HTTP // handleQueryLogClear is the handler for the POST /control/querylog/clear HTTP

View file

@ -11,7 +11,7 @@
* The new `GET /control/querylog/export` HTTP API allows an export of query log * The new `GET /control/querylog/export` HTTP API allows an export of query log
items in the CSV file. It returns a CSV object with the following format: items in the CSV file. It returns a CSV object with the following format:
```text ```csv
ans_dnssec,ans_rcode,ans_type,ans_value,cached,client_ip,client_id,ecs,elapsed,filter_id,filter_rule,proto,qclass,qname,qtype,reason,time,upstream ans_dnssec,ans_rcode,ans_type,ans_value,cached,client_ip,client_id,ecs,elapsed,filter_id,filter_rule,proto,qclass,qname,qtype,reason,time,upstream
false,NOERROR,A,192.168.1.1,false,127.0.0.1,,,0.097409,,,,IN,example.com,A,Rewrite,2023-01-30T12:21:13.947563+07:00, false,NOERROR,A,192.168.1.1,false,127.0.0.1,,,0.097409,,,,IN,example.com,A,Rewrite,2023-01-30T12:21:13.947563+07:00,
false,NOERROR,A,45.33.2.79,false,127.0.0.1,,,482.967871,,,,IN,test.com,A,NotFilteredNotFound,2022-12-13T12:18:04.964403+07:00,https://dns10.quad9.net:443/dns-query false,NOERROR,A,45.33.2.79,false,127.0.0.1,,,482.967871,,,,IN,test.com,A,NotFilteredNotFound,2022-12-13T12:18:04.964403+07:00,https://dns10.quad9.net:443/dns-query