Skip to content

Commit

Permalink
feat: allow disabling profiling endpoints (#2195)
Browse files Browse the repository at this point in the history
* feat(wip): allow disabling profiling endpoints

* chore: wip tests
  • Loading branch information
markphelps authored Oct 4, 2023
1 parent bf89edb commit 08e8450
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
7 changes: 7 additions & 0 deletions config/flipt.schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "strings"
authentication?: #authentication
cache?: #cache
cors?: #cors
diagnostics?: #diagnostics
storage?: #storage
db?: #db
log?: #log
Expand Down Expand Up @@ -121,6 +122,12 @@ import "strings"
allowed_origins?: [...] | string | *["*"]
}

#diagnostics: {
profiling?: {
enabled?: bool | *true
}
}

#storage: {
type: "database" | "git" | "local" | "object" | *""
local?: path: string | *"."
Expand Down
21 changes: 21 additions & 0 deletions config/flipt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"db": {
"$ref": "#/definitions/db"
},
"diagnostics": {
"$ref": "#/definitions/diagnostics"
},
"storage": {
"$ref": "#/definitions/storage"
},
Expand Down Expand Up @@ -397,6 +400,24 @@
"required": [],
"title": "Cors"
},
"diagnostics": {
"type": "object",
"additionalProperties": false,
"properties": {
"profiling": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"default": true
}
}
}
},
"required": [],
"title": "Diagnostics"
},
"db": {
"type": "object",
"additionalProperties": false,
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ func NewHTTPServer(
})
r.Use(middleware.Compress(gzip.DefaultCompression))
r.Use(middleware.Recoverer)
r.Mount("/debug", middleware.Profiler())

if cfg.Diagnostics.Profiling.Enabled {
r.Mount("/debug", middleware.Profiler())
}

r.Mount("/metrics", promhttp.Handler())

r.Group(func(r chi.Router) {
Expand Down
21 changes: 14 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ var DecodeHooks = []mapstructure.DecodeHookFunc{
// any errors derived from the resulting state of the configuration.
type Config struct {
Version string `json:"version,omitempty" mapstructure:"version,omitempty" yaml:"version,omitempty"`
Audit AuditConfig `json:"audit,omitempty" mapstructure:"audit" yaml:"audit,omitempty"`
Authentication AuthenticationConfig `json:"authentication,omitempty" mapstructure:"authentication" yaml:"authentication,omitempty"`
Cache CacheConfig `json:"cache,omitempty" mapstructure:"cache" yaml:"cache,omitempty"`
Cors CorsConfig `json:"cors,omitempty" mapstructure:"cors" yaml:"cors,omitempty"`
Database DatabaseConfig `json:"db,omitempty" mapstructure:"db" yaml:"db,omitempty"`
Diagnostics DiagnosticConfig `json:"diagnostics,omitempty" mapstructure:"diagnostics" yaml:"diagnostics,omitempty"`
Experimental ExperimentalConfig `json:"experimental,omitempty" mapstructure:"experimental" yaml:"experimental,omitempty"`
Log LogConfig `json:"log,omitempty" mapstructure:"log" yaml:"log,omitempty"`
UI UIConfig `json:"ui,omitempty" mapstructure:"ui" yaml:"ui,omitempty"`
Cors CorsConfig `json:"cors,omitempty" mapstructure:"cors" yaml:"cors,omitempty"`
Cache CacheConfig `json:"cache,omitempty" mapstructure:"cache" yaml:"cache,omitempty"`
Meta MetaConfig `json:"meta,omitempty" mapstructure:"meta" yaml:"meta,omitempty"`
Server ServerConfig `json:"server,omitempty" mapstructure:"server" yaml:"server,omitempty"`
Storage StorageConfig `json:"storage,omitempty" mapstructure:"storage" yaml:"storage,omitempty"`
Tracing TracingConfig `json:"tracing,omitempty" mapstructure:"tracing" yaml:"tracing,omitempty"`
Database DatabaseConfig `json:"db,omitempty" mapstructure:"db" yaml:"db,omitempty"`
Meta MetaConfig `json:"meta,omitempty" mapstructure:"meta" yaml:"meta,omitempty"`
Authentication AuthenticationConfig `json:"authentication,omitempty" mapstructure:"authentication" yaml:"authentication,omitempty"`
Audit AuditConfig `json:"audit,omitempty" mapstructure:"audit" yaml:"audit,omitempty"`
UI UIConfig `json:"ui,omitempty" mapstructure:"ui" yaml:"ui,omitempty"`
}

type Result struct {
Expand Down Expand Up @@ -469,6 +470,12 @@ func Default() *Config {
},
},

Diagnostics: DiagnosticConfig{
Profiling: ProfilingDiagnosticConfig{
Enabled: true,
},
},

Server: ServerConfig{
Host: "0.0.0.0",
Protocol: HTTP,
Expand Down
24 changes: 24 additions & 0 deletions internal/config/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import "github.com/spf13/viper"

// cheers up the unparam linter
var _ defaulter = (*DiagnosticConfig)(nil)

type DiagnosticConfig struct {
Profiling ProfilingDiagnosticConfig `json:"profiling,omitempty" mapstructure:"profiling" yaml:"profiling,omitempty"`
}

type ProfilingDiagnosticConfig struct {
Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled,omitempty"`
}

func (c *DiagnosticConfig) setDefaults(v *viper.Viper) error {
v.SetDefault("diagnostics", map[string]any{
"profiling": map[string]any{
"enabled": true,
},
})

return nil
}
5 changes: 4 additions & 1 deletion internal/config/testdata/marshal/yaml/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ ui:
cors:
enabled: false
allowed_origins:
- '*'
- "*"
server:
host: 0.0.0.0
http_port: 8080
https_port: 443
grpc_port: 9000
storage:
type: database
diagnostics:
profiling:
enabled: true
db:
url: file:/tmp/flipt/flipt.db
max_idle_conn: 2
Expand Down

0 comments on commit 08e8450

Please sign in to comment.