Skip to content

Instantly share code, notes, and snippets.

@carlin-q-scott
Created February 24, 2023 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlin-q-scott/54d7170b19938b45d2e69d5b73cd8fc1 to your computer and use it in GitHub Desktop.
Save carlin-q-scott/54d7170b19938b45d2e69d5b73cd8fc1 to your computer and use it in GitHub Desktop.

Azure Application Insights in Kubernetes for Java

This gist describes how to add Kubernetes customDismensions to the Java agent for Application Insights.

It's supplying the same customDimensions that are provided by the asp.net 6+ instrumentiation, except that not all of those dimensions are available using the Kubernetes Downward API

How it Works

The supplied app-insights-java.yaml patch file configures customDimensions for the app insights agent using the APPLICATIONINSIGHTS_CONFIGURATION_CONTENT env var. This will override your applicationinsights.json configuration file if you were using one.

The example kustomization.yaml applies the patch to all deployments with the instrumentation.opentelemetry.io/inject-java: "true" annotation. This is the same annotation that is used by the Open Telemetry Kuberentes Operator. However, that operator selects pods with the annotation.

NOTE

Only the first container in each pod will have the configuration injected. If you want to inject into other pods, then you will need to replace the 0 in the path:s with the container index.

- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: POD_ID
valueFrom:
fieldRef:
fieldPath: metadata.uid
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: POD_SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: POD_LABELS_APP
valueFrom:
fieldRef:
fieldPath: metadata.labels['app']
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: APPLICATIONINSIGHTS_CONFIGURATION_CONTENT
value: |
{
"customDimensions": {
"Kubernetes.Container.Name": "${POD_LABELS_APP}",
"Kubernetes.Deployment.Name": "${POD_LABELS_APP}",
"Kubernetes.Node.Name": "${NODE_NAME}",
"Kubernetes.Pod.ID": "${POD_ID}",
"Kubernetes.Pod.Labels": "app=${POD_LABELS_APP}",
"Kubernetes.Pod.Name": "${POD_NAME}",
"Kubernetes.Pod.Namespace": "${POD_NAMESPACE}",
"Kubernetes.Pod.ServiceAccount": "${POD_SERVICE_ACCOUNT}"
}
}
## Use these to see all the available labels and annotations
# - op: add
# path: /spec/template/spec/containers/0/volumeMounts/-
# value:
# name: podinfo
# mountPath: /etc/podinfo
# - op: add
# path: /spec/template/spec/volumes/-
# value:
# name: podinfo
# downwardAPI:
# items:
# - path: "labels"
# fieldRef:
# fieldPath: metadata.labels
# - path: "annotations"
# fieldRef:
# fieldPath: metadata.annotations
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- my-app.yaml
patches:
- path: app-insights-java.yaml
target:
kind: Deployment
annotationSelector: instrumentation.opentelemetry.io/inject-java=true
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
annotations:
instrumentation.opentelemetry.io/inject-java: "true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment