Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit e014dfc

Browse files
authored
feat: add metricsEnabled env (#435)
1 parent c064179 commit e014dfc

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Configuration of the adapter is done via environment variables at startup.
175175
| `SCANNER_API_SERVER_READ_TIMEOUT` | `15s` | The maximum duration for reading the entire request, including the body |
176176
| `SCANNER_API_SERVER_WRITE_TIMEOUT` | `15s` | The maximum duration before timing out writes of the response |
177177
| `SCANNER_API_SERVER_IDLE_TIMEOUT` | `60s` | The maximum amount of time to wait for the next request when keep-alives are enabled |
178+
| `SCANNER_API_SERVER_METRICS_ENABLED` | `true` | Whether to enable metrics |
178179
| `SCANNER_TRIVY_CACHE_DIR` | `/home/scanner/.cache/trivy` | Trivy cache directory |
179180
| `SCANNER_TRIVY_REPORTS_DIR` | `/home/scanner/.cache/reports` | Trivy reports directory |
180181
| `SCANNER_TRIVY_DEBUG_MODE` | `false` | The flag to enable or disable Trivy debug mode |
@@ -184,7 +185,7 @@ Configuration of the adapter is done via environment variables at startup.
184185
| `SCANNER_TRIVY_IGNORE_UNFIXED` | `false` | The flag to display only fixed vulnerabilities |
185186
| `SCANNER_TRIVY_IGNORE_POLICY` | `` | The path for the Trivy ignore policy OPA Rego file |
186187
| `SCANNER_TRIVY_SKIP_UPDATE` | `false` | The flag to disable [Trivy DB] downloads. |
187-
| `SCANNER_TRIVY_SKIP_JAVA_DB_UPDATE` | `false` | The flag to disable [Trivy JAVA DB] downloads. |
188+
| `SCANNER_TRIVY_SKIP_JAVA_DB_UPDATE` | `false` | The flag to disable [Trivy JAVA DB] downloads. |
188189
| `SCANNER_TRIVY_OFFLINE_SCAN` | `false` | The flag to disable external API requests to identify dependencies. |
189190
| `SCANNER_TRIVY_GITHUB_TOKEN` | N/A | The GitHub access token to download [Trivy DB] (see [GitHub rate limiting][gh-rate-limit]) |
190191
| `SCANNER_TRIVY_INSECURE` | `false` | The flag to skip verifying registry certificate |

pkg/etc/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type API struct {
4949
ReadTimeout time.Duration `env:"SCANNER_API_SERVER_READ_TIMEOUT" envDefault:"15s"`
5050
WriteTimeout time.Duration `env:"SCANNER_API_SERVER_WRITE_TIMEOUT" envDefault:"15s"`
5151
IdleTimeout time.Duration `env:"SCANNER_API_SERVER_IDLE_TIMEOUT" envDefault:"60s"`
52+
MetricsEnabled bool `env:"SCANNER_API_SERVER_METRICS_ENABLED" envDefault:"true"`
5253
}
5354

5455
func (c *API) IsTLSEnabled() bool {

pkg/etc/config_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ func TestGetConfig(t *testing.T) {
6060
},
6161
expectedConfig: Config{
6262
API: API{
63-
Addr: ":8080",
64-
ReadTimeout: parseDuration(t, "15s"),
65-
WriteTimeout: parseDuration(t, "15s"),
66-
IdleTimeout: parseDuration(t, "60s"),
63+
Addr: ":8080",
64+
ReadTimeout: parseDuration(t, "15s"),
65+
WriteTimeout: parseDuration(t, "15s"),
66+
IdleTimeout: parseDuration(t, "60s"),
67+
MetricsEnabled: true,
6768
},
6869
Trivy: Trivy{
6970
DebugMode: true,
@@ -99,10 +100,11 @@ func TestGetConfig(t *testing.T) {
99100
name: "Should return default config",
100101
expectedConfig: Config{
101102
API: API{
102-
Addr: ":8080",
103-
ReadTimeout: parseDuration(t, "15s"),
104-
WriteTimeout: parseDuration(t, "15s"),
105-
IdleTimeout: parseDuration(t, "60s"),
103+
Addr: ":8080",
104+
ReadTimeout: parseDuration(t, "15s"),
105+
WriteTimeout: parseDuration(t, "15s"),
106+
IdleTimeout: parseDuration(t, "60s"),
107+
MetricsEnabled: true,
106108
},
107109
Trivy: Trivy{
108110
DebugMode: false,
@@ -166,10 +168,11 @@ func TestGetConfig(t *testing.T) {
166168
"SCANNER_JOB_QUEUE_REDIS_NAMESPACE": "job-queue.ns",
167169
"SCANNER_JOB_QUEUE_WORKER_CONCURRENCY": "3",
168170

169-
"SCANNER_REDIS_URL": "redis://harbor-harbor-redis:6379",
170-
"SCANNER_REDIS_POOL_MAX_ACTIVE": "3",
171-
"SCANNER_REDIS_POOL_MAX_IDLE": "7",
172-
"SCANNER_REDIS_POOL_IDLE_TIMEOUT": "3m",
171+
"SCANNER_REDIS_URL": "redis://harbor-harbor-redis:6379",
172+
"SCANNER_REDIS_POOL_MAX_ACTIVE": "3",
173+
"SCANNER_REDIS_POOL_MAX_IDLE": "7",
174+
"SCANNER_REDIS_POOL_IDLE_TIMEOUT": "3m",
175+
"SCANNER_API_SERVER_METRICS_ENABLED": "false",
173176
},
174177
expectedConfig: Config{
175178
API: API{
@@ -180,6 +183,7 @@ func TestGetConfig(t *testing.T) {
180183
ReadTimeout: parseDuration(t, "1h"),
181184
WriteTimeout: parseDuration(t, "2m"),
182185
IdleTimeout: parseDuration(t, "3m10s"),
186+
MetricsEnabled: false,
183187
},
184188
Trivy: Trivy{
185189
CacheDir: "/home/scanner/trivy-cache",

pkg/http/api/v1/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func NewAPIHandler(info etc.BuildInfo, config etc.Config, enqueuer queue.Enqueue
6060
probeRouter.Methods(http.MethodGet).Path("/healthy").HandlerFunc(handler.GetHealthy)
6161
probeRouter.Methods(http.MethodGet).Path("/ready").HandlerFunc(handler.GetReady)
6262

63-
router.Methods(http.MethodGet).Path("/metrics").Handler(promhttp.Handler())
63+
if config.API.MetricsEnabled {
64+
router.Methods(http.MethodGet).Path("/metrics").Handler(promhttp.Handler())
65+
}
6466

6567
return router
6668
}

0 commit comments

Comments
 (0)