_config.tpl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. {{- if .Values.useGOMEMLIMIT }}
  21. {{- if (((.Values.config).service).extensions) }}
  22. {{- $_ := set .Values.config.service "extensions" (without .Values.config.service.extensions "memory_ballast") }}
  23. {{- end}}
  24. {{- $_ := unset (.Values.config.extensions) "memory_ballast" }}
  25. {{- else }}
  26. {{- $memoryBallastConfig := get .Values.config.extensions "memory_ballast" }}
  27. {{- if or (not $memoryBallastConfig) (not $memoryBallastConfig.size_in_percentage) }}
  28. {{- $_ := set $memoryBallastConfig "size_in_percentage" 40 }}
  29. {{- end }}
  30. {{- end }}
  31. {{- .Values.config | toYaml }}
  32. {{- end }}
  33. {{/*
  34. Build config file for daemonset OpenTelemetry Collector
  35. */}}
  36. {{- define "opentelemetry-collector.daemonsetConfig" -}}
  37. {{- $values := deepCopy .Values }}
  38. {{- $data := dict "Values" $values | mustMergeOverwrite (deepCopy .) }}
  39. {{- $config := include "opentelemetry-collector.baseConfig" $data | fromYaml }}
  40. {{- if .Values.presets.logsCollection.enabled }}
  41. {{- $config = (include "opentelemetry-collector.applyLogsCollectionConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  42. {{- end }}
  43. {{- if .Values.presets.hostMetrics.enabled }}
  44. {{- $config = (include "opentelemetry-collector.applyHostMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  45. {{- end }}
  46. {{- if .Values.presets.kubeletMetrics.enabled }}
  47. {{- $config = (include "opentelemetry-collector.applyKubeletMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  48. {{- end }}
  49. {{- if .Values.presets.kubernetesAttributes.enabled }}
  50. {{- $config = (include "opentelemetry-collector.applyKubernetesAttributesConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  51. {{- end }}
  52. {{- if .Values.presets.clusterMetrics.enabled }}
  53. {{- $config = (include "opentelemetry-collector.applyClusterMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  54. {{- end }}
  55. {{- tpl (toYaml $config) . }}
  56. {{- end }}
  57. {{/*
  58. Build config file for deployment OpenTelemetry Collector
  59. */}}
  60. {{- define "opentelemetry-collector.deploymentConfig" -}}
  61. {{- $values := deepCopy .Values }}
  62. {{- $data := dict "Values" $values | mustMergeOverwrite (deepCopy .) }}
  63. {{- $config := include "opentelemetry-collector.baseConfig" $data | fromYaml }}
  64. {{- if .Values.presets.logsCollection.enabled }}
  65. {{- $config = (include "opentelemetry-collector.applyLogsCollectionConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  66. {{- end }}
  67. {{- if .Values.presets.hostMetrics.enabled }}
  68. {{- $config = (include "opentelemetry-collector.applyHostMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  69. {{- end }}
  70. {{- if .Values.presets.kubeletMetrics.enabled }}
  71. {{- $config = (include "opentelemetry-collector.applyKubeletMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  72. {{- end }}
  73. {{- if .Values.presets.kubernetesAttributes.enabled }}
  74. {{- $config = (include "opentelemetry-collector.applyKubernetesAttributesConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  75. {{- end }}
  76. {{- if .Values.presets.kubernetesEvents.enabled }}
  77. {{- $config = (include "opentelemetry-collector.applyKubernetesEventsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  78. {{- end }}
  79. {{- if .Values.presets.clusterMetrics.enabled }}
  80. {{- $config = (include "opentelemetry-collector.applyClusterMetricsConfig" (dict "Values" $data "config" $config) | fromYaml) }}
  81. {{- end }}
  82. {{- tpl (toYaml $config) . }}
  83. {{- end }}
  84. {{- define "opentelemetry-collector.applyHostMetricsConfig" -}}
  85. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.hostMetricsConfig" .Values | fromYaml) .config }}
  86. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "hostmetrics" | uniq) }}
  87. {{- $config | toYaml }}
  88. {{- end }}
  89. {{- define "opentelemetry-collector.hostMetricsConfig" -}}
  90. receivers:
  91. hostmetrics:
  92. root_path: /hostfs
  93. collection_interval: 10s
  94. scrapers:
  95. cpu:
  96. load:
  97. memory:
  98. disk:
  99. filesystem:
  100. exclude_mount_points:
  101. mount_points:
  102. - /dev/*
  103. - /proc/*
  104. - /sys/*
  105. - /run/k3s/containerd/*
  106. - /var/lib/docker/*
  107. - /var/lib/kubelet/*
  108. - /snap/*
  109. match_type: regexp
  110. exclude_fs_types:
  111. fs_types:
  112. - autofs
  113. - binfmt_misc
  114. - bpf
  115. - cgroup2
  116. - configfs
  117. - debugfs
  118. - devpts
  119. - devtmpfs
  120. - fusectl
  121. - hugetlbfs
  122. - iso9660
  123. - mqueue
  124. - nsfs
  125. - overlay
  126. - proc
  127. - procfs
  128. - pstore
  129. - rpc_pipefs
  130. - securityfs
  131. - selinuxfs
  132. - squashfs
  133. - sysfs
  134. - tracefs
  135. match_type: strict
  136. network:
  137. {{- end }}
  138. {{- define "opentelemetry-collector.applyClusterMetricsConfig" -}}
  139. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.clusterMetricsConfig" .Values | fromYaml) .config }}
  140. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "k8s_cluster" | uniq) }}
  141. {{- $config | toYaml }}
  142. {{- end }}
  143. {{- define "opentelemetry-collector.clusterMetricsConfig" -}}
  144. receivers:
  145. k8s_cluster:
  146. collection_interval: 10s
  147. {{- end }}
  148. {{- define "opentelemetry-collector.applyKubeletMetricsConfig" -}}
  149. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.kubeletMetricsConfig" .Values | fromYaml) .config }}
  150. {{- $_ := set $config.service.pipelines.metrics "receivers" (append $config.service.pipelines.metrics.receivers "kubeletstats" | uniq) }}
  151. {{- $config | toYaml }}
  152. {{- end }}
  153. {{- define "opentelemetry-collector.kubeletMetricsConfig" -}}
  154. receivers:
  155. kubeletstats:
  156. collection_interval: 20s
  157. auth_type: "serviceAccount"
  158. endpoint: "${env:K8S_NODE_NAME}:10250"
  159. {{- end }}
  160. {{- define "opentelemetry-collector.applyLogsCollectionConfig" -}}
  161. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.logsCollectionConfig" .Values | fromYaml) .config }}
  162. {{- $_ := set $config.service.pipelines.logs "receivers" (append $config.service.pipelines.logs.receivers "filelog" | uniq) }}
  163. {{- if .Values.Values.presets.logsCollection.storeCheckpoints}}
  164. {{- $_ := set $config.service "extensions" (append $config.service.extensions "file_storage" | uniq) }}
  165. {{- end }}
  166. {{- $config | toYaml }}
  167. {{- end }}
  168. {{- define "opentelemetry-collector.logsCollectionConfig" -}}
  169. {{- if .Values.presets.logsCollection.storeCheckpoints }}
  170. extensions:
  171. file_storage:
  172. directory: /var/lib/otelcol
  173. {{- end }}
  174. receivers:
  175. filelog:
  176. include: [ /var/log/pods/*/*/*.log ]
  177. {{- if .Values.presets.logsCollection.includeCollectorLogs }}
  178. exclude: []
  179. {{- else }}
  180. # Exclude collector container's logs. The file format is /var/log/pods/<namespace_name>_<pod_name>_<pod_uid>/<container_name>/<run_id>.log
  181. exclude: [ /var/log/pods/{{ include "opentelemetry-collector.namespace" . }}_{{ include "opentelemetry-collector.fullname" . }}*_*/{{ include "opentelemetry-collector.lowercase_chartname" . }}/*.log ]
  182. {{- end }}
  183. start_at: end
  184. retry_on_failure:
  185. enabled: true
  186. {{- if .Values.presets.logsCollection.storeCheckpoints}}
  187. storage: file_storage
  188. {{- end }}
  189. include_file_path: true
  190. include_file_name: false
  191. operators:
  192. # Find out which format is used by kubernetes
  193. - type: router
  194. id: get-format
  195. routes:
  196. - output: parser-docker
  197. expr: 'body matches "^\\{"'
  198. - output: parser-crio
  199. expr: 'body matches "^[^ Z]+ "'
  200. - output: parser-containerd
  201. expr: 'body matches "^[^ Z]+Z"'
  202. # Parse CRI-O format
  203. - type: regex_parser
  204. id: parser-crio
  205. regex: '^(?P<time>[^ Z]+) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
  206. timestamp:
  207. parse_from: attributes.time
  208. layout_type: gotime
  209. layout: '2006-01-02T15:04:05.999999999Z07:00'
  210. - type: recombine
  211. id: crio-recombine
  212. output: extract_metadata_from_filepath
  213. combine_field: attributes.log
  214. source_identifier: attributes["log.file.path"]
  215. is_last_entry: "attributes.logtag == 'F'"
  216. combine_with: ""
  217. max_log_size: {{ $.Values.presets.logsCollection.maxRecombineLogSize }}
  218. # Parse CRI-Containerd format
  219. - type: regex_parser
  220. id: parser-containerd
  221. regex: '^(?P<time>[^ ^Z]+Z) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
  222. timestamp:
  223. parse_from: attributes.time
  224. layout: '%Y-%m-%dT%H:%M:%S.%LZ'
  225. - type: recombine
  226. id: containerd-recombine
  227. output: extract_metadata_from_filepath
  228. combine_field: attributes.log
  229. source_identifier: attributes["log.file.path"]
  230. is_last_entry: "attributes.logtag == 'F'"
  231. combine_with: ""
  232. max_log_size: {{ $.Values.presets.logsCollection.maxRecombineLogSize }}
  233. # Parse Docker format
  234. - type: json_parser
  235. id: parser-docker
  236. output: extract_metadata_from_filepath
  237. timestamp:
  238. parse_from: attributes.time
  239. layout: '%Y-%m-%dT%H:%M:%S.%LZ'
  240. # Extract metadata from file path
  241. - type: regex_parser
  242. id: extract_metadata_from_filepath
  243. regex: '^.*\/(?P<namespace>[^_]+)_(?P<pod_name>[^_]+)_(?P<uid>[a-f0-9\-]+)\/(?P<container_name>[^\._]+)\/(?P<restart_count>\d+)\.log$'
  244. parse_from: attributes["log.file.path"]
  245. # Rename attributes
  246. - type: move
  247. from: attributes.stream
  248. to: attributes["log.iostream"]
  249. - type: move
  250. from: attributes.container_name
  251. to: resource["k8s.container.name"]
  252. - type: move
  253. from: attributes.namespace
  254. to: resource["k8s.namespace.name"]
  255. - type: move
  256. from: attributes.pod_name
  257. to: resource["k8s.pod.name"]
  258. - type: move
  259. from: attributes.restart_count
  260. to: resource["k8s.container.restart_count"]
  261. - type: move
  262. from: attributes.uid
  263. to: resource["k8s.pod.uid"]
  264. # Clean up log body
  265. - type: move
  266. from: attributes.log
  267. to: body
  268. {{- end }}
  269. {{- define "opentelemetry-collector.applyKubernetesAttributesConfig" -}}
  270. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.kubernetesAttributesConfig" .Values | fromYaml) .config }}
  271. {{- if and ($config.service.pipelines.logs) (not (has "k8sattributes" $config.service.pipelines.logs.processors)) }}
  272. {{- $_ := set $config.service.pipelines.logs "processors" (prepend $config.service.pipelines.logs.processors "k8sattributes" | uniq) }}
  273. {{- end }}
  274. {{- if and ($config.service.pipelines.metrics) (not (has "k8sattributes" $config.service.pipelines.metrics.processors)) }}
  275. {{- $_ := set $config.service.pipelines.metrics "processors" (prepend $config.service.pipelines.metrics.processors "k8sattributes" | uniq) }}
  276. {{- end }}
  277. {{- if and ($config.service.pipelines.traces) (not (has "k8sattributes" $config.service.pipelines.traces.processors)) }}
  278. {{- $_ := set $config.service.pipelines.traces "processors" (prepend $config.service.pipelines.traces.processors "k8sattributes" | uniq) }}
  279. {{- end }}
  280. {{- $config | toYaml }}
  281. {{- end }}
  282. {{- define "opentelemetry-collector.kubernetesAttributesConfig" -}}
  283. processors:
  284. k8sattributes:
  285. {{- if eq .Values.mode "daemonset" }}
  286. filter:
  287. node_from_env_var: K8S_NODE_NAME
  288. {{- end }}
  289. passthrough: false
  290. pod_association:
  291. - sources:
  292. - from: resource_attribute
  293. name: k8s.pod.ip
  294. - sources:
  295. - from: resource_attribute
  296. name: k8s.pod.uid
  297. - sources:
  298. - from: connection
  299. extract:
  300. metadata:
  301. - "k8s.namespace.name"
  302. - "k8s.deployment.name"
  303. - "k8s.statefulset.name"
  304. - "k8s.daemonset.name"
  305. - "k8s.cronjob.name"
  306. - "k8s.job.name"
  307. - "k8s.node.name"
  308. - "k8s.pod.name"
  309. - "k8s.pod.uid"
  310. - "k8s.pod.start_time"
  311. {{- if .Values.presets.kubernetesAttributes.extractAllPodLabels }}
  312. labels:
  313. - tag_name: $$1
  314. key_regex: (.*)
  315. from: pod
  316. {{- end }}
  317. {{- if .Values.presets.kubernetesAttributes.extractAllPodAnnotations }}
  318. annotations:
  319. - tag_name: $$1
  320. key_regex: (.*)
  321. from: pod
  322. {{- end }}
  323. {{- end }}
  324. {{/* Build the list of port for service */}}
  325. {{- define "opentelemetry-collector.servicePortsConfig" -}}
  326. {{- $ports := deepCopy .Values.ports }}
  327. {{- range $key, $port := $ports }}
  328. {{- if $port.enabled }}
  329. - name: {{ $key }}
  330. port: {{ $port.servicePort }}
  331. targetPort: {{ $port.containerPort }}
  332. protocol: {{ $port.protocol }}
  333. {{- if $port.appProtocol }}
  334. appProtocol: {{ $port.appProtocol }}
  335. {{- end }}
  336. {{- if $port.nodePort }}
  337. nodePort: {{ $port.nodePort }}
  338. {{- end }}
  339. {{- end }}
  340. {{- end }}
  341. {{- end }}
  342. {{/* Build the list of port for pod */}}
  343. {{- define "opentelemetry-collector.podPortsConfig" -}}
  344. {{- $ports := deepCopy .Values.ports }}
  345. {{- range $key, $port := $ports }}
  346. {{- if $port.enabled }}
  347. - name: {{ $key }}
  348. containerPort: {{ $port.containerPort }}
  349. protocol: {{ $port.protocol }}
  350. {{- if and $.isAgent $port.hostPort }}
  351. hostPort: {{ $port.hostPort }}
  352. {{- end }}
  353. {{- end }}
  354. {{- end }}
  355. {{- end }}
  356. {{- define "opentelemetry-collector.applyKubernetesEventsConfig" -}}
  357. {{- $config := mustMergeOverwrite (include "opentelemetry-collector.kubernetesEventsConfig" .Values | fromYaml) .config }}
  358. {{- $_ := set $config.service.pipelines.logs "receivers" (append $config.service.pipelines.logs.receivers "k8sobjects" | uniq) }}
  359. {{- $config | toYaml }}
  360. {{- end }}
  361. {{- define "opentelemetry-collector.kubernetesEventsConfig" -}}
  362. receivers:
  363. k8sobjects:
  364. objects:
  365. - name: events
  366. mode: "watch"
  367. group: "events.k8s.io"
  368. exclude_watch_type:
  369. - "DELETED"
  370. {{- end }}