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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
6845af08
Commit
6845af08
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
удалены тесты prometheus
parent
90c95329
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/metrics/example_test.go
+0
-123
0 additions, 123 deletions
pkg/metrics/example_test.go
with
0 additions
and
123 deletions
pkg/metrics/example_test.go
deleted
100644 → 0
+
0
−
123
View file @
90c95329
package
metrics
import
(
"fmt"
"io"
"net"
"net/http"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/id"
"git.perx.ru/perxis/perxis-go/pkg/optional"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto
"github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func
serveMetricsHTTP
(
t
*
testing
.
T
)
net
.
Listener
{
mux
:=
http
.
NewServeMux
()
mux
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
srv
:=
&
http
.
Server
{
Addr
:
"localhost:30341"
,
Handler
:
mux
}
listener
,
err
:=
net
.
Listen
(
"tcp"
,
"localhost:0"
)
require
.
NoError
(
t
,
err
)
go
func
()
{
_
=
srv
.
Serve
(
listener
)
}()
return
listener
}
func
TestMetrics_Example
(
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
)
prometheus
.
MustRegister
(
m
)
assert
.
Panics
(
t
,
func
()
{
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
})
})
t
.
Run
(
"Metrics allow dynamically assigned label values"
,
func
(
t
*
testing
.
T
)
{
l
:=
serveMetricsHTTP
(
t
)
defer
func
()
{
_
=
l
.
Close
()
}()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
},
[]
string
{
"a"
,
"b"
})
prometheus
.
MustRegister
(
m
)
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
resp
,
err
:=
http
.
Get
(
fmt
.
Sprintf
(
"http://%s/metrics"
,
l
.
Addr
()
.
String
()))
require
.
NoError
(
t
,
err
)
defer
resp
.
Body
.
Close
()
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
require
.
NoError
(
t
,
err
)
// fmt.Println(string(b))
assert
.
Contains
(
t
,
string
(
b
),
name
+
"{a=
\"
v_a1
\"
,b=
\"
v_b1
\"
} 1"
)
})
t
.
Run
(
"Metrics with constant label values"
,
func
(
t
*
testing
.
T
)
{
l
:=
serveMetricsHTTP
(
t
)
defer
func
()
{
_
=
l
.
Close
()
}()
name
:=
"test_counter_"
+
id
.
GenerateNewID
()
m
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Namespace
:
"test_namespace"
,
Subsystem
:
"test_subsystem"
,
Name
:
name
,
Help
:
"Test Help"
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_1"
},
},
[]
string
{
"a"
,
"b"
})
prometheus
.
MustRegister
(
m
)
m
.
With
(
prometheus
.
Labels
{
"a"
:
"v_a1"
,
"b"
:
"v_b1"
})
.
Inc
()
resp
,
err
:=
http
.
Get
(
fmt
.
Sprintf
(
"http://%s/metrics"
,
l
.
Addr
()
.
String
()))
require
.
NoError
(
t
,
err
)
defer
resp
.
Body
.
Close
()
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
require
.
NoError
(
t
,
err
)
// fmt.Println(string(b))
assert
.
Contains
(
t
,
string
(
b
),
name
+
"{a=
\"
v_a1
\"
,b=
\"
v_b1
\"
,const_1=
\"
val_1
\"
} 1"
)
})
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
()
m1
:=
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
name
,
ConstLabels
:
prometheus
.
Labels
{
"const_1"
:
"val_1"
}},
[]
string
{
"a1"
,
"b1"
})
registry
.
MustRegister
(
m1
)
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
()
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
()
require
.
NoError
(
t
,
err
)
require
.
Len
(
t
,
metricsFamily
,
1
)
assert
.
Equal
(
t
,
name
,
*
metricsFamily
[
0
]
.
Name
)
require
.
Len
(
t
,
metricsFamily
[
0
]
.
Metric
,
2
)
registeredMetric1
:=
metricsFamily
[
0
]
.
Metric
[
0
]
assert
.
Equal
(
t
,
[]
*
dto
.
LabelPair
{
{
Name
:
optional
.
Ptr
(
"a1"
),
Value
:
optional
.
Ptr
(
"v_a1"
)},
{
Name
:
optional
.
Ptr
(
"b1"
),
Value
:
optional
.
Ptr
(
"v_b1"
)},
{
Name
:
optional
.
Ptr
(
"const_1"
),
Value
:
optional
.
Ptr
(
"val_1"
)},
},
registeredMetric1
.
Label
,
)
registeredMetric2
:=
metricsFamily
[
0
]
.
Metric
[
1
]
assert
.
Equal
(
t
,
[]
*
dto
.
LabelPair
{
{
Name
:
optional
.
Ptr
(
"a1"
),
Value
:
optional
.
Ptr
(
"v_a2"
)},
{
Name
:
optional
.
Ptr
(
"b1"
),
Value
:
optional
.
Ptr
(
"v_b2"
)},
{
Name
:
optional
.
Ptr
(
"const_1"
),
Value
:
optional
.
Ptr
(
"val_2"
)},
},
registeredMetric2
.
Label
,
)
})
}
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