Skip to content

Commit

Permalink
metrics query
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Aug 2, 2023
1 parent 89f994f commit d910933
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/loki/flow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 20 additions & 1 deletion pkg/loki/topology_query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loki

import (
"fmt"
"strings"

"github.com/netobserv/network-observability-console-plugin/pkg/utils"
Expand All @@ -23,6 +24,8 @@ type Topology struct {
skipEmptyDropCause bool
skipNonDNS bool
skipEmptyDNSRCode bool
skipEmptyRTT bool
factor float64
}

type TopologyQueryBuilder struct {
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 ")
Expand All @@ -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)
Expand Down

0 comments on commit d910933

Please sign in to comment.