Skip to content

Commit fb1a95d

Browse files
feat(helm): Add tracing to medcat-trainer (#52)
* feat(helm): Add tracing to medcat-trainer * feat(medcat-trainer): Add semantic convention resource attributes
1 parent 8414ef4 commit fb1a95d

6 files changed

Lines changed: 105 additions & 3 deletions

File tree

deployment/kubernetes/charts/cogstack-helm-ce/templates/NOTES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export OPENSEARCH_DASHBOARD_PORT={{ index .Values "opensearch-dashboards" "servi
2929
kubectl --namespace {{ .Release.Namespace }} port-forward svc/$OPENSEARCH_DASHBOARD_SERVICE 5601:$OPENSEARCH_DASHBOARD_PORT &
3030
{{- end }}
3131

32-
# 5. Jupyterhub
3332
{{- if index .Values "cogstack-jupyterhub" "enabled" }}
33+
# 5. Jupyterhub
3434
export JUPYTERHUB_PROXY_PUBLIC_SERVICE=proxy-public
3535
export JUPYTERHUB_PROXY_SERVICE_PORT=$(kubectl get svc $JUPYTERHUB_PROXY_PUBLIC_SERVICE -o jsonpath="{.spec.ports[0].port}" )
3636
kubectl --namespace {{ .Release.Namespace }} port-forward svc/$JUPYTERHUB_PROXY_PUBLIC_SERVICE 8000:$JUPYTERHUB_PROXY_SERVICE_PORT &

deployment/kubernetes/charts/cogstack-helm-ce/values.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ medcat-service:
2424
repository: cogstacksystems/medcat-service
2525
tag: "1.2.0"
2626

27-
2827
medcat-trainer:
2928
image:
30-
tag: "latest@sha256:dcbd9f7c480dcb88e072189d233a146317f210a826573fd0c47d469da5360be0"
29+
tag: latest@sha256:103215a7540ad614c32866f4cb00ddd91e7aff37cea9abc25dc226c577f9506d
3130
provisioning:
3231
enabled: true
3332
existingConfigMap:

deployment/kubernetes/charts/medcat-trainer-helm/templates/_helpers.tpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ Return full Solr URL: combines host and port
9797
http://{{ include "medcat-trainer-helm.solrHost" . }}:{{ include "medcat-trainer-helm.solrPort" . }}
9898
{{- end }}
9999

100+
{{/*
101+
Validate tracing.otlp: when otlp.enabled is true, at least one of grpc.enabled or http.enabled must be true.
102+
*/}}
103+
{{- define "medcat-trainer-helm.validateTracing" -}}
104+
{{- if and .Values.tracing .Values.tracing.otlp (eq .Values.tracing.otlp.enabled true) -}}
105+
{{- if not (or (index .Values.tracing.otlp.grpc "enabled") (index .Values.tracing.otlp.http "enabled")) -}}
106+
{{- fail "tracing.otlp.enabled is true but neither tracing.otlp.grpc.enabled nor tracing.otlp.http.enabled is true. Enable at least one of tracing.otlp.grpc.enabled or tracing.otlp.http.enabled." -}}
107+
{{- end -}}
108+
{{- end -}}
109+
{{- end -}}
110+
111+
{{/*
112+
Convert tracing.resourceAttributes (object) to OTEL CSV format: key1=value1,key2=value2,...
113+
Values in the map are templated (e.g. "{{ .Release.Name }}") so they are evaluated at render time.
114+
*/}}
115+
{{- define "medcat-trainer-helm.tracingResourceAttributesCsv" -}}
116+
{{- $root := . -}}
117+
{{- $parts := list -}}
118+
{{- range $name, $value := .Values.tracing.resourceAttributes -}}
119+
{{- $parts = append $parts (printf "%s=%s" $name (tpl (toString $value) $root)) -}}
120+
{{- end -}}
121+
{{- join "," $parts | quote -}}
122+
{{- end -}}
123+
100124
{{- define "postgres.service-name" -}}
101125
{{ include "postgresql.v1.primary.fullname" (dict "Values" .Values.postgresql "Chart" (dict "Name" "postgresql") "Release" .Release) }}
102126
{{- end -}}

deployment/kubernetes/charts/medcat-trainer-helm/templates/medcat-trainer-deployment.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- include "medcat-trainer-helm.validateTracing" . -}}
12
apiVersion: apps/v1
23
kind: Deployment
34
metadata:
@@ -58,6 +59,18 @@ spec:
5859
name: {{ include "medcat-trainer-helm.fullname" . }}-env
5960
- secretRef:
6061
name: {{ include "medcat-trainer-helm.fullname" . }}-secret
62+
env:
63+
{{- range $key, $value := .Values.envValueFrom }}
64+
- name: {{ $key | quote }}
65+
valueFrom:
66+
{{- tpl (toYaml $value) $ | nindent 14 }}
67+
{{- end }}
68+
{{- if .Values.tracing.otlp.enabled }}
69+
- name: K8S_CONTAINER_NAME
70+
value: medcat-trainer
71+
- name: OTEL_RESOURCE_ATTRIBUTES
72+
value: {{ include "medcat-trainer-helm.tracingResourceAttributesCsv" . }}
73+
{{- end }}
6174
{{- with .Values.livenessProbe }}
6275
livenessProbe:
6376
{{- toYaml . | nindent 12 }}

deployment/kubernetes/charts/medcat-trainer-helm/templates/medcat-trainer-env-configmap.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ data:
1414
DB_PATH: "/home/api/db/db.sqlite3"
1515
MCT_VERSION: {{ .Values.image.tag | default .Chart.AppVersion }}
1616
API_URL: http://localhost:{{ .Values.service.apiPort }}/api/
17+
REMOTE_MODEL_SERVICE_TYPE: "medcat" # TODO: Remove this for REMOTE_MODEL_SERVICE_TYPE after resolving pr-363 in cogstack-nlp
18+
19+
{{- if .Values.tracing.otlp.enabled }}
20+
# Tracing configuration
21+
MCT_ENABLE_TRACING: {{ .Values.tracing.otlp.enabled | quote }}
22+
OTEL_TRACES_EXPORTER: "otlp"
23+
OTEL_SERVICE_NAME: {{ .Values.tracing.serviceName | quote }}
24+
{{- if .Values.tracing.otlp.grpc.enabled }}
25+
OTEL_EXPORTER_OTLP_ENDPOINT: {{ .Values.tracing.otlp.grpc.endpoint | quote }}
26+
OTEL_EXPORTER_OTLP_PROTOCOL: "grpc"
27+
{{- else if .Values.tracing.otlp.http.enabled }}
28+
OTEL_EXPORTER_OTLP_ENDPOINT: {{ .Values.tracing.otlp.http.endpoint | quote }}
29+
OTEL_EXPORTER_OTLP_PROTOCOL: "http/protobuf"
30+
{{- end }}
31+
OTEL_METRICS_EXPORTER: none
32+
OTEL_LOGS_EXPORTER: none
33+
OTEL_PYTHON_DJANGO_EXCLUDED_URLS: /api/health/*,/metrics
34+
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: {{ .Values.tracing.experimentalResourceDetectors | quote }}
35+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: {{ .Values.tracing.disabledInstrumentations | quote }}
36+
37+
{{- end }}
38+
1739
{{- if and .Values.provisioning .Values.provisioning.enabled }}
1840
LOAD_EXAMPLES: "1"
1941
PROVISIONING_CONFIG_PATH: "/home/configs/provisioning.yaml"

deployment/kubernetes/charts/medcat-trainer-helm/values.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ env:
3939
DB_ENGINE: "postgresql"
4040
DB_PORT: "5432"
4141

42+
# Allow setting env values from field/configmap/secret references. Defaults to include k8s details for observability.
43+
envValueFrom:
44+
K8S_NODE_NAME:
45+
fieldRef:
46+
fieldPath: spec.nodeName
47+
K8S_POD_NAME:
48+
fieldRef:
49+
fieldPath: metadata.name
50+
K8S_POD_UID:
51+
fieldRef:
52+
fieldPath: metadata.uid
53+
K8S_POD_NAMESPACE:
54+
fieldRef:
55+
fieldPath: metadata.namespace
56+
4257
provisioning:
4358
# -- Set to true to enable provisioning of projects and models on startup..
4459
enabled: false
@@ -61,6 +76,35 @@ provisioning:
6176
description: "Example project for medcat trainer"
6277
annotationGuidelineLink: "https://docs.google.com/document/d/1xxelBOYbyVzJ7vLlztP2q1Kw9F5Vr1pRwblgrXPS7QM/edit?usp=sharing"
6378

79+
# Enable tracing with opentelemetry for the medcat-trainer backend
80+
tracing:
81+
# The name of the service in the tracing system
82+
serviceName: "medcat-trainer"
83+
# Resource attributes to add to the traces. Can be templated
84+
resourceAttributes:
85+
k8s.pod.uid: "$(K8S_POD_UID)"
86+
k8s.pod.name: "$(K8S_POD_NAME)"
87+
k8s.namespace.name: "$(K8S_POD_NAMESPACE)"
88+
k8s.node.name: "$(K8S_NODE_NAME)"
89+
service.version: "{{ .Values.image.tag | default .Chart.AppVersion }}"
90+
service.instance.id: "$(K8S_POD_NAMESPACE)-$(K8S_POD_NAME)-$(K8S_CONTAINER_NAME)"
91+
experimentalResourceDetectors: "containerid,os"
92+
# Optionally disable the db instrumentations due to noise.
93+
disabledInstrumentations: "psycopg,sqlite3"
94+
otlp:
95+
# Set to true to enable tracing
96+
enabled: false
97+
grpc:
98+
# Set to true to enable grpc tracing
99+
enabled: false
100+
# The endpoint to send the traces to
101+
endpoint: "http://<unused>:4317"
102+
http:
103+
# Set to true to enable http tracing over http/protobug
104+
enabled: false
105+
# The endpoint to send the traces to
106+
endpoint: "http://<unused>:4318"
107+
64108
postgresql:
65109
enabled: true
66110
# TODO: Support custom DB overrides

0 commit comments

Comments
 (0)