{{- if and .Values.istio.create .Values.metrics.tcp }}
# rule to dispatch tcp metrics to handler wavefront-handler
apiVersion: {{ include "wfafi.istio.apiVersion" . }}
kind: rule
metadata:
  name: {{ include "common.names.fullname" . }}-wavefront-tcp-rule
  namespace: {{ .Values.istio.namespace }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  match: context.protocol == "tcp"
  actions:
    - handler: {{ include "common.names.fullname" . }}-wavefront-handler.{{ .Values.istio.namespace }}
      instances:
        - {{ include "common.names.fullname" . }}-tcpsentbytes.instance.{{ .Release.Namespace }}
        - {{ include "common.names.fullname" . }}-tcpreceivedbytes.instance.{{ .Release.Namespace }}
---
# rule to dispatch tcp connection open metric to handler wavefront-handler
apiVersion: {{ include "wfafi.istio.apiVersion" . }}
kind: rule
metadata:
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: wavefront-adapter-for-istio
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  name: {{ include "common.names.fullname" . }}-wavefront-tcp-connection-open-rule
  namespace: {{ .Values.istio.namespace }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  match: context.protocol == "tcp" && connection.event == "open"
  actions:
    - handler: {{ include "common.names.fullname" . }}-wavefront-handler.{{ .Values.istio.namespace }}
      instances:
        - {{ include "common.names.fullname" . }}-tcpconnectionsopened.instance.{{ .Release.Namespace }}
---
# rule to dispatch tcp connection close metric to handler wavefront-handler
apiVersion: {{ include "wfafi.istio.apiVersion" . }}
kind: rule
metadata:
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: wavefront-adapter-for-istio
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  name: {{ include "common.names.fullname" . }}-wavefront-tcp-connection-close-rule
  namespace: {{ .Values.istio.namespace }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  match: context.protocol == "tcp" && connection.event == "close"
  actions:
    - handler: {{ include "common.names.fullname" . }}-wavefront-handler.{{ .Values.istio.namespace }}
      instances:
        - {{ include "common.names.fullname" . }}-tcpconnectionsclosed.instance.{{ .Release.Namespace }}
{{- end }}
