_config.tpl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. {{/*
  2. Default memory limiter configuration for OpenTelemetry Collector based on k8s resource limits.
  3. */}}
  4. {{- define "opentelemetry-collector.memoryLimiter" -}}
  5. # check_interval is the time between measurements of memory usage.
  6. check_interval: 5s
  7. # By default limit_mib is set to 80% of ".Values.resources.limits.memory"
  8. limit_percentage: 80
  9. # By default spike_limit_mib is set to 25% of ".Values.resources.limits.memory"
  10. spike_limit_percentage: 25
  11. {{- end }}
  12. {{/*
  13. Merge user supplied config into memory limiter config.
  14. */}}
  15. {{- define "opentelemetry-collector.baseConfig" -}}
  16. {{- $processorsConfig := get .Values.config "processors" }}
  17. {{- if not $processorsConfig.memory_limiter }}
  18. {{- $_ := set $processorsConfig "memory_limiter" (include "opentelemetry-collector.memoryLimiter" . | fromYaml) }}
  19. {{- end }}
  20. {{- .Values.config | toYaml }}
  21. {{- end }}
  22. {{/*
  23. Build config file for daemonset OpenTelemetry Collector
  24. */}}
  25. {{- define "opentelemetry-collector.daemonsetConfig" -}}
  26. {{- $values := deepCopy .Values }}
  27. {{- $data := dict "Values" $values | mustMergeOverwrite (deepCopy .) }}
  28. {{- $config := include "opentelemetry-collector.baseConfig" $data | fromYaml }}
  29. {{- if eq (include "opentelemetry-collector.logsCollectionEnabled" .) "true" }}
  30. {{- $config = (include "opentelemetry-collector.applyLogsCollectionConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  31. {{- end }}
  32. {{- if .Values.presets.hostMetrics.enabled }}
  33. {{- $config = (include "opentelemetry-collector.applyHostMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  34. {{- end }}
  35. {{- if .Values.presets.kubeletMetrics.enabled }}
  36. {{- $config = (include "opentelemetry-collector.applyKubeletMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  37. {{- end }}
  38. {{- if .Values.presets.kubernetesAttributes.enabled }}
  39. {{- $config = (include "opentelemetry-collector.applyKubernetesAttributesConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  40. {{- end }}
  41. {{- if .Values.presets.clusterMetrics.enabled }}
  42. {{- $config = (include "opentelemetry-collector.applyClusterMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  43. {{- end }}
  44. {{- tpl (toYaml $config) . }}
  45. {{- end }}
  46. {{/*
  47. Build config file for deployment OpenTelemetry Collector
  48. */}}
  49. {{- define "opentelemetry-collector.deploymentConfig" -}}
  50. {{- $values := deepCopy .Values }}
  51. {{- $data := dict "Values" $values | mustMergeOverwrite (deepCopy .) }}
  52. {{- $config := include "opentelemetry-collector.baseConfig" $data | fromYaml }}
  53. {{- if eq (include "opentelemetry-collector.logsCollectionEnabled" .) "true" }}
  54. {{- $config = (include "opentelemetry-collector.applyLogsCollectionConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  55. {{- end }}
  56. {{- if .Values.presets.hostMetrics.enabled }}
  57. {{- $config = (include "opentelemetry-collector.applyHostMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  58. {{- end }}
  59. {{- if .Values.presets.kubeletMetrics.enabled }}
  60. {{- $config = (include "opentelemetry-collector.applyKubeletMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  61. {{- end }}
  62. {{- if .Values.presets.kubernetesAttributes.enabled }}
  63. {{- $config = (include "opentelemetry-collector.applyKubernetesAttributesConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  64. {{- end }}
  65. {{- if .Values.presets.clusterMetrics.enabled }}
  66. {{- $config = (include "opentelemetry-collector.applyClusterMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  67. {{- end }}
  68. {{- tpl (toYaml $config) . }}
  69. {{- end }}
  70. {{- define "opentelemetry-collector.applyHostMetricsConfig" -}}
  71. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.hostMetricsConfig" .Values | fromYaml) .config }}
  72. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "hostmetrics" | uniq) }}
  73. {{- $config | toYaml }}
  74. {{- end }}
  75. {{- define "opentelemetry-collector.hostMetricsConfig" -}}
  76. receivers:
  77. hostmetrics:
  78. root_path: /hostfs
  79. collection_interval: 10s
  80. scrapers:
  81. cpu:
  82. load:
  83. memory:
  84. disk:
  85. filesystem:
  86. exclude_mount_points:
  87. mount_points:
  88. - /dev/*
  89. - /proc/*
  90. - /sys/*
  91. - /run/k3s/containerd/*
  92. - /var/lib/docker/*
  93. - /var/lib/kubelet/*
  94. - /snap/*
  95. match_type: regexp
  96. exclude_fs_types:
  97. fs_types:
  98. - autofs
  99. - binfmt_misc
  100. - bpf
  101. - cgroup2
  102. - configfs
  103. - debugfs
  104. - devpts
  105. - devtmpfs
  106. - fusectl
  107. - hugetlbfs
  108. - iso9660
  109. - mqueue
  110. - nsfs
  111. - overlay
  112. - proc
  113. - procfs
  114. - pstore
  115. - rpc_pipefs
  116. - securityfs
  117. - selinuxfs
  118. - squashfs
  119. - sysfs
  120. - tracefs
  121. match_type: strict
  122. network:
  123. {{- end }}
  124. {{- define "opentelemetry-collector.applyClusterMetricsConfig" -}}
  125. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.clusterMetricsConfig" .Values | fromYaml) .config }}
  126. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "k8s_cluster" | uniq) }}
  127. {{- $config | toYaml }}
  128. {{- end }}
  129. {{- define "opentelemetry-collector.clusterMetricsConfig" -}}
  130. receivers:
  131. k8s_cluster:
  132. collection_interval: 10s
  133. {{- end }}
  134. {{- define "opentelemetry-collector.applyKubeletMetricsConfig" -}}
  135. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.kubeletMetricsConfig" .Values | fromYaml) .config }}
  136. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "kubeletstats" | uniq) }}
  137. {{- $config | toYaml }}
  138. {{- end }}
  139. {{- define "opentelemetry-collector.kubeletMetricsConfig" -}}
  140. receivers:
  141. kubeletstats:
  142. collection_interval: 20s
  143. auth_type: "serviceAccount"
  144. endpoint: "${K8S_NODE_NAME}:10250"
  145. {{- end }}
  146. {{- define "opentelemetry-collector.applyLogsCollectionConfig" -}}
  147. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.logsCollectionConfig" .Values | fromYaml) .config }}
  148. {{- $_ := set $config.service.pipelines.logs "receivers" (append $config.service.pipelines.logs.receivers "filelog" | uniq) }}
  149. {{- if .Values.Values.presets.logsCollection.storeCheckpoints}}
  150. {{- $_ := set $config.service "extensions" (append $config.service.extensions "file_storage" | uniq) }}
  151. {{- end }}
  152. {{- $config | toYaml }}
  153. {{- end }}
  154. {{- define "opentelemetry-collector.logsCollectionConfig" -}}
  155. {{- if .Values.presets.logsCollection.storeCheckpoints }}
  156. extensions:
  157. file_storage:
  158. directory: /var/lib/otelcol
  159. {{- end }}
  160. receivers:
  161. filelog:
  162. include: [ /var/log/pods/*/*/*.log ]
  163. {{- if .Values.presets.logsCollection.includeCollectorLogs }}
  164. exclude: []
  165. {{- else }}
  166. # Exclude collector container's logs. The file format is /var/log/pods/<namespace_name>_<pod_name>_<pod_uid>/<container_name>/<run_id>.log
  167. exclude: [ /var/log/pods/{{ .Release.Namespace }}_{{ include "opentelemetry-collector.lowercase_chartname" . }}*_*/{{ include "opentelemetry-collector.name" . }}/*.log ]
  168. {{- end }}
  169. start_at: end
  170. {{- if .Values.presets.logsCollection.storeCheckpoints}}
  171. storage: file_storage
  172. {{- end }}
  173. include_file_path: true
  174. include_file_name: false
  175. operators:
  176. # Find out which format is used by kubernetes
  177. - type: router
  178. id: get-format
  179. routes:
  180. - output: parser-docker
  181. expr: 'body matches "^\\{"'
  182. - output: parser-crio
  183. expr: 'body matches "^[^ Z]+ "'
  184. - output: parser-containerd
  185. expr: 'body matches "^[^ Z]+Z"'
  186. # Parse CRI-O format
  187. - type: regex_parser
  188. id: parser-crio
  189. regex: '^(?P<time>[^ Z]+) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
  190. output: extract_metadata_from_filepath
  191. timestamp:
  192. parse_from: attributes.time
  193. layout_type: gotime
  194. layout: '2006-01-02T15:04:05.000000000-07:00'
  195. # Parse CRI-Containerd format
  196. - type: regex_parser
  197. id: parser-containerd
  198. regex: '^(?P<time>[^ ^Z]+Z) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
  199. output: extract_metadata_from_filepath
  200. timestamp:
  201. parse_from: attributes.time
  202. layout: '%Y-%m-%dT%H:%M:%S.%LZ'
  203. # Parse Docker format
  204. - type: json_parser
  205. id: parser-docker
  206. output: extract_metadata_from_filepath
  207. timestamp:
  208. parse_from: attributes.time
  209. layout: '%Y-%m-%dT%H:%M:%S.%LZ'
  210. # Extract metadata from file path
  211. - type: regex_parser
  212. id: extract_metadata_from_filepath
  213. regex: '^.*\/(?P<namespace>[^_]+)_(?P<pod_name>[^_]+)_(?P<uid>[a-f0-9\-]+)\/(?P<container_name>[^\._]+)\/(?P<restart_count>\d+)\.log$'
  214. parse_from: attributes["log.file.path"]
  215. # Rename attributes
  216. - type: move
  217. from: attributes.stream
  218. to: attributes["log.iostream"]
  219. - type: move
  220. from: attributes.container_name
  221. to: resource["k8s.container.name"]
  222. - type: move
  223. from: attributes.namespace
  224. to: resource["k8s.namespace.name"]
  225. - type: move
  226. from: attributes.pod_name
  227. to: resource["k8s.pod.name"]
  228. - type: move
  229. from: attributes.restart_count
  230. to: resource["k8s.container.restart_count"]
  231. - type: move
  232. from: attributes.uid
  233. to: resource["k8s.pod.uid"]
  234. # Clean up log body
  235. - type: move
  236. from: attributes.log
  237. to: body
  238. {{- end }}
  239. {{- define "opentelemetry-collector.applyKubernetesAttributesConfig" -}}
  240. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.kubernetesAttributesConfig" .Values | fromYaml) .config }}
  241. {{- if $config.service.pipelines.logs }}
  242. {{- $_ := set $config.service.pipelines.logs "processors" (prepend $config.service.pipelines.logs.processors "k8sattributes" | uniq) }}
  243. {{- end }}
  244. {{- if $config.service.pipelines.metrics }}
  245. {{- $_ := set $config.service.pipelines.metrics "processors" (prepend $config.service.pipelines.metrics.processors "k8sattributes" | uniq) }}
  246. {{- end }}
  247. {{- if $config.service.pipelines.traces }}
  248. {{- $_ := set $config.service.pipelines.traces "processors" (prepend $config.service.pipelines.traces.processors "k8sattributes" | uniq) }}
  249. {{- end }}
  250. {{- $config | toYaml }}
  251. {{- end }}
  252. {{- define "opentelemetry-collector.kubernetesAttributesConfig" -}}
  253. processors:
  254. k8sattributes:
  255. passthrough: false
  256. pod_association:
  257. - sources:
  258. - from: resource_attribute
  259. name: k8s.pod.ip
  260. - sources:
  261. - from: resource_attribute
  262. name: k8s.pod.uid
  263. - sources:
  264. - from: connection
  265. extract:
  266. metadata:
  267. - "k8s.namespace.name"
  268. - "k8s.deployment.name"
  269. - "k8s.statefulset.name"
  270. - "k8s.daemonset.name"
  271. - "k8s.cronjob.name"
  272. - "k8s.job.name"
  273. - "k8s.node.name"
  274. - "k8s.pod.name"
  275. - "k8s.pod.uid"
  276. - "k8s.pod.start_time"
  277. {{- end }}
  278. {{/* Build the list of port for deployment service */}}
  279. {{- define "opentelemetry-collector.deploymentPortsConfig" -}}
  280. {{- $ports := deepCopy .Values.ports }}
  281. {{- range $key, $port := $ports }}
  282. {{- if $port.enabled }}
  283. - name: {{ $key }}
  284. port: {{ $port.servicePort }}
  285. targetPort: {{ $port.containerPort }}
  286. protocol: {{ $port.protocol }}
  287. {{- if $port.appProtocol }}
  288. appProtocol: {{ $port.appProtocol }}
  289. {{- end }}
  290. {{- if $port.nodePort }}
  291. nodePort: {{ $port.nodePort }}
  292. {{- end }}
  293. {{- end }}
  294. {{- end }}
  295. {{- end }}