Skip to content

Commit fcf2965

Browse files
committed
code: Add SLI for Slowqueries
Signed-off-by: Harsh Mishra <hmisraji07@gmail.com>
1 parent 83d6a7b commit fcf2965

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

codebundles/postgres/sli.robot

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
*** Settings ***
2+
Metadata Author Harsh Mishra
3+
Documentation Checks for Long runnning queries in Postgres Clusters within Kubernetes
4+
Force Tags Postgres Kubernetes Zalando
5+
Metadata Display Name Postgres Slowquery Inspection
6+
Metadata Supports Kubernetes,AKS,EKS,GKE,OpenShift,Postgres,Zalando
7+
Library RW.Core
8+
Library RW.CLI
9+
Library String
10+
11+
Suite Setup Suite Initialization
12+
13+
*** Tasks ***
14+
Queries executing more than ${TIME_INTERVAL} milliseconds
15+
[Documentation] Returns Total No Of Queries Running For More Than ${TIME_INTERVAL} Seconds on the cluster
16+
[Tags] postgres query inspection slowquery zalando
17+
${query}= Set Variable SELECT COUNT(*) FROM (SELECT pid,user,pg_stat_activity.query_start,now() - pg_stat_activity.query_start AS query_time,query,state,wait_event_type,wait_event FROM pg_stat_activity WHERE (now() - pg_stat_activity.query_start) > interval '\\''${TIME_INTERVAL} milliseconds'\\'') AS foo
18+
${cmd}= Set Variable for pod in $(${KUBERNETES_DISTRIBUTION_BINARY} get pods -n ${NAMESPACE} --no-headers -l cluster-name=${CLUSTER_NAME_POSTGRES} 2> /dev/null | awk '{print $1};') ;do kubectl exec -n postgres-database --context ${CONTEXT} $pod -- psql -U ${PGUSER} -d ${DATABASE} -c '${query}' --tuples-only ; done > /tmp/psqlout && awk \'{Total=Total+$1} END{print Total}\' /tmp/psqlout
19+
${value}= RW.CLI.Run Cli
20+
... cmd=${cmd}
21+
... env=${env}
22+
... render_in_commandlist=false
23+
... include_in_history=false
24+
... secret_file__kubeconfig=${kubeconfig}
25+
26+
${value}= Convert To String ${value.stdout}
27+
${value}= Strip String ${value}
28+
${value}= Convert To Integer ${value}
29+
30+
RW.Core.Push Metric
31+
... value=${value}
32+
33+
*** Keywords ***
34+
Suite Initialization
35+
${kubeconfig}= RW.Core.Import Secret
36+
... kubeconfig
37+
... type=string
38+
... description=The kubernetes kubeconfig yaml containing connection configuration used to connect to cluster(s).
39+
... pattern=\w*
40+
... example=For examples, start here https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
41+
42+
${NAMESPACE}= RW.Core.Import User Variable NAMESPACE
43+
... type=string
44+
... description=The name of the Kubernetes namespace to scope actions and searching to.
45+
... pattern=\w*
46+
... example=my-namespace
47+
... default=postgres-database
48+
49+
${CONTEXT}= RW.Core.Import User Variable CONTEXT
50+
... type=string
51+
... description=Which Kubernetes context to operate within.
52+
... pattern=\w*
53+
... example=my-main-cluster
54+
... default=sandbox-cluster-1
55+
56+
${KUBERNETES_DISTRIBUTION_BINARY}= RW.Core.Import User Variable KUBERNETES_DISTRIBUTION_BINARY
57+
... type=string
58+
... description=Which binary to use for Kubernetes CLI commands.
59+
... enum=[kubectl,oc]
60+
... example=kubectl
61+
... default=kubectl
62+
63+
${TIME_INTERVAL}= RW.Core.Import User Variable TIME_INTERVAL
64+
... type=integer
65+
... description=Time interval (milliseconds) to measure in Range 1 to 604800 both included
66+
... example=1000
67+
... default=1000
68+
69+
${CLUSTER_NAME_POSTGRES}= RW.Core.Import User Variable CLUSTER_NAME_POSTGRES
70+
... type=string
71+
... description=The Postgres Cluster name or the name of cluster as defined in Zalando Manifests. For example, https://docs.runwhen.com/public/runwhen-authors/sandbox-resources/postgres-operator-and-test-database
72+
... pattern=\w*
73+
... example=acid-minimal-cluster
74+
... default=acid-minimal-cluster
75+
76+
${DATABASE}= RW.Core.Import User Variable DATABASE
77+
... type=string
78+
... description=The database to inspect for long running queries
79+
... pattern=\w*
80+
... example=postgres
81+
... default=foo
82+
83+
${PGUSER}= RW.Core.Import User Variable PGUSER
84+
... type=string
85+
... description=database user
86+
... pattern=\w*
87+
... example=postgres
88+
... default=postgres
89+
90+
${HOME}= RW.Core.Import User Variable HOME
91+
92+
Set Suite Variable ${kubeconfig} ${kubeconfig}
93+
# Set Suite Variable ${kubectl} ${kubectl}
94+
Set Suite Variable ${KUBERNETES_DISTRIBUTION_BINARY} ${KUBERNETES_DISTRIBUTION_BINARY}
95+
Set Suite Variable ${CONTEXT} ${CONTEXT}
96+
Set Suite Variable ${NAMESPACE} ${NAMESPACE}
97+
Set Suite Variable ${HOME} ${HOME}
98+
Set Suite Variable ${TIME_INTERVAL} ${TIME_INTERVAL}
99+
Set Suite Variable ${CLUSTER_NAME_POSTGRES} ${CLUSTER_NAME_POSTGRES}
100+
Set Suite Variable ${PGUSER} ${PGUSER}
101+
Set Suite Variable ${DATABASE} ${DATABASE}
102+
Set Suite Variable
103+
... ${env}
104+
... {"KUBECONFIG":"./${kubeconfig.key}", "KUBERNETES_DISTRIBUTION_BINARY":"${KUBERNETES_DISTRIBUTION_BINARY}", "CONTEXT":"${CONTEXT}", "NAMESPACE":"${NAMESPACE}", "HOME":"${HOME}", "TIME_INTERVAL":"${TIME_INTERVAL}", "PGUSER":"${PGUSER}", "CLUSTER_NAME_POSTGRES": "${CLUSTER_NAME_POSTGRES}", "DATABASE": "${DATABASE}"}

0 commit comments

Comments
 (0)