Skip to content

Commit

Permalink
add hello-kubernetes and pod label/annotation service_name tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rlankfo committed Jan 29, 2025
1 parent 2cd354c commit 6dc7369
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ discovery.relabel "filtered_pods" {
regex = "__meta_kubernetes_pod_annotation_(.+)"
}

// explicitly set service_name. if not set, loki will automatically try to populate a default.
// see https://grafana.com/docs/loki/latest/get-started/labels/#default-labels-for-all-users
//
// choose the first value found from the following ordered list:
// - pod.annotation[resource.opentelemetry.io/service.name]
// - pod.label[app.kubernetes.io/name]
// - k8s.deployment.name (excluded: discovery.kubernetes does note expose this data)
// - k8s.replicaset.name (excluded: discovery.kubernetes does not expose this data)
// - k8s.statefulset.name (excluded: discovery.kubernetes does not expose this data)
// - k8s.daemonset.name (excluded: discovery.kubernetes does not expose this data)
// - k8s.cronjob.name (excluded: discovery.kubernetes does not have cronjob role)
// - k8s.job.name (excluded: discovery.kubernetes does not have job role)
// - k8s.pod.name
// - k8s.container.name
rule {
action = "replace"
source_labels = [
"__meta_kubernetes_pod_annotation_resource_opentelemetry_io_service_name",
"__meta_kubernetes_pod_label_app_kubernetes_io_name",
"__meta_kubernetes_pod_name",
"__meta_kubernetes_pod_container_name",
]
separator = ";"
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "service_name"
}




{{- if .Values.extraDiscoveryRules }}
{{ .Values.extraDiscoveryRules | indent 2 }}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ deployments/test-variables.yaml:
kubectl create configmap test-variables \
--from-literal=CLUSTER="$(shell yq eval '.cluster.name' values.yaml)-$$RANDOM_NUMBER" \
--from-literal=RANDOM_NUMBER="$$RANDOM_NUMBER" \
--from-literal=ANNOTATION_POD_LOG_SERVICE_NAME="hello-kubernetes-annotation" \
--from-literal=LABEL_POD_LOG_SERVICE_NAME="hello-kubernetes-label" \
-o yaml --dry-run=client >> $@

deployments/grafana-cloud-credentials.yaml:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ spec:
- query: count_over_time({cluster="$CLUSTER", job!="integrations/kubernetes/eventhandler"}[1h])
type: logql

# Pod logs service_name label
- query: count_over_time({cluster="$CLUSTER", service_name!=""}[1h])
type: logql

# Pod logs service_name label from resource.opentelemetry.io/service.name annotation
- query: count_over_time({cluster="$CLUSTER", service_name="$ANNOTATION_POD_LOG_SERVICE_NAME"}[1h])
type: logql

# Pod logs service_name label from app.kubernetes.io/name label
- query: count_over_time({cluster="$CLUSTER", service_name="$LABEL_POD_LOG_SERVICE_NAME"}[1h])
type: logql

# Alloy integration
- query: alloy_build_info{cluster="$CLUSTER"}
type: promql
Expand All @@ -82,3 +94,110 @@ spec:
expect:
value: 1
operator: ==
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-kubernetes-test-0
labels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
app.kubernetes.io/version: "1.10"
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
template:
metadata:
labels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
spec:
containers:
- name: hello-kubernetes
image: "paulbouwer/hello-kubernetes:1.10"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: KUBERNETES_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: CONTAINER_IMAGE
value: "paulbouwer/hello-kubernetes:1.10"
---
# Source: hello-kubernetes/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-kubernetes-test-1
labels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
app.kubernetes.io/version: "1.10"
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
template:
metadata:
labels:
app.kubernetes.io/name: "hello-kubernetes-label"
app.kubernetes.io/instance: test
annotations:
resource.opentelemetry.io/service.name: "hello-kubernetes-annotation"
spec:
containers:
- name: hello-kubernetes
image: "paulbouwer/hello-kubernetes:1.10"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: KUBERNETES_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: CONTAINER_IMAGE
value: "paulbouwer/hello-kubernetes:1.10"

Check failure on line 203 in charts/k8s-monitoring/tests/platform/grafana-cloud-features/k8s-monitoring/deployments/query-test.yaml

View workflow job for this annotation

GitHub Actions / runner / yamllint

[yamllint] reported by reviewdog 🐶 [error] no new line character at the end of file (new-line-at-end-of-file) Raw Output: ./charts/k8s-monitoring/tests/platform/grafana-cloud-features/k8s-monitoring/deployments/query-test.yaml:203:56: [error] no new line character at the end of file (new-line-at-end-of-file)

0 comments on commit 6dc7369

Please sign in to comment.