Created
January 24, 2026 01:05
-
-
Save lelandbatey/9ebbc80846c26929c4f14410054afaf4 to your computer and use it in GitHub Desktop.
Datadog prometheus instance creation in jsonnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run this with: | |
| // jsonnet ./dd_prom_instance.jsonnet | |
| local app = { name: 'examplesvc' }; | |
| local baseAnnotations = { | |
| deployment+: { | |
| spec: { | |
| template: { | |
| spec: { | |
| containers_:: { | |
| default: { | |
| ports_:: { | |
| 'http-prom': { | |
| containerPort: 1337, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }, | |
| annotations+: { | |
| trueVals(obj):: { | |
| [f]: obj[f] | |
| for f in std.objectFields(obj) | |
| if obj[f] == true | |
| }, | |
| DatadogPrometheusInstance( | |
| port, | |
| allowlisted_metrics, | |
| blocklisted_metrics, | |
| non_namespaced_metrics, | |
| ):: ( | |
| local alm = self.trueVals(allowlisted_metrics); | |
| local blm = self.trueVals(blocklisted_metrics); | |
| local nnm = self.trueVals(non_namespaced_metrics); | |
| [ | |
| { | |
| prometheus_url: 'http://%%host%%:' + port + '/metrics', | |
| namespace: app.name, | |
| metrics: ['*'], | |
| send_distribution_buckets: true, | |
| } + if blocklisted_metrics != {} || non_namespaced_metrics != {} then { | |
| // have to exclude non_namespaced_metrics because they'll be allowlisted in the second | |
| // prometheus instance which is not namespaced | |
| exclude_metrics: [k for k in std.objectFields(blocklisted_metrics)] + [k for k in std.objectFields(non_namespaced_metrics)], | |
| } else {} + if allowlisted_metrics != {} then { | |
| metrics : [k for k in std.objectFields(allowlisted_metrics)], | |
| //allowlisted_metrics: allowlisted_metrics, | |
| //metrics : [], | |
| } else {}, | |
| ] + if non_namespaced_metrics == {} then [] else [ | |
| // this is the same host as before, but this time with no namespacing and allowlisted metrics | |
| { | |
| prometheus_url: 'http://%%host%%:' + port + '/metrics', | |
| namespace: '', | |
| metrics: [k for k in std.objectFields(non_namespaced_metrics)], | |
| //exclude_metrics: null, | |
| send_distribution_buckets: true, | |
| }, | |
| ] | |
| ), | |
| datadog_prom_conf_+:: { | |
| main_pod: { | |
| port: $.deployment.spec.template.spec.containers_.default.ports_['http-prom'].containerPort, | |
| allowlisted_metrics: {}, | |
| blocklisted_metrics: {}, | |
| non_namespaced_metrics: {}, | |
| }, | |
| }, | |
| datadog_prom_instances_:: [], | |
| dd_instances_:: | |
| if self.datadog_prom_instances_ != [] then | |
| self.datadog_prom_instances_ | |
| else std.flattenArrays( | |
| [ | |
| self.DatadogPrometheusInstance( | |
| port=self.datadog_prom_conf_[pod_name].port, | |
| allowlisted_metrics=self.datadog_prom_conf_[pod_name].allowlisted_metrics, | |
| blocklisted_metrics=self.datadog_prom_conf_[pod_name].blocklisted_metrics, | |
| non_namespaced_metrics=self.datadog_prom_conf_[pod_name].non_namespaced_metrics, | |
| ) | |
| for pod_name in std.objectFields(self.datadog_prom_conf_) | |
| ] | |
| ), | |
| ['ad.datadoghq.com/' + app.name + '.instances']: std.manifestJsonEx(self.dd_instances_, ' '), | |
| ['ad.datadoghq.com/' + app.name + '.init_configs']: std.manifestJsonMinified([{} for x in self.dd_instances_]), | |
| ['ad.datadoghq.com/' + app.name + '.check_names']: std.manifestJsonMinified(['openmetrics' for x in self.dd_instances_]), | |
| }, | |
| }; | |
| local simpleOverride = { | |
| annotations+: { | |
| datadog_prom_instances_:: ['hello wooooooooooooooooooooooorld'], | |
| }, | |
| }; | |
| local licheeOriginal = { | |
| annotations+: { | |
| datadog_prom_instances_:: [ | |
| { | |
| prometheus_url: 'http://%%host%%:' + | |
| $.deployment.spec.template.spec.containers_.default.ports_['http-prom'].containerPort + | |
| '/metrics', | |
| namespace: app.name, | |
| metrics: ['*'], | |
| send_distribution_buckets: true, | |
| }, | |
| ], | |
| }, | |
| }; | |
| local customStateGuardOverrides = { | |
| annotations+: { | |
| datadog_prom_conf_+: { | |
| main_pod+: { | |
| non_namespaced_metrics+: { | |
| stateguard_grpc_request_count: true, | |
| stateguard_http_request_count: true, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }; | |
| local stateGuardOverrides = { | |
| annotations+: { | |
| // Override the datadog_prom_instances to add a separate scraper for stateguard metrics | |
| datadog_prom_instances_:: [ | |
| { | |
| // Regular namespaced metrics (exclude stateguard) | |
| prometheus_url: 'http://%%host%%:' + | |
| $.deployment.spec.template.spec.containers_.default.ports_['http-prom'].containerPort + | |
| '/metrics', | |
| namespace: app.name, | |
| metrics: ['*'], | |
| exclude_metrics: ['stateguard_grpc_request_count', 'stateguard_http_request_count'], | |
| send_distribution_buckets: true, | |
| }, | |
| { | |
| // Stateguard metrics without namespace prefix | |
| prometheus_url: 'http://%%host%%:' + | |
| $.deployment.spec.template.spec.containers_.default.ports_['http-prom'].containerPort + | |
| '/metrics', | |
| namespace: '', | |
| metrics: ['stateguard_grpc_request_count', 'stateguard_http_request_count'], | |
| send_distribution_buckets: true, | |
| }, | |
| ], | |
| }, | |
| }; | |
| local blockListOnly = { | |
| annotations+: { | |
| datadog_prom_conf_+: { | |
| main_pod+: { | |
| blocklisted_metrics+: { | |
| clerk_producer_event_count: true, | |
| graphql_request_duration: true, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }; | |
| local allowListOnly = { | |
| annotations+: { | |
| datadog_prom_conf_+: { | |
| main_pod+: { | |
| allowlisted_metrics+: { | |
| clerk_producer_event_count: true, | |
| graphql_request_duration: true, | |
| }, | |
| }, | |
| }, | |
| }, | |
| }; | |
| [ | |
| baseAnnotations, | |
| baseAnnotations + licheeOriginal, | |
| baseAnnotations + stateGuardOverrides, | |
| baseAnnotations + customStateGuardOverrides, | |
| baseAnnotations + blockListOnly, | |
| baseAnnotations + allowListOnly, | |
| ] | |
| /* | |
| Prints the following: | |
| [ | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"metrics\": [\n \"*\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"metrics\": [\n \"*\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\",\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{},{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"exclude_metrics\": [\n \"stateguard_grpc_request_count\",\n \"stateguard_http_request_count\"\n ],\n \"metrics\": [\n \"*\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n },\n {\n \"metrics\": [\n \"stateguard_grpc_request_count\",\n \"stateguard_http_request_count\"\n ],\n \"namespace\": \"\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\",\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{},{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"exclude_metrics\": [\n \"stateguard_grpc_request_count\",\n \"stateguard_http_request_count\"\n ],\n \"metrics\": [\n \"*\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n },\n {\n \"metrics\": [\n \"stateguard_grpc_request_count\",\n \"stateguard_http_request_count\"\n ],\n \"namespace\": \"\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"exclude_metrics\": [\n \"clerk_producer_event_count\",\n \"graphql_request_duration\"\n ],\n \"metrics\": [\n \"*\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "annotations": { | |
| "ad.datadoghq.com/examplesvc.check_names": "[\"openmetrics\"]", | |
| "ad.datadoghq.com/examplesvc.init_configs": "[{}]", | |
| "ad.datadoghq.com/examplesvc.instances": "[\n {\n \"metrics\": [\n \"clerk_producer_event_count\",\n \"graphql_request_duration\"\n ],\n \"namespace\": \"examplesvc\",\n \"prometheus_url\": \"http://%%host%%:1337/metrics\",\n \"send_distribution_buckets\": true\n }\n]" | |
| }, | |
| "deployment": { | |
| "spec": { | |
| "template": { | |
| "spec": { } | |
| } | |
| } | |
| } | |
| } | |
| ] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment