Skip to content

Commit 8caba0e

Browse files
authored
convert latency field to float + new advanced example (#758)
* remove deprecated examples * change latency to float * add example
1 parent dbe0cb7 commit 8caba0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+240
-757
lines changed

dnsutils/message.go

+30-19
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ type DNSTap struct {
150150
Timestamp int64 `json:"-"`
151151
TimeSec int `json:"-"`
152152
TimeNsec int `json:"-"`
153-
Latency float64 `json:"-"`
154-
LatencySec string `json:"latency"`
153+
Latency float64 `json:"latency"`
155154
Payload []byte `json:"-"`
156155
Extra string `json:"extra"`
157156
PolicyRule string `json:"policy-rule"`
@@ -284,15 +283,15 @@ func (dm *DNSMessage) Init() {
284283
Identity: "-",
285284
Version: "-",
286285
TimestampRFC3339: "-",
287-
LatencySec: "-",
288-
Extra: "-",
289-
PolicyRule: "-",
290-
PolicyType: "-",
291-
PolicyMatch: "-",
292-
PolicyAction: "-",
293-
PolicyValue: "-",
294-
PeerName: "-",
295-
QueryZone: "-",
286+
// LatencySec: "-",
287+
Extra: "-",
288+
PolicyRule: "-",
289+
PolicyType: "-",
290+
PolicyMatch: "-",
291+
PolicyAction: "-",
292+
PolicyValue: "-",
293+
PeerName: "-",
294+
QueryZone: "-",
296295
}
297296

298297
dm.DNS = DNS{
@@ -734,7 +733,7 @@ func (dm *DNSMessage) ToTextLine(format []string, fieldDelimiter string, fieldBo
734733
case directive == "qclass":
735734
s.WriteString(dm.DNS.Qclass)
736735
case directive == "latency":
737-
s.WriteString(dm.DNSTap.LatencySec)
736+
s.WriteString(fmt.Sprintf("%.9f", dm.DNSTap.Latency))
738737
case directive == "malformed":
739738
if dm.DNS.MalformedPacket {
740739
s.WriteString("PKTERR")
@@ -1173,7 +1172,7 @@ func (dm *DNSMessage) Flatten() (map[string]interface{}, error) {
11731172
"dns.rcode": dm.DNS.Rcode,
11741173
"dns.questions-count": dm.DNS.QuestionsCount,
11751174
"dnstap.identity": dm.DNSTap.Identity,
1176-
"dnstap.latency": dm.DNSTap.LatencySec,
1175+
"dnstap.latency": dm.DNSTap.Latency,
11771176
"dnstap.operation": dm.DNSTap.Operation,
11781177
"dnstap.timestamp-rfc3339ns": dm.DNSTap.TimestampRFC3339,
11791178
"dnstap.version": dm.DNSTap.Version,
@@ -1406,8 +1405,6 @@ func (dm *DNSMessage) Matching(matching map[string]interface{}) (error, bool) {
14061405
}
14071406

14081407
expectedValue := reflect.ValueOf(value)
1409-
// fmt.Println(nestedKeys, realValue, realValue.Kind(), expectedValue.Kind())
1410-
14111408
switch expectedValue.Kind() {
14121409
// integer
14131410
case reflect.Int:
@@ -1461,7 +1458,15 @@ func matchUserMap(realValue, expectedValue reflect.Value) (bool, error) {
14611458
switch opName {
14621459
// Integer great than ?
14631460
case MatchingOpGreaterThan:
1461+
isFloat, isInt := false, false
1462+
if _, ok := opValue.Interface().(float64); ok {
1463+
isFloat = true
1464+
}
14641465
if _, ok := opValue.Interface().(int); !ok {
1466+
isInt = true
1467+
}
1468+
1469+
if !isFloat && !isInt {
14651470
return false, fmt.Errorf("integer is expected for greater-than operator")
14661471
}
14671472

@@ -1483,12 +1488,18 @@ func matchUserMap(realValue, expectedValue reflect.Value) (bool, error) {
14831488
return false, nil
14841489
}
14851490

1486-
if realValue.Kind() != reflect.Int {
1487-
return false, nil
1491+
if realValue.Kind() == reflect.Float64 {
1492+
if realValue.Interface().(float64) > opValue.Interface().(float64) {
1493+
return true, nil
1494+
}
14881495
}
1489-
if realValue.Interface().(int) > opValue.Interface().(int) {
1490-
return true, nil
1496+
1497+
if realValue.Kind() == reflect.Int {
1498+
if realValue.Interface().(int) > opValue.Interface().(int) {
1499+
return true, nil
1500+
}
14911501
}
1502+
14921503
return false, nil
14931504

14941505
// Integer lower than ?

dnsutils/message_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestDnsMessage_Json_Reference(t *testing.T) {
242242
"identity": "-",
243243
"version": "-",
244244
"timestamp-rfc3339ns": "-",
245-
"latency": "-",
245+
"latency": 0,
246246
"extra": "-",
247247
"policy-type": "-",
248248
"policy-action": "-",
@@ -478,7 +478,7 @@ func TestDnsMessage_JsonFlatten_Reference(t *testing.T) {
478478
"dns.resource-records.ar": "-",
479479
"dns.resource-records.ns": "-",
480480
"dnstap.identity": "-",
481-
"dnstap.latency": "-",
481+
"dnstap.latency": 0,
482482
"dnstap.operation": "-",
483483
"dnstap.timestamp-rfc3339ns": "-",
484484
"dnstap.version": "-",
@@ -818,7 +818,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
818818
format: config.Global.TextFormat,
819819
qname: "dnscollector.fr",
820820
identity: "collector",
821-
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b dnscollector.fr A -",
821+
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b dnscollector.fr A 0.000000000",
822822
},
823823
{
824824
name: "custom_delimiter",
@@ -827,7 +827,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
827827
format: config.Global.TextFormat,
828828
qname: "dnscollector.fr",
829829
identity: "collector",
830-
expected: "-;collector;CLIENT_QUERY;NOERROR;1.2.3.4;1234;-;-;0b;dnscollector.fr;A;-",
830+
expected: "-;collector;CLIENT_QUERY;NOERROR;1.2.3.4;1234;-;-;0b;dnscollector.fr;A;0.000000000",
831831
},
832832
{
833833
name: "empty_delimiter",
@@ -836,7 +836,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
836836
format: config.Global.TextFormat,
837837
qname: "dnscollector.fr",
838838
identity: "collector",
839-
expected: "-collectorCLIENT_QUERYNOERROR1.2.3.41234--0bdnscollector.frA-",
839+
expected: "-collectorCLIENT_QUERYNOERROR1.2.3.41234--0bdnscollector.frA0.000000000",
840840
},
841841
{
842842
name: "qname_quote",
@@ -845,7 +845,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
845845
format: config.Global.TextFormat,
846846
qname: "dns collector.fr",
847847
identity: "collector",
848-
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b \"dns collector.fr\" A -",
848+
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b \"dns collector.fr\" A 0.000000000",
849849
},
850850
{
851851
name: "default_boundary",
@@ -854,7 +854,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
854854
format: config.Global.TextFormat,
855855
qname: "dns\"coll tor\".fr",
856856
identity: "collector",
857-
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b \"dns\\\"coll tor\\\".fr\" A -",
857+
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b \"dns\\\"coll tor\\\".fr\" A 0.000000000",
858858
},
859859
{
860860
name: "custom_boundary",
@@ -863,7 +863,7 @@ func TestDnsMessage_TextFormat_ToString(t *testing.T) {
863863
format: config.Global.TextFormat,
864864
qname: "dnscoll tor.fr",
865865
identity: "collector",
866-
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b !dnscoll tor.fr! A -",
866+
expected: "- collector CLIENT_QUERY NOERROR 1.2.3.4 1234 - - 0b !dnscoll tor.fr! A 0.000000000",
867867
},
868868
{
869869
name: "custom_text",
@@ -939,8 +939,8 @@ func TestDnsMessage_TextFormat_DefaultDirectives(t *testing.T) {
939939
},
940940
{
941941
format: "latency",
942-
dm: DNSMessage{DNSTap: DNSTap{LatencySec: "0.00001"}},
943-
expected: "0.00001",
942+
dm: DNSMessage{DNSTap: DNSTap{Latency: 0.00001}},
943+
expected: "0.000010000",
944944
},
945945
{
946946
format: "qname qtype opcode",

docs/_examples/use-case-1.deprecated.yml

-39
This file was deleted.

docs/_examples/use-case-1.yml

+10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
# This configuration sets up DNS traffic monitoring through DNStap on port 6000
22
# and logging in both text and pcap formats.
3+
#
4+
# As prerequisites, we assume you have a DNS server which supports DNSTap (unbound, bind, powerdns, etc)
5+
# For more informations about dnstap, read the following page: https://dmachard.github.io/posts/0001-dnstap-testing/
6+
#
37

8+
# If turned on, debug messages are printed in the standard output
49
global:
510
trace:
611
verbose: true
712

813
pipelines:
14+
# Listen on tcp/6000 for incoming DNSTap protobuf messages from dns servers
915
- name: tap
1016
dnstap:
1117
listen-ip: 0.0.0.0
1218
listen-port: 6000
1319
routing-policy:
1420
forward: [ text, pcap ]
1521

22+
# Write DNS logs to log file in text format and pcap
23+
# with a maximum size of 100Mb for each files
24+
# A rotation mechanism is implemented with 10 files maximum
25+
# more detail about the text format: doc/configuration.md#custom-text-format
1626
- name: text
1727
logfile:
1828
file-path: "/tmp/dnstap.log"

docs/_examples/use-case-10.deprecated.yml

-34
This file was deleted.

docs/_examples/use-case-10.yml

+10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
# This configuration sets up DNS traffic monitoring through DNStap on port 6000;
22
# and applies tranformation to reduce qname to lowercase
3+
#
4+
# As prerequisites, we assume you have
5+
# - a DNS server which supports DNSTap (unbound, bind, powerdns, etc) for more informations about dnstap,
6+
# read the following page: https://dmachard.github.io/posts/0001-dnstap-testing/
37

8+
# If turned on, debug messages are printed in the standard output
49
global:
510
trace:
611
verbose: true
712

813
pipelines:
14+
# Listen on tcp/6000 for incoming DNSTap protobuf messages from dns servers
915
- name: tap
1016
dnstap:
1117
listen-ip: 0.0.0.0
1218
listen-port: 6000
19+
# Routes DNS messages from the tap collector to standard output
1320
routing-policy:
1421
forward: [ console ]
1522

23+
# Print DNS messages on standard output with TEXT format
24+
# with on tranformation to reduce qname to lowercase
25+
# For example: Wwww.GooGlE.com will be equal to www.google.com
1626
- name: console
1727
stdout:
1828
mode: text

docs/_examples/use-case-11.deprecated.yml

-35
This file was deleted.

docs/_examples/use-case-11.yml

+10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
# This configuration sets up DNS traffic monitoring through DNStap on port 6000;
22
# and add geographical metadata with GeoIP database
3+
#
4+
# As prerequisites, we assume you have
5+
# - a DNS server which supports DNSTap (unbound, bind, powerdns, etc) for more informations about dnstap,
6+
# read the following page: https://dmachard.github.io/posts/0001-dnstap-testing/
37

8+
# If turned on, debug messages are printed in the standard output
49
global:
510
trace:
611
verbose: true
712

813
pipelines:
14+
# Listen on tcp/6000 for incoming DNSTap protobuf messages from dns servers
15+
# and try to add country name in metadata
916
- name: tap
1017
dnstap:
1118
listen-ip: 0.0.0.0
1219
listen-port: 6000
1320
transforms:
1421
geoip:
1522
mmdb-country-file: "./tests/testsdata/GeoLite2-Country.mmdb"
23+
# Routes DNS messages from the tap collector to standard output
1624
routing-policy:
1725
forward: [ console ]
1826

27+
# Print DNS messages on standard output with TEXT format
28+
# Configure a custom text format to display the country name
1929
- name: console
2030
stdout:
2131
mode: text

0 commit comments

Comments
 (0)