diff --git a/main.star b/main.star index 86fe67f..f14d44a 100644 --- a/main.star +++ b/main.star @@ -2,63 +2,68 @@ CONFIG_DIR = "/config" CONFIG_FILENAME = "prometheus-config.yml" DEFAULT_SCRAPE_INTERVAL = "15s" -def run(plan, - metrics_jobs=[], - name="prometheus", - min_cpu=10, - max_cpu=1000, - min_memory=128, - max_memory=2048, - node_selectors=None, + +def run( + plan, + metrics_jobs=[], + name="prometheus", + min_cpu=10, + max_cpu=1000, + min_memory=128, + max_memory=2048, + node_selectors=None, + storage_tsdb_retention_time="1d", + storage_tsdb_retention_size="512MB", ): - """ Starts a Prometheus server that scrapes metrics off the provided prometheus metrics configurations. + """Starts a Prometheus server that scrapes metrics off the provided prometheus metrics configurations. Args: - metrics_jobs(json): A list of prometheus metrics configs to scrape metrics from. + metrics_jobs(json): A list of prometheus metrics configs to scrape metrics from. More info on scrape config here: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config eg. ``` metrics_jobs: [ { # name of metrics job - Name: "" , + Name: "" , # endpoint to scrape metrics from,eg. : - Endpoint: "", + Endpoint: "", # labels to associate with scraped metrics (eg. { "service_type": "api" } ) # optional - Labels:{}, + Labels:{}, # http path to scrape metrics from # optional - MetricsPath: "/metrics", + MetricsPath: "/metrics", # how frequently to scrape targets from this job # optional ScrapeInterval: "15s" }, - { + { ... }, ] ``` - min_cpu(int): min cpu for prometheus instance - max_cpu(int): max cpu for prometheus instance - min_memory(int): min memory for prometheus instance - max_memory(int): max memory for prometheus instance + name(string): name of the prometheus service + storage_tsdb_retention_time(string): retention time for prometheus instance (default: 1d) + storage_tsdb_retention_size(string): retention size for prometheus instance (default: 512MB) + min_cpu(int): min cpu for prometheus instance (default: 10 milicores) + max_cpu(int): max cpu for prometheus instance (default: 1000 milicores) + min_memory(int): min memory for prometheus instance (default: 128MB) + max_memory(int): max memory for prometheus instance (default: 2048MB) node_selectors (dict[string, string]): Define a dict of node selectors - only works in kubernetes example: {"kubernetes.io/hostname": node-name-01} Returns: prometheus_url: endpoint to prometheus service inside the enclave (eg. 123.123.212:9090) """ prometheus_config_template = read_file(src="./static-files/prometheus.yml.tmpl") - prometheus_config_data = { - "MetricsJobs": get_metrics_jobs(metrics_jobs) - } + prometheus_config_data = {"MetricsJobs": get_metrics_jobs(metrics_jobs)} prom_config_files_artifact = plan.render_templates( - config = { + config={ CONFIG_FILENAME: struct( template=prometheus_config_template, data=prometheus_config_data, @@ -67,45 +72,51 @@ def run(plan, name="prometheus-config", ) - config_file_path= CONFIG_DIR + "/" + CONFIG_FILENAME + config_file_path = CONFIG_DIR + "/" + CONFIG_FILENAME if node_selectors == None: node_selectors = {} - prometheus_service = plan.add_service(name=name, config=ServiceConfig( - image="prom/prometheus:latest", - ports={ - "http": PortSpec( - number=9090, - transport_protocol="TCP", - application_protocol="http", - ) - }, - files={ - CONFIG_DIR:prom_config_files_artifact, - }, - cmd=[ - "--config.file=" + config_file_path, - "--storage.tsdb.path=/prometheus", - "--storage.tsdb.retention.time=1d", - "--storage.tsdb.retention.size=512MB", - "--storage.tsdb.wal-compression", - "--web.console.libraries=/etc/prometheus/console_libraries", - "--web.console.templates=/etc/prometheus/consoles", - "--web.enable-lifecycle", - ], - min_cpu=min_cpu, - max_cpu=max_cpu, - min_memory=min_memory, - max_memory=max_memory, - node_selectors=node_selectors, - )) + prometheus_service = plan.add_service( + name=name, + config=ServiceConfig( + image="prom/prometheus:latest", + ports={ + "http": PortSpec( + number=9090, + transport_protocol="TCP", + application_protocol="http", + ) + }, + files={ + CONFIG_DIR: prom_config_files_artifact, + }, + cmd=[ + "--config.file=" + config_file_path, + "--storage.tsdb.path=/prometheus", + "--storage.tsdb.retention.time=" + str(storage_tsdb_retention_time), + "--storage.tsdb.retention.size=" + str(storage_tsdb_retention_size), + "--storage.tsdb.wal-compression", + "--web.console.libraries=/etc/prometheus/console_libraries", + "--web.console.templates=/etc/prometheus/consoles", + "--web.enable-lifecycle", + ], + min_cpu=min_cpu, + max_cpu=max_cpu, + min_memory=min_memory, + max_memory=max_memory, + node_selectors=node_selectors, + ), + ) prometheus_service_ip_address = prometheus_service.ip_address prometheus_service_http_port = prometheus_service.ports["http"].number - return "http://{0}:{1}".format(prometheus_service_ip_address, prometheus_service_http_port) - + return "http://{0}:{1}".format( + prometheus_service_ip_address, prometheus_service_http_port + ) + + def get_metrics_jobs(service_metrics_configs): metrics_jobs = [] for metrics_config in service_metrics_configs: @@ -113,7 +124,7 @@ def get_metrics_jobs(service_metrics_configs): fail("Name not provided in metrics config.") if "Endpoint" not in metrics_config: fail("Endpoint not provided in metrics config") - + labels = {} if "Labels" in metrics_config: labels = metrics_config["Labels"] @@ -126,12 +137,14 @@ def get_metrics_jobs(service_metrics_configs): if "ScrapeInterval" in metrics_config: scrape_interval = metrics_config["ScrapeInterval"] - metrics_jobs.append({ - "Name": metrics_config["Name"], - "Endpoint": metrics_config["Endpoint"], - "Labels": labels, - "MetricsPath": metrics_path, - "ScrapeInterval": scrape_interval, - }) - + metrics_jobs.append( + { + "Name": metrics_config["Name"], + "Endpoint": metrics_config["Endpoint"], + "Labels": labels, + "MetricsPath": metrics_path, + "ScrapeInterval": scrape_interval, + } + ) + return metrics_jobs