-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature/monitor #1792
Feature/monitor #1792
Changes from all commits
ffdd639
e13ef90
1be4bf1
87f590d
d4427a7
93f8c82
c89c587
4870e09
5a9bebc
f7277a5
69c5fbf
19968ff
772888b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2021 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"bytes" | ||
"time" | ||
|
||
"github.com/loadimpact/k6/stats" | ||
dto "github.com/prometheus/client_model/go" | ||
"github.com/prometheus/common/expfmt" | ||
) | ||
|
||
func newMetricFamily(m *stats.Metric, t time.Duration) []dto.MetricFamily { | ||
metrics := make([]dto.MetricFamily, 0) | ||
|
||
switch m.Type { | ||
case stats.Counter: | ||
counter := m.Sink.(*stats.CounterSink) | ||
metrics = append(metrics, newCounter(m.Name+"_count", counter.Value, m.Name+" cumulative value")) | ||
metrics = append(metrics, newGauge(m.Name+"_rate", counter.Value/(float64(t)/float64(time.Second)), | ||
m.Name+" value per seconds")) | ||
case stats.Gauge: | ||
gauge := m.Sink.(*stats.GaugeSink) | ||
metrics = append(metrics, newGauge(m.Name+"_value", gauge.Value, m.Name+" latest value")) | ||
case stats.Trend: | ||
trend := m.Sink.(*stats.TrendSink) | ||
trend.Calc() | ||
metrics = append(metrics, newGauge(m.Name+"_min", trend.Min, m.Name+" minimum value")) | ||
metrics = append(metrics, newGauge(m.Name+"_max", trend.Max, m.Name+" maximum value")) | ||
metrics = append(metrics, newGauge(m.Name+"_avg", trend.Avg, m.Name+" average value")) | ||
metrics = append(metrics, newGauge(m.Name+"_med", trend.Med, m.Name+" median value")) | ||
metrics = append(metrics, newGauge(m.Name+"_p90", trend.P(0.90), m.Name+" 90 percentile value")) | ||
metrics = append(metrics, newGauge(m.Name+"_p95", trend.P(0.95), m.Name+" 95 percentile value")) | ||
case stats.Rate: | ||
rate := m.Sink.(*stats.RateSink) | ||
metrics = append(metrics, newGauge(m.Name+"_rate", float64(rate.Trues)/float64(rate.Total), | ||
m.Name+" percentage of non-zero values")) | ||
} | ||
return metrics | ||
} | ||
|
||
func newGauge(name string, value float64, help string) dto.MetricFamily { | ||
return dto.MetricFamily{ | ||
Name: &name, | ||
Help: &help, | ||
Type: dto.MetricType_GAUGE.Enum(), | ||
Metric: []*dto.Metric{{Gauge: &dto.Gauge{Value: &value}}}, | ||
} | ||
} | ||
|
||
func newCounter(name string, value float64, help string) dto.MetricFamily { | ||
return dto.MetricFamily{ | ||
Name: &name, | ||
Help: &help, | ||
Type: dto.MetricType_COUNTER.Enum(), | ||
Metric: []*dto.Metric{{Counter: &dto.Counter{Value: &value}}}, | ||
} | ||
} | ||
|
||
func marshallMetricFamily(metrics []dto.MetricFamily) ([]byte, error) { | ||
var b bytes.Buffer | ||
for i := range metrics { | ||
_, err := expfmt.MetricFamilyToText(&b, &metrics[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return b.Bytes(), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2021 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/loadimpact/k6/api/common" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func handleGetMonitor(rw http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
engine := common.GetEngine(r.Context()) | ||
|
||
var t time.Duration | ||
if engine.ExecutionScheduler != nil { | ||
t = engine.ExecutionScheduler.GetState().GetCurrentTestRunDuration() | ||
} | ||
|
||
metrics := make([]dto.MetricFamily, 0) | ||
for _, m := range engine.Metrics { | ||
metrics = append(metrics, newMetricFamily(m, t)...) | ||
} | ||
|
||
data, err := marshallMetricFamily(metrics) | ||
Comment on lines
+42
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definetely racy ... there is a reason the Engine has a MetricsLock ;) You can run p.s. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, I just made it similar to metrics endpoint and didn't spent too much time on it... |
||
if err != nil { | ||
apiError(rw, "Encoding error", err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
rw.Header().Add("Content-Length", strconv.Itoa(len(data))) | ||
_, _ = rw.Write(data) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2021 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/guregu/null.v3" | ||
|
||
"github.com/loadimpact/k6/core" | ||
"github.com/loadimpact/k6/core/local" | ||
"github.com/loadimpact/k6/lib" | ||
"github.com/loadimpact/k6/lib/testutils" | ||
"github.com/loadimpact/k6/lib/testutils/minirunner" | ||
"github.com/loadimpact/k6/stats" | ||
|
||
"github.com/prometheus/common/expfmt" | ||
) | ||
|
||
func TestGetMonitor(t *testing.T) { | ||
logger := logrus.New() | ||
logger.SetOutput(testutils.NewTestOutput(t)) | ||
execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{}, logger) | ||
require.NoError(t, err) | ||
engine, err := core.NewEngine(execScheduler, lib.Options{}, logger) | ||
require.NoError(t, err) | ||
|
||
engine.Metrics = map[string]*stats.Metric{ | ||
"my_trend": stats.New("my_trend", stats.Trend, stats.Time), | ||
} | ||
engine.Metrics["my_trend"].Tainted = null.BoolFrom(true) | ||
|
||
rw := httptest.NewRecorder() | ||
NewHandler().ServeHTTP(rw, newRequestWithEngine(engine, "GET", "/v1/monitor", nil)) | ||
res := rw.Result() | ||
|
||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
assert.Equal(t, "text/plain; charset=utf-8", res.Header.Get("Content-Type")) | ||
assert.NotEmpty(t, res.Header.Get("Content-Length")) | ||
|
||
t.Run("metrics", func(t *testing.T) { | ||
parser := expfmt.TextParser{} | ||
metrics, err := parser.TextToMetricFamilies(rw.Body) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, metrics) | ||
assert.Len(t, metrics, 6) | ||
suffixes := []string{"_min", "_max", "_avg", "_med", "_p90", "_p95"} | ||
for _, suffix := range suffixes { | ||
name := "my_trend" + suffix | ||
assert.Equal(t, name, metrics[name].GetName()) | ||
} | ||
}) | ||
|
||
assert.NoError(t, res.Body.Close()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* | ||
* k6 - a next-generation load testing tool | ||
* Copyright (C) 2021 Load Impact | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/loadimpact/k6/stats" | ||
) | ||
|
||
func TestNewMetricFamilyTrend(t *testing.T) { | ||
trend := stats.New("name", stats.Trend, stats.Time) | ||
sink := trend.Sink.(*stats.TrendSink) | ||
sink.Min = 1 | ||
sink.Max = 10 | ||
sink.Avg = 5 | ||
sink.Med = 4 | ||
|
||
m := newMetricFamily(trend, 0) | ||
assert.Len(t, m, 6) | ||
|
||
assert.Equal(t, "name_min", *m[0].Name) | ||
assert.Equal(t, float64(1), *m[0].GetMetric()[0].Gauge.Value) | ||
|
||
assert.Equal(t, "name_max", *m[1].Name) | ||
assert.Equal(t, float64(10), *m[1].GetMetric()[0].Gauge.Value) | ||
|
||
assert.Equal(t, "name_avg", *m[2].Name) | ||
assert.Equal(t, float64(5), *m[2].GetMetric()[0].Gauge.Value) | ||
|
||
assert.Equal(t, "name_med", *m[3].Name) | ||
assert.Equal(t, float64(4), *m[3].GetMetric()[0].Gauge.Value) | ||
|
||
assert.Equal(t, "name_p90", *m[4].Name) | ||
assert.Equal(t, "name_p95", *m[5].Name) | ||
} | ||
|
||
func TestNewMetricFamilyCounter(t *testing.T) { | ||
counter := stats.New("name", stats.Counter, stats.Time) | ||
sink := counter.Sink.(*stats.CounterSink) | ||
sink.Value = 42 | ||
|
||
m := newMetricFamily(counter, 0) | ||
assert.Len(t, m, 2) | ||
|
||
assert.Equal(t, "name_count", *m[0].Name) | ||
assert.Equal(t, float64(42), *m[0].GetMetric()[0].Counter.Value) | ||
|
||
assert.Equal(t, "name_rate", *m[1].Name) | ||
} | ||
|
||
func TestNewMetricFamilyGauge(t *testing.T) { | ||
gauge := stats.New("name", stats.Gauge, stats.Time) | ||
sink := gauge.Sink.(*stats.GaugeSink) | ||
sink.Value = 42 | ||
|
||
m := newMetricFamily(gauge, 0) | ||
assert.Len(t, m, 1) | ||
|
||
assert.Equal(t, "name_value", *m[0].Name) | ||
assert.Equal(t, float64(42), *m[0].GetMetric()[0].Gauge.Value) | ||
} | ||
|
||
func TestNewMetricFamilyRate(t *testing.T) { | ||
rate := stats.New("name", stats.Rate, stats.Time) | ||
sink := rate.Sink.(*stats.RateSink) | ||
sink.Total = 42 | ||
sink.Trues = 42 * 42 | ||
|
||
m := newMetricFamily(rate, 0) | ||
assert.Len(t, m, 1) | ||
|
||
assert.Equal(t, "name_rate", *m[0].Name) | ||
assert.Equal(t, float64(42), *m[0].GetMetric()[0].Gauge.Value) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we should just make everything into a GAUGE ;).
I am not familiar with the prometheus API and conventions, but if this is the way things need to be done ... I don't think it will be all that useful ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably counter fits better