Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
0eca5f27
Commit
0eca5f27
authored
1 year ago
by
Alena Petraki
Browse files
Options
Downloads
Patches
Plain Diff
Доработка теста-примера
parent
9c2a11da
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/metrics/example_test.go
+27
-25
27 additions, 25 deletions
pkg/metrics/example_test.go
with
27 additions
and
25 deletions
pkg/metrics/
metrics
_test.go
→
pkg/metrics/
example
_test.go
+
27
−
25
View file @
0eca5f27
package
metrics
package
metrics
import
(
import
(
"
contex
t"
"
fm
t"
"io"
"io"
"net"
"net/http"
"net/http"
"testing"
"testing"
...
@@ -15,36 +16,33 @@ import (
...
@@ -15,36 +16,33 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
)
func
serveMetrics
(
t
*
testing
.
T
)
*
http
.
Serv
er
{
func
serveMetrics
HTTP
(
t
*
testing
.
T
)
net
.
Listen
er
{
mux
:=
http
.
NewServeMux
()
mux
:=
http
.
NewServeMux
()
mux
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
mux
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
srv
:=
&
http
.
Server
{
Addr
:
"localhost:30301"
,
Handler
:
mux
}
srv
:=
&
http
.
Server
{
Addr
:
"localhost:30341"
,
Handler
:
mux
}
go
func
()
{
_
=
srv
.
ListenAndServe
()
}()
listener
,
err
:=
net
.
Listen
(
"tcp"
,
"localhost:0"
)
return
srv
require
.
NoError
(
t
,
err
)
go
func
()
{
_
=
srv
.
Serve
(
listener
)
}()
return
listener
}
}
func
TestMetrics_Example
(
t
*
testing
.
T
)
{
func
TestMetrics_Example
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
t
.
Run
(
"Metrics do not allow dynamically assigned labels"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"Metrics do not allow dynamically assigned labels"
,
func
(
t
*
testing
.
T
)
{
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
"test_counter_"
+
id
.
GenerateNewID
()},
nil
)
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
"test_counter_"
+
id
.
GenerateNewID
()},
nil
)
prometheus
.
MustRegister
(
m
)
prometheus
.
MustRegister
(
m
)
assert
.
Panics
(
t
,
func
()
{
assert
.
Panics
(
t
,
func
()
{
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
})
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
})
})
})
t
.
Run
(
"Metrics allow dynamically assigned label values"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"Metrics allow dynamically assigned label values"
,
func
(
t
*
testing
.
T
)
{
srv
:=
serveMetrics
(
t
)
l
:=
serveMetricsHTTP
(
t
)
defer
func
()
{
defer
func
()
{
_
=
l
.
Close
()
}()
_
=
srv
.
Shutdown
(
ctx
)
}()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
},
[]
string
{
"a"
,
"b"
})
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
},
[]
string
{
"a"
,
"b"
})
prometheus
.
MustRegister
(
m
)
prometheus
.
MustRegister
(
m
)
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
resp
,
err
:=
http
.
Get
(
"http://localhost:30301/metrics"
)
resp
,
err
:=
http
.
Get
(
fmt
.
Sprintf
(
"http://%s/metrics"
,
l
.
Addr
()
.
String
())
)
require
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
...
@@ -54,10 +52,8 @@ func TestMetrics_Example(t *testing.T) {
...
@@ -54,10 +52,8 @@ func TestMetrics_Example(t *testing.T) {
})
})
t
.
Run
(
"Metrics with constant label values"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"Metrics with constant label values"
,
func
(
t
*
testing
.
T
)
{
srv
:=
serveMetrics
(
t
)
l
:=
serveMetricsHTTP
(
t
)
defer
func
()
{
defer
func
()
{
_
=
l
.
Close
()
}()
_
=
srv
.
Shutdown
(
ctx
)
}()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
...
@@ -70,7 +66,7 @@ func TestMetrics_Example(t *testing.T) {
...
@@ -70,7 +66,7 @@ func TestMetrics_Example(t *testing.T) {
prometheus
.
MustRegister
(
m
)
prometheus
.
MustRegister
(
m
)
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
resp
,
err
:=
http
.
Get
(
"http://localhost:30301/metrics"
)
resp
,
err
:=
http
.
Get
(
fmt
.
Sprintf
(
"http://%s/metrics"
,
l
.
Addr
()
.
String
())
)
require
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
...
@@ -79,17 +75,23 @@ func TestMetrics_Example(t *testing.T) {
...
@@ -79,17 +75,23 @@ func TestMetrics_Example(t *testing.T) {
assert
.
Contains
(
t
,
string
(
b
),
name
+
"{a=
\"
v_a1
\"
,b=
\"
v_b1
\"
,const_1=
\"
val_1
\"
} 1"
)
assert
.
Contains
(
t
,
string
(
b
),
name
+
"{a=
\"
v_a1
\"
,b=
\"
v_b1
\"
,const_1=
\"
val_1
\"
} 1"
)
})
})
t
.
Run
(
"Register multiple metrics with the same name and labels combine into metric family"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"Multiple metrics registered with the same name and labels combine into metric family"
,
func
(
t
*
testing
.
T
)
{
registry
:=
prometheus
.
NewRegistry
()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
m1
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_1"
}},
[]
string
{
"a1"
,
"b1"
})
m1
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_1"
}},
[]
string
{
"a1"
,
"b1"
})
m2
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_2"
}},
[]
string
{
"a1"
,
"b1"
})
registry
:=
prometheus
.
NewRegistry
()
registry
.
MustRegister
(
m1
)
registry
.
MustRegister
(
m1
)
registry
.
MustRegister
(
m2
)
m1
.
With
(
prometheus
.
Labels
{
"a1"
:
"v_a1"
,
"b1"
:
"v_b1"
})
.
Inc
()
m1
.
With
(
prometheus
.
Labels
{
"a1"
:
"v_a1"
,
"b1"
:
"v_b1"
})
.
Inc
()
m2
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_2"
}},
[]
string
{
"a1"
,
"b1"
})
registry
.
MustRegister
(
m2
)
m2
.
With
(
prometheus
.
Labels
{
"a1"
:
"v_a2"
,
"b1"
:
"v_b2"
})
.
Inc
()
m2
.
With
(
prometheus
.
Labels
{
"a1"
:
"v_a2"
,
"b1"
:
"v_b2"
})
.
Inc
()
assert
.
Panics
(
t
,
func
()
{
m2
.
With
(
prometheus
.
Labels
{
"c"
:
"v3"
})
.
Inc
()
},
"Невозможно добавить метку, которая не была зарегистрирована при создании метрики"
)
assert
.
Panics
(
t
,
func
()
{
m3
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_2"
:
"val_2"
}},
[]
string
{
"a3"
,
"b3"
})
registry
.
MustRegister
(
m3
)
},
"Невозможно зарегистрировать метрики с одним названием, но разными метками"
)
metricsFamily
,
err
:=
registry
.
Gather
()
metricsFamily
,
err
:=
registry
.
Gather
()
require
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
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