Skip to content

VM Backend for prom proxy #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ARG ARCH="amd64"
ARG ARCH="arm64"
ARG OS="linux"
FROM quay.io/prometheus/busybox-${OS}-${ARCH}:latest
LABEL maintainer="The Prometheus Authors <[email protected]>"

ARG ARCH="amd64"
ARG ARCH="arm64"
ARG OS="linux"
COPY .build/${OS}-${ARCH}/prometheus /bin/prometheus
COPY .build/${OS}-${ARCH}/promtool /bin/promtool
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# limitations under the License.

# Needs to be defined before including Makefile.common to auto-generate targets
DOCKER_ARCHS ?= amd64 armv7 arm64 ppc64le s390x
#DOCKER_ARCHS ?= amd64 armv7 arm64 ppc64le s390x
DOCKER_ARCHS ?= arm64

UI_PATH = web/ui
UI_NODE_MODULES_PATH = $(UI_PATH)/node_modules
Expand Down
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ DOCKERFILE_PATH ?= ./Dockerfile
DOCKERBUILD_CONTEXT ?= ./
DOCKER_REPO ?= prom

DOCKER_ARCHS ?= amd64
DOCKER_ARCHS ?= arm64

BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
Expand Down
48 changes: 45 additions & 3 deletions rules/alerting.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Prometheus Authors

Check failure on line 1 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/prometheus/prometheus/rules [github.com/prometheus/prometheus/rules.test]

Check failure on line 1 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/prometheus/prometheus/rules [github.com/prometheus/prometheus/rules.test]
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -20,13 +20,14 @@
"strings"
"sync"
"time"

"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
"go.uber.org/atomic"
"gopkg.in/yaml.v2"

apic "github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/prometheus/prometheus/model/timestamp"
Expand Down Expand Up @@ -342,12 +343,53 @@
// is kept in memory state and consequently repeatedly sent to the AlertManager.
const resolvedRetention = 15 * time.Minute

func modelToPromql(mv model.Vector) promql.Vector {
var vec promql.Vector
var lbss labels.Labels
for _, m := range mv {

lbs := labels.NewBuilder(lbss)
for n, v := range m.Metric {
lbs.Set(string(n), string(v))
}
vec = append(vec, promql.Sample{Metric: lbs.Labels(), T: int64(m.Timestamp), F: float64(m.V

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or ) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' before newline in argument list (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / Go tests on Windows

syntax error: unexpected newline in argument list; possibly missing comma or )

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or ) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing ',' before newline in argument list (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected newline in argument list; possibly missing comma or )) (typecheck)

Check failure on line 355 in rules/alerting.go

View workflow job for this annotation

GitHub Actions / Go tests on Windows

syntax error: unexpected newline in argument list; possibly missing comma or )
alue)})
}
return vec
}

func vmQueryAlerting(ctx context.Context, rule *AlertingRule, ts time.Time) (promql.Vector, error) {
vmURL, ok := os.LookupEnv("VMSELECT_URL")
if !ok {
level.Warn(rule.logger).Log("FATAL: evironment variable VMSELECT_URL doesn't exists")
os.Exit(-1)
}
client, err := apic.NewClient(apic.Config{Address: vmURL})
if err != nil {
return nil, err
}

//stime := time.Now()
queryAPI := v1.NewAPI(client)
//fmt.Println("Evaluating:", time.Since(stime), rule.vector.String())
value, _, err := queryAPI.Query(context.Background(), rule.vector.String(), time.Now())
if err != nil {
return nil, err
}

if _, ok := value.(model.Vector); !ok {
err := fmt.Errorf("returned value is not a vector")
return nil, err
}
return modelToPromql(value.(model.Vector)), nil
}

// Eval evaluates the rule expression and then creates pending alerts and fires
// or removes previously pending alerts accordingly.
func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc, externalURL *url.URL, limit int) (promql.Vector, error) {
ctx = NewOriginContext(ctx, NewRuleDetail(r))

res, err := query(ctx, r.vector.String(), ts)
res, err := vmQueryAlerting(ctx, r, ts)
if err != nil {
return nil, err
}
Expand Down
35 changes: 31 additions & 4 deletions rules/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"fmt"
"net/url"
"time"

"os"
"go.uber.org/atomic"
"gopkg.in/yaml.v2"

"github.com/prometheus/common/model"
apic "github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/prometheus/prometheus/promql"
Expand Down Expand Up @@ -76,11 +78,36 @@ func (rule *RecordingRule) Labels() labels.Labels {
return rule.labels
}

func vmQueryRecording(ctx context.Context, rule *RecordingRule, ts time.Time) (promql.Vector, error) {
vmURL, ok := os.LookupEnv("VMSELECT_URL")
if !ok {
fmt.Println("FATAL: please specify VMSELECT_URL to run rules against to")
os.Exit(-1)
}
client, err := apic.NewClient(apic.Config{Address: vmURL})
if err != nil {
return nil, err
}

//stime := time.Now()
queryAPI := v1.NewAPI(client)
//fmt.Println("Evaluating:", time.Since(stime), rule.vector.String())
value, _, err := queryAPI.Query(context.Background(), rule.vector.String(), time.Now())
if err != nil {
return nil, err
}

if _, ok := value.(model.Vector); !ok {
err := fmt.Errorf("returned value is not a vector")
return nil, err
}
return modelToPromql(value.(model.Vector)), nil
}

// Eval evaluates the rule and then overrides the metric names and labels accordingly.
func (rule *RecordingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc, _ *url.URL, limit int) (promql.Vector, error) {
ctx = NewOriginContext(ctx, NewRuleDetail(rule))

vector, err := query(ctx, rule.vector.String(), ts)
vector, err := vmQueryRecording(ctx, rule, ts)
if err != nil {
return nil, err
}
Expand Down
Loading