Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kurtosis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "github.com/kurtosis-tech/prometheus-package"
name: "github.com/barnabasbusa/prometheus-package"
Comment thread
barnabasbusa marked this conversation as resolved.
Outdated
description: |
# Prometheus
This is a Kurtosis package for launching a Prometheus server that scrapes metrics from services. See the `main.star#run` function for details on how to configure the server.
139 changes: 76 additions & 63 deletions main.star
Original file line number Diff line number Diff line change
Expand Up @@ -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",
storage_tsdb_retention_time="1d",
storage_tsdb_retention_size="512MB",
min_cpu=10,
max_cpu=1000,
min_memory=128,
max_memory=2048,
node_selectors=None,
):
""" 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. <services ip address>:<exposed metrics port>
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,
Expand All @@ -67,53 +72,59 @@ 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:
if "Name" not in metrics_config:
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"]
Expand All @@ -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