From d9109334a6e3b7987f045f852a8e0b3512ab9ae9 Mon Sep 17 00:00:00 2001 From: Julien Pinsonneau Date: Wed, 2 Aug 2023 17:01:46 +0200 Subject: [PATCH] metrics query --- pkg/loki/flow_query.go | 8 ++++++++ pkg/loki/topology_query.go | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/loki/flow_query.go b/pkg/loki/flow_query.go index a1f834647..7d4a66a52 100644 --- a/pkg/loki/flow_query.go +++ b/pkg/loki/flow_query.go @@ -289,6 +289,14 @@ func (q *FlowQueryBuilder) appendDNSRCodeFilter(sb *strings.Builder) { sb.WriteString("`") } +func (q *FlowQueryBuilder) appendRTTFilter(sb *strings.Builder) { + // ensure at TimeFlowRttNs field is specified + // |~`"TimeFlowRttNs"` + sb.WriteString("|~`") + sb.WriteString(`"TimeFlowRttNs"`) + sb.WriteString("`") +} + func (q *FlowQueryBuilder) appendJSON(sb *strings.Builder, forceAppend bool) { if forceAppend || len(q.jsonFilters) > 0 { sb.WriteString("|json") diff --git a/pkg/loki/topology_query.go b/pkg/loki/topology_query.go index 9e799ea31..93ebc92b7 100644 --- a/pkg/loki/topology_query.go +++ b/pkg/loki/topology_query.go @@ -1,6 +1,7 @@ package loki import ( + "fmt" "strings" "github.com/netobserv/network-observability-console-plugin/pkg/utils" @@ -23,6 +24,8 @@ type Topology struct { skipEmptyDropCause bool skipNonDNS bool skipEmptyDNSRCode bool + skipEmptyRTT bool + factor float64 } type TopologyQueryBuilder struct { @@ -40,6 +43,7 @@ func NewTopologyQuery(cfg *Config, start, end, limit, rateInterval, step, metric fields := getFields(aggregate, groups) var f, t string + factor := 1.0 switch metricType { case "count", "countDns": f = "count_over_time" @@ -55,6 +59,10 @@ func NewTopologyQuery(cfg *Config, start, end, limit, rateInterval, step, metric case "dnsLatencies": f = "avg_over_time" t = "DnsLatencyMs" + case "flowRtt": + f = "avg_over_time" + t = "TimeFlowRttNs" + factor = 0.000001 // nanoseconds to miliseconds default: f = "rate" t = "Bytes" @@ -84,6 +92,8 @@ func NewTopologyQuery(cfg *Config, start, end, limit, rateInterval, step, metric skipEmptyDropCause: aggregate == "droppedCause", skipNonDNS: metricType == "dnsLatencies" || metricType == "countDns", skipEmptyDNSRCode: aggregate == "dnsRCode", + skipEmptyRTT: metricType == "flowRtt", + factor: factor, }, }, nil } @@ -178,6 +188,10 @@ func (q *TopologyQueryBuilder) Build() string { q.appendDNSFilter(sb) } + if q.topology.skipEmptyRTT { + q.appendRTTFilter(sb) + } + q.appendJSON(sb, true) if len(q.topology.dataField) > 0 { sb.WriteString("|unwrap ") @@ -190,7 +204,12 @@ func (q *TopologyQueryBuilder) Build() string { } else { sb.WriteString(q.topology.rateInterval) } - sb.WriteString("])))") + sb.WriteString("])") + if q.topology.factor != 1 { + sb.WriteRune('*') + sb.WriteString(strings.Replace(fmt.Sprintf("%f", q.topology.factor), ",", ".", 1)) + } + sb.WriteString("))") q.appendQueryParams(sb) sb.WriteString("&step=") sb.WriteString(q.topology.step)