diff --git a/charts/sourcegraph/CHANGELOG.md b/charts/sourcegraph/CHANGELOG.md index 999de7af..cc12daef 100644 --- a/charts/sourcegraph/CHANGELOG.md +++ b/charts/sourcegraph/CHANGELOG.md @@ -10,11 +10,13 @@ Use `**BREAKING**:` to denote a breaking change - Added `frontend.internalServiceAnnotations` and `frontend.internalServiceLabels` for the `sourcegraph-frontend-internal` Service. That Service no longer renders `frontend.serviceAnnotations` and `frontend.serviceLabels`, which now apply to the `sourcegraph-frontend` Service only, so load balancer annotations or a service mesh label set for the external Service no longer land on the internal one. If you relied on them on the internal Service, set the new values. - The `correct-data-dir-permissions` init containers of `pgsql`, `codeintel-db`, and `codeinsights-db` now run the database's own image instead of `sourcegraph/alpine-3.14`, which was based on an end-of-life Alpine release. The chart no longer pulls `alpine-3.14`, so private registries no longer need to mirror it. `alpine.image` has been removed and is ignored if set; `alpine.containerSecurityContext`, `alpine.resources`, and `codeInsightsDB.init.containerSecurityContext` still apply. +- Fixed the "pods available" panel and alert of the OpenTelemetry Collector dashboard, which query `up{app=~".*otel-collector"}`, having no data. The `otel-collector` Service was missing the `app` label that every other scraped Service has, so its metrics had no `app` and `job` labels. They now have `app="otel-collector"` and `job="otel-collector"`. The collector also served its own metrics on 127.0.0.1 only, unless `jaeger.enabled` was set, so Prometheus could not scrape them. The chart now always passes the container one argument, after `openTelemetry.gateway.args`, that serves them on all interfaces, port 8888. A command set in `openTelemetry.gateway.command` gets the argument too, so a shell wrapper must pass it on to the collector: as `"$0" "$@"` when the script is the first of `openTelemetry.gateway.args` after `command: ["/bin/sh", "-c"]`, or as `"$@"` when the script is in `command`, followed by a placeholder for `$0`. The argument replaces `service.telemetry.metrics.readers` of the configuration files the command loads. - Every object the chart creates now has the `deploy` and `app.kubernetes.io/component` labels. PodDisruptionBudgets get the component of their workload, PriorityClasses get `priority-class`, the StorageClass gets `storage` and keeps `deploy: sourcegraph-storage`, and the `sg-test-connection` Pod gets `test-connection`. `extraResources` get `deploy: sourcegraph` unless they set their own; set `app.kubernetes.io/component` on them yourself. The `jaeger` ConfigMap, Deployment, and Services gain `app.kubernetes.io/instance` and keep `app.kubernetes.io/name: jaeger`, which their selectors use. Labels on these objects can be changed in place; the `jaeger` pod restarts once, because its `checksum/jaeger-config` annotation covers the ConfigMap's labels. - `.labels` is now also rendered on the component's ConfigMaps, Secrets, and PodDisruptionBudget, not only on its workload, so releases that set it gain these labels on upgrade. Added `storageClass.labels` and a `labels` field on each `priorityClasses` entry. - Fixed `redisStore.serviceAccount.annotations` being ignored, so the `redis-store` ServiceAccount was created without them, e.g. without a workload identity annotation. They are now rendered, as on every other ServiceAccount. - The `prometheus` pod now restarts when an upgrade changes the chart's `prometheus.yml`, through a `checksum/prometheus-config` pod annotation. Prometheus does not reload a changed ConfigMap by itself, so such a change took effect only at the pod's next restart. The annotation is not set with `prometheus.existingConfig`. - Fixed a label key set both in `sourcegraph.labels` and in a component's `labels`, `podLabels`, `serviceLabels`, or other label value being rendered twice on the same object, which Kustomize and `kubeconform -strict` reject. Each key is now rendered once, with the value that Kubernetes already applied: the later one in the template, so the component's value, and the labels the chart sets itself, such as `deploy` and `app`, win. Objects keep the same labels. +- The `otel-collector` pod now restarts when `openTelemetry.gateway.config.traces` changes, through a `checksum/otel-collector-config` pod annotation. The collector does not reload a changed ConfigMap by itself, so a changed exporter or processor took effect only at the pod's next restart. The annotation is set only with `openTelemetry.gateway.config.traces.exporters`, which is when the chart renders that ConfigMap, so the default install is not affected. - `sourcegraph.labels` and the chart's standard labels (`helm.sh/chart`, `app.kubernetes.io/name`, `app.kubernetes.io/instance`, `app.kubernetes.io/version`, `app.kubernetes.io/managed-by`) are now rendered on every object the chart creates: also on pods, Services, ConfigMaps, Secrets, the Ingress, PodDisruptionBudgets, PriorityClasses, and `extraResources`, so existing releases gain these labels on upgrade. Pods do not get `helm.sh/chart` and `app.kubernetes.io/version`, which change with every chart version and would restart every pod on every upgrade, and they also gain `app.kubernetes.io/component`. The per-pod scratch PersistentVolumeClaim of `preciseCodeIntel` and `syntacticCodeIntel`, created when `storageType` is `pvc`, gets all of these labels, so with that setting those pods restart whenever the chart version changes. Labels that an `extraResources` entry, `serviceLabels`, or `podLabels` set take precedence, except that `podLabels` cannot change a selector label. No object is recreated. Every pod restarts once, on this upgrade. Prometheus copies the labels of a Service or pod to the metrics it scrapes from it, so the bundled Prometheus configuration now drops the five standard labels and the keys of `sourcegraph.labels`. No bundled dashboard or alert queries them. Metrics lose `app_kubernetes_io_managed_by`, which Helm added to every Service, `otel-collector` metrics lose the other standard labels, which its Service already had, and `cadvisor` and `jaeger` metrics, scraped from pods, lose `app_kubernetes_io_name` and `app_kubernetes_io_instance` and gain `app_kubernetes_io_component`. To add a label to the metrics of a Service, set it in `.serviceLabels`, with a key that is not in `sourcegraph.labels`. If you set `prometheus.existingConfig`, add the same `labeldrop` rule after each `__meta_kubernetes_service_label_(.+)` and `__meta_kubernetes_pod_label_(.+)` `labelmap` rule in your `prometheus.yml`, as in the [k3s example](examples/prometheus/prometheus-override-k3s.ConfigMap.yaml), and append the keys of `sourcegraph.labels` to its regex. - Added `storageLabels` on `blobstore`, `codeInsightsDB`, `codeIntelDB`, `pgsql`, `prometheus`, `redisCache`, and `redisStore` to label each component's PersistentVolumeClaim, and `.serviceAccount.labels` to label a component's ServiceAccount and, for `frontend` and `prometheus`, its Role, RoleBinding, ClusterRole, and ClusterRoleBinding. Also added `storageLabels` on `preciseCodeIntel` and `syntacticCodeIntel`, to label the per-pod `/tmp` scratch PersistentVolumeClaim when `storageType` is `pvc`; they default to empty, so no pod restarts. `sourcegraph.labels` and the chart's standard labels (`helm.sh/chart`, `app.kubernetes.io/name`, `app.kubernetes.io/instance`, `app.kubernetes.io/version`, `app.kubernetes.io/managed-by`) are now also rendered on these PersistentVolumeClaims, on every ServiceAccount, and on the `prometheus` RBAC objects, as they already are on every workload and on the `frontend` Role and RoleBinding, so existing releases gain these labels on upgrade. Labels on these objects can be changed in place, so none is recreated. A key set in `sourcegraph.labels` and in one of these values is rendered once, with the component's value, and the labels the chart sets itself win over both, so Kustomize and `kubeconform -strict` accept the output. See [Adopting existing workloads](README.md#adopting-existing-workloads). - Fixed the `jaeger` ServiceAccount failing to render with `jaeger.serviceAccount.create: true`. Its name and annotations were read from a `tracing` value that does not exist, and are now read from `jaeger.serviceAccount`, as the `jaeger` Deployment's `serviceAccountName` already was. diff --git a/charts/sourcegraph/README.md b/charts/sourcegraph/README.md index 3beb012a..1cf2d0f1 100644 --- a/charts/sourcegraph/README.md +++ b/charts/sourcegraph/README.md @@ -401,7 +401,7 @@ In addition to the documented values, all services also support the following va | openTelemetry.agent.serviceAccount.create | bool | `false` | Enable creation of ServiceAccount for `otel-agent` | | openTelemetry.agent.serviceAccount.name | string | `""` | Name of the ServiceAccount to be created or an existing ServiceAccount | | openTelemetry.enabled | bool | `true` | | -| openTelemetry.gateway.command | list | `[]` | Command for the `otel-collector` container, as a list, replacing the chart's `/bin/otelcol-sourcegraph --config=` command, including the `--config` flag the chart selects, e.g. a shell wrapper that exports an exporter API key read from a mounted file. `openTelemetry.gateway.args` is still passed as the arguments. | +| openTelemetry.gateway.command | list | `[]` | Command for the `otel-collector` container, as a list, replacing the chart's `/bin/otelcol-sourcegraph --config=` command, including the `--config` flag the chart selects, e.g. a shell wrapper that exports an exporter API key read from a mounted file. `openTelemetry.gateway.args` is still passed as the arguments. The chart always passes one last argument that makes the collector serve its own metrics on all interfaces, where Prometheus scrapes them, so a shell wrapper must pass it on: as `"$0" "$@"` when the script is the first of `openTelemetry.gateway.args` after `command: ["/bin/sh", "-c"]`, or as `"$@"` when the script is in `command`, followed by a placeholder for `$0`. | | openTelemetry.gateway.config.traces.exporters | object | `{}` | Define where traces should be exported to. Read how to configure different backends in the [OpenTelemetry documentation](https://opentelemetry.io/docs/collector/configuration/#exporters) | | openTelemetry.gateway.config.traces.exportersTlsSecretName | string | `""` | Define the name of a preexisting secret containing TLS certificates for exporters, which will be mounted under "/tls". Read more about TLS configuration of exporters in the [OpenTelemetry Collector documentation](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md) | | openTelemetry.gateway.config.traces.processors | object | `{}` | Define trace processors. Read how to configure sampling in the [OpenTelemetry documentation](https://docs.sourcegraph.com/admin/observability/opentelemetry#sampling-traces) | diff --git a/charts/sourcegraph/templates/_helpers/_otelCollector.tpl b/charts/sourcegraph/templates/_helpers/_otelCollector.tpl new file mode 100644 index 00000000..4bc828b6 --- /dev/null +++ b/charts/sourcegraph/templates/_helpers/_otelCollector.tpl @@ -0,0 +1,12 @@ +{{/* +The `service.telemetry.metrics.readers` setting of `otel-collector`, as a YAML +flow sequence. It serves the collector's own metrics on all interfaces, port +8888, where Prometheus scrapes them. The collector defaults to 127.0.0.1, which +other pods cannot reach. Without the type and unit suffixes the metric names are +the ones the bundled dashboard queries, e.g. `otelcol_receiver_accepted_spans`. + +The image's `jaeger.yaml` already has this setting. Its `logging.yaml` does not. +*/}} +{{- define "sourcegraph.otelCollector.metricsReaders" -}} +[{pull: {exporter: {prometheus: {host: "0.0.0.0", port: 8888, without_type_suffix: true, without_units: true}}}}] +{{- end }} diff --git a/charts/sourcegraph/templates/otel-collector/otel-collector.ConfigMap.yaml b/charts/sourcegraph/templates/otel-collector/otel-collector.ConfigMap.yaml index d85f1f53..02bdb4a9 100644 --- a/charts/sourcegraph/templates/otel-collector/otel-collector.ConfigMap.yaml +++ b/charts/sourcegraph/templates/otel-collector/otel-collector.ConfigMap.yaml @@ -34,6 +34,11 @@ data: endpoint: "localhost:55679" service: + telemetry: + metrics: + # Serve the collector's own metrics on all interfaces, for Prometheus to scrape. + # The collector defaults to 127.0.0.1. + readers: {{ include "sourcegraph.otelCollector.metricsReaders" . }} extensions: - health_check - zpages diff --git a/charts/sourcegraph/templates/otel-collector/otel-collector.Deployment.yaml b/charts/sourcegraph/templates/otel-collector/otel-collector.Deployment.yaml index 0805e604..675f6e9c 100644 --- a/charts/sourcegraph/templates/otel-collector/otel-collector.Deployment.yaml +++ b/charts/sourcegraph/templates/otel-collector/otel-collector.Deployment.yaml @@ -19,6 +19,10 @@ spec: metadata: annotations: kubectl.kubernetes.io/default-container: otel-collector + {{- if .Values.openTelemetry.gateway.config.traces.exporters }} + {{- /* The collector does not reload a changed ConfigMap by itself. Its data is rendered from these values only. */}} + checksum/otel-collector-config: {{ toJson .Values.openTelemetry.gateway.config.traces | sha256sum }} + {{- end }} {{- if .Values.sourcegraph.podAnnotations }} {{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }} {{- end }} @@ -48,11 +52,15 @@ spec: {{- else }} # use config defined by customer in values.yaml - "--config=/etc/otel-collector/config.yaml" {{- end }} - {{- end -}} - {{- with .Values.openTelemetry.gateway.args }} + {{- end }} args: - {{- toYaml . | nindent 8 }} - {{- end }} + {{- with .Values.openTelemetry.gateway.args }} + {{- toYaml . | nindent 10 }} + {{- end }} + # The image's logging.yaml serves the collector's own metrics on 127.0.0.1 only, where Prometheus cannot + # scrape them. An argument, so that a command set in `openTelemetry.gateway.command` gets it too, and the + # last one, so that it is `$0` of a `sh -c` command whose script is the first argument. + - {{ printf "--config=yaml:service::telemetry::metrics::readers: %s" (include "sourcegraph.otelCollector.metricsReaders" .) | quote }} terminationMessagePolicy: FallbackToLogsOnError env: {{- if .Values.jaeger.enabled }} diff --git a/charts/sourcegraph/templates/otel-collector/otel-collector.Service.yaml b/charts/sourcegraph/templates/otel-collector/otel-collector.Service.yaml index be064d0b..f171a091 100644 --- a/charts/sourcegraph/templates/otel-collector/otel-collector.Service.yaml +++ b/charts/sourcegraph/templates/otel-collector/otel-collector.Service.yaml @@ -9,7 +9,7 @@ metadata: {{- toYaml .Values.openTelemetry.gateway.serviceAnnotations | nindent 4 }} {{- end }} labels: - {{- include "sourcegraph.mergedLabels" (list . .Values.openTelemetry.gateway.serviceLabels (dict "deploy" "sourcegraph" "app.kubernetes.io/component" "otel-collector")) | nindent 4 }} + {{- include "sourcegraph.mergedLabels" (list . (dict "app" "otel-collector") .Values.openTelemetry.gateway.serviceLabels (dict "deploy" "sourcegraph" "app.kubernetes.io/component" "otel-collector")) | nindent 4 }} name: otel-collector spec: ports: diff --git a/charts/sourcegraph/tests/allObjectLabels_test.yaml b/charts/sourcegraph/tests/allObjectLabels_test.yaml index 2321a4ad..1c72351d 100644 --- a/charts/sourcegraph/tests/allObjectLabels_test.yaml +++ b/charts/sourcegraph/tests/allObjectLabels_test.yaml @@ -323,6 +323,15 @@ tests: example.com/project: extra example.com/release: sourcegraph deploy: sourcegraph + +# Prometheus derives the `app` and `job` labels of a Service's metrics from its +# `app` label, and the "pods available" alert queries `up{app=~".*otel-collector"}`. +- it: should label the otel-collector Service with its app, like every other scraped Service + template: otel-collector/otel-collector.Service.yaml + asserts: + - equal: + path: metadata.labels.app + value: otel-collector --- suite: sourcegraph.labels on the objects that replace others release: diff --git a/charts/sourcegraph/tests/command_test.yaml b/charts/sourcegraph/tests/command_test.yaml index 2446952a..0c084119 100644 --- a/charts/sourcegraph/tests/command_test.yaml +++ b/charts/sourcegraph/tests/command_test.yaml @@ -56,12 +56,12 @@ tests: path: spec.template.spec.initContainers[0].command value: ["sh", "-c", "/migrator up"] -- it: should replace the otel-collector command, and keep args, when gateway.command is set +- it: should replace the otel-collector command, and keep args before the chart's own argument, when gateway.command is set template: otel-collector/otel-collector.Deployment.yaml set: openTelemetry.gateway.command: ["/bin/sh", "-c"] openTelemetry.gateway.args: - - API_KEY=$(cat /secrets/api-key) /bin/otelcol-sourcegraph --config=/etc/otel-collector/config.yaml + - API_KEY=$(cat /secrets/api-key) /bin/otelcol-sourcegraph --config=/etc/otel-collector/config.yaml "$0" "$@" asserts: - equal: path: spec.template.spec.containers[0].command @@ -69,4 +69,5 @@ tests: - equal: path: spec.template.spec.containers[0].args value: - - API_KEY=$(cat /secrets/api-key) /bin/otelcol-sourcegraph --config=/etc/otel-collector/config.yaml + - API_KEY=$(cat /secrets/api-key) /bin/otelcol-sourcegraph --config=/etc/otel-collector/config.yaml "$0" "$@" + - '--config=yaml:service::telemetry::metrics::readers: [{pull: {exporter: {prometheus: {host: "0.0.0.0", port: 8888, without_type_suffix: true, without_units: true}}}}]' diff --git a/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml b/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml index a9ae2a3c..c573f8d0 100644 --- a/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml +++ b/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml @@ -7,10 +7,18 @@ tests: - equal: path: spec.template.spec.containers[0].command value: [ "/bin/otelcol-sourcegraph", "--config=/etc/otel-collector/configs/logging.yaml" ] + # The argument serves the collector's own metrics on all interfaces: logging.yaml + # leaves them on 127.0.0.1, where Prometheus cannot scrape them. + - equal: + path: spec.template.spec.containers[0].args + value: + - '--config=yaml:service::telemetry::metrics::readers: [{pull: {exporter: {prometheus: {host: "0.0.0.0", port: 8888, without_type_suffix: true, without_units: true}}}}]' - isEmpty: path: spec.template.spec.containers[0].volumeMounts - isEmpty: path: spec.template.spec.volumes + - notExists: # no ConfigMap, so nothing to restart for + path: spec.template.metadata.annotations["checksum/otel-collector-config"] - hasDocuments: count: 0 # ConfigMap is not rendered template: otel-collector/otel-collector.ConfigMap.yaml @@ -27,6 +35,8 @@ tests: path: spec.template.spec.containers[0].volumeMounts - isEmpty: path: spec.template.spec.volumes + - notExists: # no ConfigMap, so nothing to restart for + path: spec.template.metadata.annotations["checksum/otel-collector-config"] - hasDocuments: count: 0 # ConfigMap is not rendered template: otel-collector/otel-collector.ConfigMap.yaml @@ -61,6 +71,13 @@ tests: items: - key: config.yaml path: config.yaml + # The collector does not reload a changed ConfigMap, so the pod restarts when the values it is rendered from change. + - matchRegex: + path: spec.template.metadata.annotations["checksum/otel-collector-config"] + pattern: ^[0-9a-f]{64}$ + - notEqual: # the hash of `null`, which is what a missing value would hash to + path: spec.template.metadata.annotations["checksum/otel-collector-config"] + value: 74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b - hasDocuments: count: 1 # ConfigMap is rendered template: otel-collector/otel-collector.ConfigMap.yaml @@ -68,6 +85,11 @@ tests: path: data["config.yaml"] pattern: "endpoint: otlp.service.com:443" # check if element of config is substituted correctly template: otel-collector/otel-collector.ConfigMap.yaml + - matchRegex: + path: data["config.yaml"] + # the collector's own metrics are served on all interfaces, for Prometheus to scrape + pattern: 'service:\n telemetry:\n metrics:\n(\s*#.*\n)*\s*readers: \[\{pull: \{exporter: \{prometheus: \{host: "0\.0\.0\.0", port: 8888, ' + template: otel-collector/otel-collector.ConfigMap.yaml - it: should fail if a configuration is provided and jaeger is enabled template: otel-collector/otel-collector.ConfigMap.yaml set: diff --git a/charts/sourcegraph/values.yaml b/charts/sourcegraph/values.yaml index dfd5ae7d..a0cd7cc2 100644 --- a/charts/sourcegraph/values.yaml +++ b/charts/sourcegraph/values.yaml @@ -762,6 +762,10 @@ openTelemetry: # `/bin/otelcol-sourcegraph --config=` command, including the `--config` flag the chart # selects, e.g. a shell wrapper that exports an exporter API key read from a mounted file. # `openTelemetry.gateway.args` is still passed as the arguments. + # The chart always passes one last argument that makes the collector serve its own metrics on all + # interfaces, where Prometheus scrapes them, so a shell wrapper must pass it on: as `"$0" "$@"` when the + # script is the first of `openTelemetry.gateway.args` after `command: ["/bin/sh", "-c"]`, or as `"$@"` + # when the script is in `command`, followed by a placeholder for `$0`. command: [] # -- Resource requests & limits for the `otel-collector` container, # learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/)