Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
kube-prometheus
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
prometheus-operator
kube-prometheus
Commits
f846c2e7
Unverified
Commit
f846c2e7
authored
5 years ago
by
paulfantom
Browse files
Options
Downloads
Patches
Plain Diff
tests/e2e: use prometheus client_golang in e2e tests & add testing for http endpoints
parent
6f37ddbc
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/e2e/main_test.go
+25
-1
25 additions, 1 deletion
tests/e2e/main_test.go
tests/e2e/prometheus_client.go
+60
-27
60 additions, 27 deletions
tests/e2e/prometheus_client.go
with
85 additions
and
28 deletions
tests/e2e/main_test.go
+
25
−
1
View file @
f846c2e7
...
@@ -123,7 +123,7 @@ func TestDroppedMetrics(t *testing.T) {
...
@@ -123,7 +123,7 @@ func TestDroppedMetrics(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
for
_
,
k
:=
range
md
.
Data
{
for
_
,
k
:=
range
md
{
// check if the metric' help text contains Deprecated
// check if the metric' help text contains Deprecated
if
strings
.
Contains
(
k
.
Help
,
"Deprecated"
)
{
if
strings
.
Contains
(
k
.
Help
,
"Deprecated"
)
{
// query prometheus for the Deprecated metric
// query prometheus for the Deprecated metric
...
@@ -138,3 +138,27 @@ func TestDroppedMetrics(t *testing.T) {
...
@@ -138,3 +138,27 @@ func TestDroppedMetrics(t *testing.T) {
}
}
}
}
func
TestTargetsScheme
(
t
*
testing
.
T
)
{
// query targets for all endpoints
tgs
,
err
:=
promClient
.
targets
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
// exclude jobs from checking for http endpoints
// TODO(paulfantom): This should be reduced as we secure connections for those components
exclude
:=
map
[
string
]
bool
{
"alertmanager-main"
:
true
,
"prometheus-k8s"
:
true
,
"kube-dns"
:
true
,
"grafana"
:
true
,
}
for
_
,
k
:=
range
tgs
.
Active
{
job
:=
k
.
Labels
[
"job"
]
if
k
.
DiscoveredLabels
[
"__scheme__"
]
==
"http"
&&
!
exclude
[
string
(
job
)]
{
log
.
Fatalf
(
"target exposing metrics over HTTP instead of HTTPS: %+v"
,
k
)
}
}
}
This diff is collapsed.
Click to expand it.
tests/e2e/prometheus_client.go
+
60
−
27
View file @
f846c2e7
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes"
"github.com/Jeffail/gabs"
"github.com/Jeffail/gabs"
promv1
"github.com/prometheus/client_golang/api/prometheus/v1"
)
)
type
prometheusClient
struct
{
type
prometheusClient
struct
{
...
@@ -32,6 +33,42 @@ func newPrometheusClient(kubeClient kubernetes.Interface) *prometheusClient {
...
@@ -32,6 +33,42 @@ func newPrometheusClient(kubeClient kubernetes.Interface) *prometheusClient {
return
&
prometheusClient
{
kubeClient
}
return
&
prometheusClient
{
kubeClient
}
}
}
// Response hold API response in a form similar to apiResponse struct from prometheus/client_golang
// https://github.com/prometheus/client_golang/blob/master/api/prometheus/v1/api.go
type
Response
struct
{
Status
string
`json:"status"`
Data
json
.
RawMessage
`json:"data"`
}
// apiRequest makes a request against specified Prometheus API endpoint
func
(
c
*
prometheusClient
)
apiRequest
(
endpoint
string
,
selector
string
,
query
string
)
(
Response
,
error
)
{
req
:=
c
.
kubeClient
.
CoreV1
()
.
RESTClient
()
.
Get
()
.
Namespace
(
"monitoring"
)
.
Resource
(
"pods"
)
.
SubResource
(
"proxy"
)
.
Name
(
"prometheus-k8s-0:9090"
)
.
Suffix
(
endpoint
)
.
Param
(
selector
,
query
)
var
data
Response
b
,
err
:=
req
.
DoRaw
()
if
err
!=
nil
{
return
data
,
err
}
r
:=
bytes
.
NewReader
(
b
)
decoder
:=
json
.
NewDecoder
(
r
)
err
=
decoder
.
Decode
(
&
data
)
if
err
!=
nil
{
return
data
,
err
}
if
data
.
Status
!=
"success"
{
return
data
,
fmt
.
Errorf
(
"status of returned response was not successful; status: %s"
,
data
.
Status
)
}
return
data
,
err
}
// Query makes a request against the Prometheus /api/v1/query endpoint.
// Query makes a request against the Prometheus /api/v1/query endpoint.
func
(
c
*
prometheusClient
)
query
(
query
string
)
(
int
,
error
)
{
func
(
c
*
prometheusClient
)
query
(
query
string
)
(
int
,
error
)
{
req
:=
c
.
kubeClient
.
CoreV1
()
.
RESTClient
()
.
Get
()
.
req
:=
c
.
kubeClient
.
CoreV1
()
.
RESTClient
()
.
Get
()
.
...
@@ -55,40 +92,36 @@ func (c *prometheusClient) query(query string) (int, error) {
...
@@ -55,40 +92,36 @@ func (c *prometheusClient) query(query string) (int, error) {
return
n
,
err
return
n
,
err
}
}
type
Metadata
struct
{
Status
string
`json:"status,omitempty"`
Data
[]
Data
`json:"data,omitempty"`
}
type
Data
struct
{
Metric
string
`json:"metric,omitempty"`
Help
string
`json:"help,omitempty"`
}
// metadata makes a request against the Prometheus /api/v1/targets/metadata endpoint.
// metadata makes a request against the Prometheus /api/v1/targets/metadata endpoint.
// It returns all the metrics and its metadata.
// It returns all the metrics and its metadata.
func
(
c
*
prometheusClient
)
metadata
(
query
string
)
(
Metadata
,
error
)
{
func
(
c
*
prometheusClient
)
metadata
(
query
string
)
([]
promv1
.
MetricMetadata
,
error
)
{
req
:=
c
.
kubeClient
.
CoreV1
()
.
RESTClient
()
.
Get
()
.
var
metadata
[]
promv1
.
MetricMetadata
Namespace
(
"monitoring"
)
.
rsp
,
err
:=
c
.
apiRequest
(
"/api/v1/targets/metadata"
,
"match_target"
,
query
)
Resource
(
"pods"
)
.
SubResource
(
"proxy"
)
.
Name
(
"prometheus-k8s-0:9090"
)
.
Suffix
(
"/api/v1/targets/metadata"
)
.
Param
(
"match_target"
,
query
)
var
data
Metadata
r
:=
bytes
.
NewReader
(
rsp
.
Data
)
b
,
err
:=
req
.
DoRaw
()
decoder
:=
json
.
NewDecoder
(
r
)
err
=
decoder
.
Decode
(
&
metadata
)
if
err
!=
nil
{
if
err
!=
nil
{
return
data
,
err
return
meta
data
,
err
}
}
return
metadata
,
err
}
r
:=
bytes
.
NewReader
(
b
)
// targets makes a request against the Prometheus /api/v1/targets endpoint.
decoder
:=
json
.
NewDecoder
(
r
)
// It returns all targets registered in prometheus.
err
=
decoder
.
Decode
(
&
data
)
func
(
c
*
prometheusClient
)
targets
()
(
promv1
.
TargetsResult
,
error
)
{
var
targets
promv1
.
TargetsResult
rsp
,
err
:=
c
.
apiRequest
(
"/api/v1/targets"
,
"state"
,
"any"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
da
ta
,
err
return
ta
rgets
,
err
}
}
if
data
.
Status
!=
"success"
{
return
data
,
fmt
.
Errorf
(
"status of returned response was not successful; status: %s"
,
data
.
Status
)
r
:=
bytes
.
NewReader
(
rsp
.
Data
)
decoder
:=
json
.
NewDecoder
(
r
)
err
=
decoder
.
Decode
(
&
targets
)
if
err
!=
nil
{
return
targets
,
err
}
}
return
data
,
err
return
targets
,
err
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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