mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-10 07:34:04 -08:00
d4374a9265
This depends on https://github.com/prometheus/client_golang/pull/51. For vectors, the result format looks like this: ```json { "version": 1, "type" : "vector", "value" : [ { "timestamp" : 1421765411.045, "value" : "65.475000", "metric" : { "quantile" : "0.5", "instance" : "http://localhost:9090/metrics", "job" : "prometheus", "__name__" : "http_request_duration_microseconds", "handler" : "/static/", "method" : "get", "code" : "304" } }, { "timestamp" : 1421765411.045, "value" : "5826.339000", "metric" : { "quantile" : "0.9", "instance" : "http://localhost:9090/metrics", "job" : "prometheus", "__name__" : "http_request_duration_microseconds", "handler" : "prometheus", "method" : "get", "code" : "200" } }, /* ... */ ] } ``` For matrices, it looks like this: ```json { "version": 1, "type" : "matrix", "value" : [ { "metric" : { "quantile" : "0.99", "instance" : "http://localhost:9090/metrics", "job" : "prometheus", "__name__" : "http_request_duration_microseconds", "handler" : "/static/", "method" : "get", "code" : "200" }, "values" : [ [ 1421765547.659, "29162.953000" ], [ 1421765548.659, "29162.953000" ], [ 1421765549.659, "29162.953000" ], /* ... */ ] } ] } ```
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
// Copyright 2013 The Prometheus Authors
|
|
// 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
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package notification
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
)
|
|
|
|
type testHTTPPoster struct {
|
|
message string
|
|
receivedPost chan<- bool
|
|
}
|
|
|
|
func (p *testHTTPPoster) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(body)
|
|
p.message = buf.String()
|
|
p.receivedPost <- true
|
|
return &http.Response{
|
|
Body: ioutil.NopCloser(&bytes.Buffer{}),
|
|
}, nil
|
|
}
|
|
|
|
type testNotificationScenario struct {
|
|
description string
|
|
summary string
|
|
message string
|
|
}
|
|
|
|
func (s *testNotificationScenario) test(i int, t *testing.T) {
|
|
h := NewNotificationHandler("alertmanager_url", 0)
|
|
defer h.Stop()
|
|
|
|
receivedPost := make(chan bool, 1)
|
|
poster := testHTTPPoster{receivedPost: receivedPost}
|
|
h.httpClient = &poster
|
|
|
|
go h.Run()
|
|
|
|
h.SubmitReqs(NotificationReqs{
|
|
{
|
|
Summary: s.summary,
|
|
Description: s.description,
|
|
Labels: clientmodel.LabelSet{
|
|
clientmodel.LabelName("instance"): clientmodel.LabelValue("testinstance"),
|
|
},
|
|
Value: clientmodel.SampleValue(1.0 / 3.0),
|
|
ActiveSince: time.Time{},
|
|
RuleString: "Test rule string",
|
|
GeneratorURL: "prometheus_url",
|
|
},
|
|
})
|
|
|
|
<-receivedPost
|
|
if poster.message != s.message {
|
|
t.Fatalf("%d. Expected '%s', received '%s'", i, s.message, poster.message)
|
|
}
|
|
}
|
|
|
|
func TestNotificationHandler(t *testing.T) {
|
|
scenarios := []testNotificationScenario{
|
|
{
|
|
// Correct message.
|
|
summary: "Summary",
|
|
description: "Description",
|
|
message: `[{"Description":"Description","Labels":{"instance":"testinstance"},"Payload":{"ActiveSince":"0001-01-01T00:00:00Z","AlertingRule":"Test rule string","GeneratorURL":"prometheus_url","Value":"0.3333333333333333"},"Summary":"Summary"}]`,
|
|
},
|
|
}
|
|
|
|
for i, s := range scenarios {
|
|
s.test(i, t)
|
|
}
|
|
}
|