Skip to content

Commit c0551eb

Browse files
authored
GT-127 Add support for Metrics. Deprecate statistics (#440)
1 parent e2fe7cb commit c0551eb

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Add tests to check support for Enterprise Graphs
1818
- Search View v2 (`search-alias`)
1919
- Add Rename View support
20+
- Add support for `Metrics`
2021

2122
## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27)
2223
- Fix `lastValue` field type

client_server_admin.go

+20
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ type ClientServerAdmin interface {
3838
// Shutdown a specific server, optionally removing it from its cluster.
3939
Shutdown(ctx context.Context, removeFromCluster bool) error
4040

41+
// Metrics returns the metrics of the server in Prometheus format.
42+
// List of metrics: https://www.arangodb.com/docs/devel/http/administration-and-monitoring-metrics.html
43+
// You can parse it using Prometheus client:
44+
/*
45+
var parser expfmt.TextParser
46+
metricsProm, err := parser.TextToMetricFamilies(strings.NewReader(string(metrics)))
47+
*/
48+
Metrics(ctx context.Context) ([]byte, error)
49+
50+
// MetricsForSingleServer returns the metrics of the specific server in Prometheus format.
51+
// This parameter 'serverID' is only meaningful on Coordinators.
52+
// List of metrics: https://www.arangodb.com/docs/devel/http/administration-and-monitoring-metrics.html
53+
// You can parse it using Prometheus client:
54+
/*
55+
var parser expfmt.TextParser
56+
metricsProm, err := parser.TextToMetricFamilies(strings.NewReader(string(metrics)))
57+
*/
58+
MetricsForSingleServer(ctx context.Context, serverID string) ([]byte, error)
59+
60+
// Deprecated: Use Metrics instead.
4161
// Statistics queries statistics from a specific server
4262
Statistics(ctx context.Context) (ServerStatistics, error)
4363

client_server_admin_impl.go

+35
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ func (c *client) Shutdown(ctx context.Context, removeFromCluster bool) error {
142142
return nil
143143
}
144144

145+
// Metrics returns the metrics of the server in Prometheus format.
146+
func (c *client) Metrics(ctx context.Context) ([]byte, error) {
147+
return c.getMetrics(ctx, "")
148+
}
149+
150+
// MetricsForSingleServer returns the metrics of the specific server in Prometheus format.
151+
// This parameter 'serverID' is only meaningful on Coordinators.
152+
func (c *client) MetricsForSingleServer(ctx context.Context, serverID string) ([]byte, error) {
153+
return c.getMetrics(ctx, serverID)
154+
}
155+
156+
// Metrics returns the metrics of the server in Prometheus format.
157+
func (c *client) getMetrics(ctx context.Context, serverID string) ([]byte, error) {
158+
var rawResponse []byte
159+
ctx = WithRawResponse(ctx, &rawResponse)
160+
161+
req, err := c.conn.NewRequest("GET", "_admin/metrics/v2")
162+
if err != nil {
163+
return rawResponse, WithStack(err)
164+
}
165+
166+
if serverID != "" {
167+
req.SetQuery("serverId", serverID)
168+
}
169+
170+
resp, err := c.conn.Do(ctx, req)
171+
if err != nil {
172+
return rawResponse, WithStack(err)
173+
}
174+
if err := resp.CheckStatus(200); err != nil {
175+
return rawResponse, WithStack(err)
176+
}
177+
return rawResponse, nil
178+
}
179+
145180
// Statistics queries statistics from a specific server.
146181
func (c *client) Statistics(ctx context.Context) (ServerStatistics, error) {
147182
req, err := c.conn.NewRequest("GET", "_admin/statistics")

http/connection.go

+4
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ func (c *httpConnection) Do(ctx context.Context, req driver.Request) (driver.Res
270270
return nil, driver.WithStack(err)
271271
}
272272
var rawResponse *[]byte
273+
useRawResponse := false
273274
if ctx != nil {
274275
if v := ctx.Value(keyRawResponse); v != nil {
276+
useRawResponse = true
275277
if buf, ok := v.(*[]byte); ok {
276278
rawResponse = buf
277279
}
@@ -310,6 +312,8 @@ func (c *httpConnection) Do(ctx context.Context, req driver.Request) (driver.Res
310312
*rawResponse = body
311313
}
312314
httpResp = &httpJSONResponse{resp: resp, rawResponse: body}
315+
} else if useRawResponse {
316+
httpResp = &httpJSONResponse{resp: resp, rawResponse: body}
313317
} else {
314318
return nil, driver.WithStack(fmt.Errorf("Unsupported content type '%s' with status %d and content '%s'", ct, resp.StatusCode, string(body)))
315319
}

test/server_metrics_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
20+
package test
21+
22+
import (
23+
"context"
24+
"testing"
25+
26+
"github.com/arangodb/go-driver"
27+
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
// TestGetServerMetrics tests if Client.Metrics works at all
32+
func TestGetServerMetrics(t *testing.T) {
33+
c := createClientFromEnv(t, true)
34+
ctx := context.Background()
35+
skipBelowVersion(c, "3.8", t)
36+
37+
metrics, err := c.Metrics(ctx)
38+
require.NoError(t, err)
39+
require.Contains(t, string(metrics), "arangodb_client_connection_statistics_total_time")
40+
}
41+
42+
// TestGetServerMetricsForSingleServer tests if Client.MetricsForSingleServer works at all
43+
func TestGetServerMetricsForSingleServer(t *testing.T) {
44+
c := createClientFromEnv(t, true)
45+
ctx := context.Background()
46+
skipBelowVersion(c, "3.8", t)
47+
skipNoCluster(c, t)
48+
49+
cl, err := c.Cluster(ctx)
50+
require.NoError(t, err)
51+
52+
h, err := cl.Health(ctx)
53+
require.NoError(t, err)
54+
55+
for id, sh := range h.Health {
56+
if sh.Role == driver.ServerRoleDBServer || sh.Role == driver.ServerRoleCoordinator {
57+
metrics, err := c.MetricsForSingleServer(ctx, string(id))
58+
require.NoError(t, err)
59+
require.Contains(t, string(metrics), "arangodb_client_connection_statistics_total_time")
60+
require.Contains(t, string(metrics), sh.ShortName)
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)