Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
consul
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
What's new
5
Snippets
Groups
Projects
gitlab-org
build
omnibus-mirror
consul
Commits
7d8b9135
Unverified
Commit
7d8b9135
authored
3 years ago
by
FFMMM
Browse files
Options
Downloads
Patches
Plain Diff
test client does not emit autopilot metric
Signed-off-by:
FFMMM
<
FFMMM@users.noreply.github.com
>
parent
9f13c6ad
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
agent/metrics_test.go
+75
-29
75 additions, 29 deletions
agent/metrics_test.go
with
75 additions
and
29 deletions
agent/metrics_test.go
+
75
−
29
View file @
7d8b9135
package
agent
import
(
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus adds testing around
// the published autopilot metrics on https://www.consul.io/docs/agent/telemetry#autopilot
func
TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus
(
t
*
testing
.
T
)
{
checkForShortTesting
(
t
)
// This test cannot use t.Parallel() since we modify global state, ie the global metrics instance
// don't bootstrap agent so as not to
// become a leader
hcl
:=
`
telemetry = {
prometheus_retention_time = "5s",
disable_hostname = true
func
checkForShortTesting
(
t
*
testing
.
T
)
{
if
testing
.
Short
()
{
t
.
Skip
(
"too slow for testing.Short"
)
}
bootstrap = false
`
a
:=
StartTestAgent
(
t
,
TestAgent
{
HCL
:
hcl
})
defer
a
.
Shutdown
()
}
func
recordPromMetrics
(
t
*
testing
.
T
,
a
*
TestAgent
,
respRec
*
httptest
.
ResponseRecorder
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/v1/agent/metrics?format=prometheus"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to generate new http request.
err: %v"
,
err
)
require
.
NoError
(
t
,
err
,
"Failed to generate new http request.
"
)
}
respRec
:=
httptest
.
NewRecorder
()
_
,
err
=
a
.
srv
.
AgentMetrics
(
respRec
,
req
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to serve agent metrics. err: %v"
,
err
)
require
.
NoError
(
t
,
err
,
"Failed to serve agent metrics"
)
}
}
func
assertMetricExistsWithValue
(
t
*
testing
.
T
,
respRec
*
httptest
.
ResponseRecorder
,
metric
string
,
value
string
)
{
if
respRec
.
Body
.
String
()
==
""
{
t
.
Fatalf
(
"Response body is empty."
)
}
// eg "consul_autopilot_healthy NaN"
target
:=
metric
+
" "
+
value
if
!
strings
.
Contains
(
respRec
.
Body
.
String
(),
target
)
{
t
.
Fatalf
(
"Could not find the metric
\"
%s
\"
with value
\"
%s
\"
in the /v1/agent/metrics response"
,
metric
,
value
)
}
}
func
assertMetricNotExists
(
t
*
testing
.
T
,
respRec
*
httptest
.
ResponseRecorder
,
metric
string
)
{
if
respRec
.
Body
.
String
()
==
""
{
t
.
Fatalf
(
"Response body is empty."
)
}
if
strings
.
Contains
(
respRec
.
Body
.
String
(),
metric
)
{
t
.
Fatalf
(
"Didn't expect to find the metric
\"
%s
\"
in the /v1/agent/metrics response"
,
metric
)
}
}
// TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus adds testing around
// the published autopilot metrics on https://www.consul.io/docs/agent/telemetry#autopilot
func
TestHTTPHandlers_AgentMetrics_ConsulAutopilotHealthy_Prometheus
(
t
*
testing
.
T
)
{
checkForShortTesting
(
t
)
// This test cannot use t.Parallel() since we modify global state, ie the global metrics instance
t
.
Run
(
"Check consul_autopilot_healthy metric value on startup"
,
func
(
t
*
testing
.
T
)
{
target
:=
"consul_autopilot_healthy NaN"
keyValue
:=
strings
.
Split
(
target
,
" "
)
if
!
strings
.
Contains
(
respRec
.
Body
.
String
(),
target
)
{
t
.
Fatalf
(
"Could not find the metric
\"
%s
\"
with value
\"
%s
\"
in the /v1/agent/metrics response"
,
keyValue
[
0
],
keyValue
[
1
])
// don't bootstrap agent so as not to
// become a leader
hcl
:=
`
telemetry = {
prometheus_retention_time = "5s",
disable_hostname = true
}
bootstrap = false
`
a
:=
StartTestAgent
(
t
,
TestAgent
{
HCL
:
hcl
})
defer
a
.
Shutdown
()
respRec
:=
httptest
.
NewRecorder
()
recordPromMetrics
(
t
,
a
,
respRec
)
assertMetricExistsWithValue
(
t
,
respRec
,
"consul_autopilot_healthy"
,
"NaN"
)
})
}
func
checkForShortTesting
(
t
*
testing
.
T
)
{
if
testing
.
Short
()
{
t
.
Skip
(
"too slow for testing.Short"
)
}
t
.
Run
(
"Check autopilot metrics on clients"
,
func
(
t
*
testing
.
T
)
{
hcl
:=
`
telemetry = {
prometheus_retention_time = "5s"
disable_hostname = true
metrics_prefix = "agent_2"
}
bootstrap = false
server = false
`
a
:=
StartTestAgent
(
t
,
TestAgent
{
HCL
:
hcl
})
defer
a
.
Shutdown
()
respRec
:=
httptest
.
NewRecorder
()
recordPromMetrics
(
t
,
a
,
respRec
)
// todo -- uncomment test once client metric is emitted anymore for clients
//assertMetricNotExists(t, respRec, "consul_autopilot_healthy")
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment