Skip to content

Commit df8ada3

Browse files
authored
feat: add profiler to controlplane (#1546)
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
1 parent 6b93967 commit df8ada3

12 files changed

Lines changed: 253 additions & 178 deletions

File tree

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ linters-settings:
3434
excludes:
3535
# https://github.com/moby/moby/issues/48358
3636
- G115
37+
# We have the endpoint enabled on demand
38+
- G108
3739
gofmt:
3840
simplify: true
3941
dupl:

app/controlplane/cmd/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ import (
3636
"github.com/chainloop-dev/chainloop/pkg/credentials/manager"
3737
"github.com/chainloop-dev/chainloop/pkg/servicelogger"
3838

39+
_ "net/http/pprof"
40+
3941
"github.com/go-kratos/kratos/v2"
4042
"github.com/go-kratos/kratos/v2/config"
4143
"github.com/go-kratos/kratos/v2/config/env"
4244
"github.com/go-kratos/kratos/v2/config/file"
4345
"github.com/go-kratos/kratos/v2/log"
46+
"github.com/go-kratos/kratos/v2/transport"
4447
"github.com/go-kratos/kratos/v2/transport/grpc"
4548
"github.com/go-kratos/kratos/v2/transport/http"
4649
)
@@ -61,15 +64,20 @@ func init() {
6164
flag.StringVar(&flagconf, "conf", "../configs", "config path, eg: -conf config.yaml")
6265
}
6366

64-
func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server, ms *server.HTTPMetricsServer, expirer *biz.WorkflowRunExpirerUseCase, plugins sdk.AvailablePlugins, tokenSync *biz.APITokenSyncerUseCase) *app {
67+
func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server, ms *server.HTTPMetricsServer, profilerSvc *server.HTTPProfilerServer, expirer *biz.WorkflowRunExpirerUseCase, plugins sdk.AvailablePlugins, tokenSync *biz.APITokenSyncerUseCase, cfg *conf.Bootstrap) *app {
68+
servers := []transport.Server{gs, hs, ms}
69+
if cfg.EnableProfiler {
70+
servers = append(servers, profilerSvc)
71+
}
72+
6573
return &app{
6674
kratos.New(
6775
kratos.ID(id),
6876
kratos.Name(Name),
6977
kratos.Version(Version),
7078
kratos.Metadata(map[string]string{}),
7179
kratos.Logger(logger),
72-
kratos.Server(gs, hs, ms),
80+
kratos.Server(servers...),
7381
), expirer, plugins, tokenSync}
7482
}
7583

app/controlplane/cmd/wire_gen.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/configs/config.devel.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ prometheus_integration:
8383
policy_providers:
8484
- name: chainloop
8585
default: true
86-
url: http://localhost:8002/v1
86+
url: http://localhost:8002/v1
87+
88+
enable_profiler: true

app/controlplane/configs/samples/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ onboarding:
8787

8888
# Organizations with Prometheus integration enabled
8989
prometheus_integration:
90-
- org_name: "my-org"
90+
- org_name: "my-org"
91+
92+
enable_profiler: true

app/controlplane/internal/conf/controlplane/config/v1/conf.pb.go

Lines changed: 178 additions & 167 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/conf/controlplane/config/v1/conf.proto

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ message Bootstrap {
6868

6969
// providers for external policy repositories
7070
repeated PolicyProvider policy_providers = 12;
71+
72+
// Enable :6060 pprof endpoint
73+
bool enable_profiler = 13;
7174
}
7275

7376
message PolicyProvider {
7477
string name = 1 [(buf.validate.field) = {
7578
cel: {
76-
message: "must contain only lowercase letters, numbers, and hyphens.",
77-
expression: "this.matches('^[a-z0-9]([-a-z0-9]*[a-z0-9])?$')",
78-
id: "name.dns-1123",
79-
},
79+
message: "must contain only lowercase letters, numbers, and hyphens."
80+
expression: "this.matches('^[a-z0-9]([-a-z0-9]*[a-z0-9])?$')"
81+
id: "name.dns-1123"
82+
}
8083
}];
8184
// default policies will be used for schema-less references eg `my-policy`. Only one provider can be the default one
8285
bool default = 2;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 server
17+
18+
import (
19+
"time"
20+
21+
"github.com/go-kratos/kratos/v2/transport/http"
22+
)
23+
24+
// HTTPMetricsServer is a HTTP server that exposes the metrics endpoint
25+
type HTTPProfilerServer struct {
26+
*http.Server
27+
}
28+
29+
// NewHTTPProfilerServer exposes the metrics endpoint in another port
30+
func NewHTTPProfilerServer(_ *Opts) (*HTTPProfilerServer, error) {
31+
httpSrv := http.NewServer(http.Address("0.0.0.0:6060"), http.Timeout(10*time.Second))
32+
33+
return &HTTPProfilerServer{httpSrv}, nil
34+
}

app/controlplane/internal/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var ProviderSet = wire.NewSet(
2424
NewGRPCServer,
2525
NewHTTPServer,
2626
NewHTTPMetricsServer,
27+
NewHTTPProfilerServer,
2728
wire.Struct(new(Opts), "*"),
2829
)
2930

deployment/chainloop/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Chainloop is an open source software supply chain control plane, a
77

88
type: application
99
# Bump the patch (not minor, not major) version on each change in the Chart Source code
10-
version: 1.131.0
10+
version: 1.131.1
1111
# Do not update appVersion, this is handled automatically by the release process
1212
appVersion: v0.113.0
1313

0 commit comments

Comments
 (0)