Skip to content

Commit d08b241

Browse files
authored
chore(prometheus): update histogram variable reference (#1167)
Signed-off-by: Miguel <[email protected]>
1 parent 835806c commit d08b241

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed

app/controlplane/pkg/metrics/prometheus/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ import (
2020
)
2121

2222
type ChainloopRegistryManager struct {
23-
Registries map[string]*registry.PrometheusRegistry
23+
registries map[string]*registry.PrometheusRegistry
2424
}
2525

2626
func NewChainloopRegistryManager() *ChainloopRegistryManager {
2727
return &ChainloopRegistryManager{
28-
Registries: make(map[string]*registry.PrometheusRegistry),
28+
registries: make(map[string]*registry.PrometheusRegistry),
2929
}
3030
}
3131

3232
// AddRegistry adds a registry to the manager
3333
func (rm *ChainloopRegistryManager) AddRegistry(reg *registry.PrometheusRegistry) {
34-
rm.Registries[reg.Name] = reg
34+
rm.registries[reg.Name] = reg
3535
}
3636

3737
// GetRegistryByName returns a registry by name
3838
func (rm *ChainloopRegistryManager) GetRegistryByName(name string) *registry.PrometheusRegistry {
39-
return rm.Registries[name]
39+
return rm.registries[name]
4040
}
4141

4242
// DeleteRegistryByName deletes a registry by name
4343
func (rm *ChainloopRegistryManager) DeleteRegistryByName(name string) {
44-
delete(rm.Registries, name)
44+
delete(rm.registries, name)
4545
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Copyright 2024 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package prometheus
17+
18+
import (
19+
"testing"
20+
21+
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/metrics/prometheus/registry"
22+
"github.com/stretchr/testify/require"
23+
"github.com/stretchr/testify/suite"
24+
)
25+
26+
func (s *managerTestSuite) TestAddAndRetrieveRegistries() {
27+
r1 := registry.NewPrometheusRegistry("test1", nil, nil)
28+
r2 := registry.NewPrometheusRegistry("test2", nil, nil)
29+
s.manager.AddRegistry(r1)
30+
s.manager.AddRegistry(r2)
31+
s.Len(s.manager.registries, 2)
32+
33+
s.Equal(r1, s.manager.GetRegistryByName("test1"))
34+
s.Equal(r2, s.manager.GetRegistryByName("test2"))
35+
s.Nil(s.manager.GetRegistryByName("test-not-found"))
36+
37+
// delete one
38+
s.manager.DeleteRegistryByName("test1")
39+
s.Len(s.manager.registries, 1)
40+
s.Nil(s.manager.GetRegistryByName("test1"))
41+
}
42+
43+
type managerTestSuite struct {
44+
suite.Suite
45+
manager *ChainloopRegistryManager
46+
}
47+
48+
func (s *managerTestSuite) SetupTest() {
49+
s.manager = NewChainloopRegistryManager()
50+
require.NotNil(s.T(), s.manager)
51+
require.NotNil(s.T(), s.manager.registries)
52+
}
53+
54+
func TestManager(t *testing.T) {
55+
suite.Run(t, new(managerTestSuite))
56+
}

app/controlplane/pkg/metrics/prometheus/registry/registry.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ type PrometheusRegistry struct {
3333
WorkflowRunDurationSeconds *prometheus.HistogramVec
3434
}
3535

36-
var workflowRunDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
37-
Name: "chainloop_workflow_run_duration_seconds",
38-
Help: "Duration of a workflow runs in seconds.",
39-
// 10 seconds to 20 minutes
40-
Buckets: []float64{10, 30, 60, 90, 120, 180, 240, 300, 600, 900, 1200},
41-
}, []string{"org", "workflow", "status", "runner"})
42-
4336
// NewPrometheusRegistry creates a new Prometheus registry with a given ID and collector
4437
func NewPrometheusRegistry(name string, gatherer collector.ChainloopMetricsGatherer, logger log.Logger) *PrometheusRegistry {
4538
reg := prometheus.NewRegistry()
@@ -49,6 +42,14 @@ func NewPrometheusRegistry(name string, gatherer collector.ChainloopMetricsGathe
4942

5043
reg.MustRegister(bcc)
5144

45+
// It's important to register this variable in the factory since it's a different one per registry
46+
workflowRunDurationSeconds := prometheus.NewHistogramVec(prometheus.HistogramOpts{
47+
Name: "chainloop_workflow_run_duration_seconds",
48+
Help: "Duration of a workflow runs in seconds.",
49+
// 10 seconds to 20 minutes
50+
Buckets: []float64{10, 30, 60, 90, 120, 180, 240, 300, 600, 900, 1200},
51+
}, []string{"org", "workflow", "status", "runner"})
52+
5253
// Custom metrics that come from the business logic
5354
reg.MustRegister(workflowRunDurationSeconds)
5455

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Copyright 2024 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package registry
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/suite"
22+
)
23+
24+
// This test looks obvious but checks an important property which is that the
25+
// metrics must be different between two isolated registries, not a shared memory space.
26+
func (s *registryTestSuite) TestIsolatedRegistries() {
27+
s.NotEqual(s.registry1, s.registry2)
28+
s.NotEqual(s.registry1.WorkflowRunDurationSeconds, s.registry2.WorkflowRunDurationSeconds)
29+
}
30+
31+
func (s *registryTestSuite) TestName() {
32+
testCases := []struct {
33+
registry *PrometheusRegistry
34+
expected string
35+
}{
36+
{s.registry1, "test1"},
37+
{s.registry2, "test2"},
38+
}
39+
40+
for _, tc := range testCases {
41+
s.Equal(tc.expected, tc.registry.Name)
42+
}
43+
}
44+
45+
type registryTestSuite struct {
46+
suite.Suite
47+
registry1, registry2 *PrometheusRegistry
48+
}
49+
50+
func (s *registryTestSuite) SetupTest() {
51+
s.registry1 = NewPrometheusRegistry("test1", nil, nil)
52+
s.registry2 = NewPrometheusRegistry("test2", nil, nil)
53+
}
54+
55+
func TestRegistry(t *testing.T) {
56+
suite.Run(t, new(registryTestSuite))
57+
}

0 commit comments

Comments
 (0)