Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
5h4d0w4rt committed Dec 18, 2020
1 parent b8b444d commit 62681d5
Show file tree
Hide file tree
Showing 20 changed files with 486 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel*
Empty file added BUILD
Empty file.
9 changes: 9 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
workspace(name = "io_bazel_rules_prometheus")

load(":deps.bzl", "prometheus_repositories")

prometheus_repositories()

register_toolchains(
"//prometheus/toolchain:all",
)
15 changes: 15 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

PROMETHEUS_VERSION = "2.23.0"
PROMETHEUS_DARWIN_ARCH = "darwin-amd64"
PROMETHEUS_DARWIN = "{PROMETHEUS_VERSION}.{PROMETHEUS_DARWIN_ARCH}"
PROMETHEUS_DARWIN_URL = "https://github.com/prometheus/prometheus/releases/download/v{PROMETHEUS_VERSION}/prometheus-{PROMETHEUS_VERSION}.{PROMETHEUS_DARWIN_ARCH}.tar.gz"

def prometheus_repositories():
http_archive(
name = "prometheus_darwin",
sha256 = "d589a45495cea1aa74bff82335d2145f2d93b8b357c3398739b9139c74dc0cfe",
urls = [PROMETHEUS_DARWIN_URL],
strip_prefix = "prometheus-%s.%s" % (PROMETHEUS_VERSION, PROMETHEUS_DARWIN_ARCH),
build_file = "@//:prometheus.BUILD",
)
59 changes: 59 additions & 0 deletions examples/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
load("//prometheus:prometheus.bzl", "promtool_config_test", "promtool_unit_test")

promtool_unit_test(
name = "test_rules_yml",
srcs = [
"tests.yml",
],
rules = ["rules.yml"],
)

promtool_unit_test(
name = "test_rules_json",
srcs = [
"tests.json",
],
rules = ["rules.json"],
)

promtool_unit_test(
name = "test_rules_multiple",
srcs = [
"tests.json",
"tests.yml",
],
rules = [
"rules.json",
"rules.yml",
],
)

promtool_config_test(
name = "test_config_yml",
srcs = ["prometheus.yml"],
)

promtool_config_test(
name = "test_config_json",
srcs = ["prometheus.json"],
)

promtool_config_test(
name = "test_config_multiple",
srcs = [
"prometheus.json",
"prometheus.yml",
],
)

test_suite(
name = "all_tests",
tests = [
":test_config_json",
":test_config_multiple",
":test_config_yml",
":test_rules_json",
":test_rules_multiple",
":test_rules_yml",
],
)
28 changes: 28 additions & 0 deletions examples/prometheus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"global": {
"scrape_interval": "15s",
"evaluation_interval": "15s"
},
"alerting": {
"alertmanagers": [
{
"static_configs": [
{
"targets": null
}
]
}
]
},
"rule_files": null,
"scrape_configs": [
{
"job_name": "prometheus",
"static_configs": [
{
"targets": ["localhost:9090"]
}
]
}
]
}
29 changes: 29 additions & 0 deletions examples/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ["localhost:9090"]
33 changes: 33 additions & 0 deletions examples/rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"groups": [
{
"name": "example",
"rules": [
{
"alert": "InstanceDown",
"expr": "up == 0",
"for": "5m",
"labels": {
"severity": "page"
},
"annotations": {
"summary": "Instance {{ $labels.instance }} down",
"description": "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
}
},
{
"alert": "AnotherInstanceDown",
"expr": "up == 0",
"for": "10m",
"labels": {
"severity": "page"
},
"annotations": {
"summary": "Instance {{ $labels.instance }} down",
"description": "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
}
}
]
}
]
}
22 changes: 22 additions & 0 deletions examples/rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is the rules file.

groups:
- name: example
rules:
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

- alert: AnotherInstanceDown
expr: up == 0
for: 10m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
62 changes: 62 additions & 0 deletions examples/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"rule_files": ["rules.json"],
"evaluation_interval": "1m",
"tests": [
{
"interval": "1m",
"input_series": [
{
"series": "up{job=\"prometheus\", instance=\"localhost:9090\"}",
"values": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
},
{
"series": "up{job=\"node_exporter\", instance=\"localhost:9100\"}",
"values": "1+0x6 0 0 0 0 0 0 0 0"
},
{
"series": "go_goroutines{job=\"prometheus\", instance=\"localhost:9090\"}",
"values": "10+10x2 30+20x5"
},
{
"series": "go_goroutines{job=\"node_exporter\", instance=\"localhost:9100\"}",
"values": "10+10x7 10+30x4"
}
],
"alert_rule_test": [
{
"eval_time": "10m",
"alertname": "InstanceDown",
"exp_alerts": [
{
"exp_labels": {
"severity": "page",
"instance": "localhost:9090",
"job": "prometheus"
},
"exp_annotations": {
"summary": "Instance localhost:9090 down",
"description": "localhost:9090 of job prometheus has been down for more than 5 minutes."
}
}
]
}
],
"promql_expr_test": [
{
"expr": "go_goroutines > 5",
"eval_time": "4m",
"exp_samples": [
{
"labels": "go_goroutines{job=\"prometheus\",instance=\"localhost:9090\"}",
"value": 50
},
{
"labels": "go_goroutines{job=\"node_exporter\",instance=\"localhost:9100\"}",
"value": 50
}
]
}
]
}
]
}
48 changes: 48 additions & 0 deletions examples/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This is the main input for unit testing.
# Only this file is passed as command line argument.

rule_files:
- rules.yml

evaluation_interval: 1m

tests:
# Test 1.
- interval: 1m
# Series data.
input_series:
- series: 'up{job="prometheus", instance="localhost:9090"}'
values: "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
- series: 'up{job="node_exporter", instance="localhost:9100"}'
values: "1+0x6 0 0 0 0 0 0 0 0" # 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
- series: 'go_goroutines{job="prometheus", instance="localhost:9090"}'
values: "10+10x2 30+20x5" # 10 20 30 30 50 70 90 110 130
- series: 'go_goroutines{job="node_exporter", instance="localhost:9100"}'
values: "10+10x7 10+30x4" # 10 20 30 40 50 60 70 80 10 40 70 100 130

# Unit test for alerting rules.
alert_rule_test:
# Unit test 1.
- eval_time: 10m
alertname: InstanceDown
exp_alerts:
# Alert 1.
- exp_labels:
severity: page
instance: localhost:9090
job: prometheus
exp_annotations:
summary: "Instance localhost:9090 down"
description: "localhost:9090 of job prometheus has been down for more than 5 minutes."
# Unit tests for promql expressions.
promql_expr_test:
# Unit test 1.
- expr: go_goroutines > 5
eval_time: 4m
exp_samples:
# Sample 1.
- labels: 'go_goroutines{job="prometheus",instance="localhost:9090"}'
value: 50
# Sample 2.
- labels: 'go_goroutines{job="node_exporter",instance="localhost:9100"}'
value: 50
4 changes: 4 additions & 0 deletions prometheus.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports_files([
"prometheus",
"promtool",
])
1 change: 1 addition & 0 deletions prometheus/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package(default_visibility = ["//visibility:public"])
12 changes: 12 additions & 0 deletions prometheus/internal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(
default_visibility = ["//prometheus:__subpackages__"],
)

exports_files(
glob([
"*.bat",
"*.sh",
"*.sh.tpl",
]),
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 62681d5

Please # to comment.