Repository: minio/minio (language: go)

Issue:
Metrics are still reported for non-existing buckets
In https://github.com/minio/minio/pull/17987, a fix was added to avoid creating bucket metadata (and metrics) for non-existing buckets in collectAPIStats.

We're still observing a lot of metrics from non-existent buckets due to our internal security scanning tool. The issue seems to be that countBktStat only gets the expected value the first time collcetAPIStats is called for any given bucket. In other words, if you run `curl http://localhost:9000/abc123` at least twice, you'll get stats for the abc123-bucket from the metrics exporter.

I manually added some debugging to bucket-metadata-sys.go to catch a stracktrace of when the nonexistent map is added, and I think that the relevant stacktrace is this:
```
BucketMetadataSys::GetConfig xyz: adding metadata for bucket
goroutine 781 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:24 +0x5e
runtime/debug.PrintStack()
	runtime/debug/stack.go:16 +0x13
github.com/minio/minio/cmd.(*BucketMetadataSys).GetConfig(_, {_, _}, {_, _})
	github.com/minio/minio/cmd/bucket-metadata-sys.go:464 +0x2fe
github.com/minio/minio/cmd.(*BucketMetadataSys).GetPolicyConfig(0x0?, {0xc0011ae13d, 0x3})
	github.com/minio/minio/cmd/bucket-metadata-sys.go:356 +0x72
github.com/minio/minio/cmd.(*PolicySys).Get(...)
	github.com/minio/minio/cmd/bucket-policy.go:43
github.com/minio/minio/cmd.(*PolicySys).IsAllowed(0x0?, {{0x0, 0x0}, {0x0, 0x0, 0x0}, {0x2d1f93c, 0xd}, {0xc0011ae13d, 0x3}, ...})
	github.com/minio/minio/cmd/bucket-policy.go:49 +0x36
github.com/minio/minio/cmd.authorizeRequest({0x4d2fc38?, 0xc00131a9f0?}, 0xc00121f680, {0x2d1f93c, 0xd})
	github.com/minio/minio/cmd/auth-handler.go:430 +0x305
github.com/minio/minio/cmd.checkRequestAuthTypeCredential({0x4d2fc38, 0xc00131a9f0}, 0xc00121f680, {0x2d1f93c, 0xd})
	github.com/minio/minio/cmd/auth-handler.go:532 +0x105
github.com/minio/minio/cmd.checkRequestAuthType({0x4d2fc38, 0xc00131a9f0}, 0xc00121f680, {0x2d1f93c, 0xd}, {0xc0011ae13d, 0x3}, {0x0, 0x0})
	github.com/minio/minio/cmd/auth-handler.go:339 +0x14a
github.com/minio/minio/cmd.objectAPIHandlers.ListObjectsV1Handler({0xc08dcf4150?}, {0x4d2a310, 0xc08d2a8180}, 0xc00121f680)
	github.com/minio/minio/cmd/bucket-listobjects-handlers.go:287 +0x1c8
net/http.HandlerFunc.ServeHTTP(0x54144c9?, {0x4d2a310?, 0xc08d2a8180?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.s3APIMiddleware.func1.httpTraceAll.httpTrace.4({0x4d2a310, 0xc08d2a8180}, 0xc00121f680)
	github.com/minio/minio/cmd/http-tracer.go:189 +0xef
net/http.HandlerFunc.ServeHTTP(0x2be4860?, {0x4d2a310?, 0xc08d2a8180?}, 0x4?)
	net/http/server.go:2166 +0x29
github.com/klauspost/compress/gzhttp.NewWrapper.func1.1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/klauspost/compress@v1.17.6/gzhttp/compress.go:501 +0x453
net/http.HandlerFunc.ServeHTTP(0xc08a93f658?, {0x4d29200?, 0xc08d1fc000?}, 0x1?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.s3APIMiddleware.func1.maxClients.1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/handler-api.go:334 +0x477
net/http.HandlerFunc.ServeHTTP(0x64c4f70?, {0x4d29200?, 0xc08d1fc000?}, 0xc0011ae13d?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.s3APIMiddleware.func1.collectAPIStats.2({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/handler-utils.go:310 +0x4be
github.com/minio/minio/cmd.s3APIMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/api-router.go:246 +0x168
net/http.HandlerFunc.ServeHTTP(0xc00131a750?, {0x4d29200?, 0xc08d1fc000?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setBucketForwardingMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/generic-handlers.go:460 +0x2fc
net/http.HandlerFunc.ServeHTTP(0x7f9ff86d7a68?, {0x4d29200?, 0xc08d1fc000?}, 0xc08dcea520?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setUploadForwardingMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/generic-handlers.go:573 +0x1e5
net/http.HandlerFunc.ServeHTTP(0x2bfdaa0?, {0x4d29200?, 0xc08d1fc000?}, 0x39?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setRequestValidityMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/generic-handlers.go:443 +0x168d
net/http.HandlerFunc.ServeHTTP(0xc00131a6f0?, {0x4d29200?, 0xc08d1fc000?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setRequestLimitMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/generic-handlers.go:134 +0x64a
net/http.HandlerFunc.ServeHTTP(0x2d187d3?, {0x4d29200?, 0xc08d1fc000?}, 0x2291a0b?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setCrossDomainPolicyMiddleware.func1({0x4d29200?, 0xc08d1fc000?}, 0x1?)
	github.com/minio/minio/cmd/crossdomain-xml-handler.go:42 +0xf1
net/http.HandlerFunc.ServeHTTP(0xc00121f680?, {0x4d29200?, 0xc08d1fc000?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setBrowserRedirectMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/generic-handlers.go:164 +0x19c
net/http.HandlerFunc.ServeHTTP(0x2884760?, {0x4d29200?, 0xc08d1fc000?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.setAuthMiddleware.func1({0x4d29200, 0xc08d1fc000}, 0xc00121f680)
	github.com/minio/minio/cmd/auth-handler.go:658 +0x94b
net/http.HandlerFunc.ServeHTTP(0x4d2fc38?, {0x4d29200?, 0xc08d1fc000?}, 0x4d06410?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.httpTracerMiddleware.func1({0x4d29f80, 0xc08df361c0}, 0xc00121f560)
	github.com/minio/minio/cmd/http-tracer.go:89 +0x362
net/http.HandlerFunc.ServeHTTP(0x2aae0c0?, {0x4d29f80?, 0xc08df361c0?}, 0xa?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.addCustomHeadersMiddleware.func1({0x4d29f80, 0xc08df361c0}, 0xc00121f560)
	github.com/minio/minio/cmd/generic-handlers.go:538 +0x429
net/http.HandlerFunc.ServeHTTP(0xc08d908240?, {0x4d29f80?, 0xc08df361c0?}, 0x6b3e7f?)
	net/http/server.go:2166 +0x29
github.com/minio/mux.(*Router).ServeHTTP(0xc00122eb40, {0x4d29f80, 0xc08df361c0}, 0xc08d908240)
	github.com/minio/mux@v1.9.0/mux.go:214 +0x202
github.com/minio/minio/cmd.corsHandler.(*Cors).Handler.func2({0x4d29f80, 0xc08df361c0}, 0xc08d908240)
	github.com/rs/cors@v1.10.1/cors.go:281 +0x184
net/http.HandlerFunc.ServeHTTP(0x7f9fb12e9428?, {0x4d29f80?, 0xc08df361c0?}, 0x0?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/cmd.serverMain.func8.setCriticalErrorHandler.2({0x4d29f80?, 0xc08df361c0?}, 0x2d0ba01?)
	github.com/minio/minio/cmd/generic-handlers.go:562 +0x78
net/http.HandlerFunc.ServeHTTP(0x7f9fb12e9428?, {0x4d29f80?, 0xc08df361c0?}, 0x10?)
	net/http/server.go:2166 +0x29
github.com/minio/minio/internal/http.(*Server).Init.func1({0x4d29f80?, 0xc08df361c0?}, 0xc08df422c0?)
	github.com/minio/minio/internal/http/server.go:127 +0x24d
net/http.HandlerFunc.ServeHTTP(0x471f79?, {0x4d29f80?, 0xc08df361c0?}, 0xc08a4bcb68?)
	net/http/server.go:2166 +0x29
net/http.serverHandler.ServeHTTP({0xc0013b6630?}, {0x4d29f80?, 0xc08df361c0?}, 0x6?)
	net/http/server.go:3137 +0x8e
net/http.(*conn).serve(0xc08d0ea000, {0x4d2fc38, 0xc00124e2d0})
	net/http/server.go:2039 +0x5e8
created by net/http.(*Server).Serve in goroutine 93
	net/http/server.go:3285 +0x4b4
```


Discussion / hints:
yes, I was able to reproduce. I will check what is going on.

Fix patch:
diff --git a/cmd/handler-utils.go b/cmd/handler-utils.go
index 0b44fb6110258..79a06a94eceb6 100644
--- a/cmd/handler-utils.go
+++ b/cmd/handler-utils.go
@@ -296,8 +296,8 @@ func collectAPIStats(api string, f http.HandlerFunc) http.HandlerFunc {
 
 		bucket, _ := path2BucketObject(resource)
 
-		_, err = globalBucketMetadataSys.Get(bucket) // check if this bucket exists.
-		countBktStat := bucket != "" && bucket != minioReservedBucket && err == nil
+		meta, err := globalBucketMetadataSys.Get(bucket) // check if this bucket exists.
+		countBktStat := bucket != "" && bucket != minioReservedBucket && err == nil && !meta.Created.IsZero()
 		if countBktStat {
 			globalBucketHTTPStats.updateHTTPStats(bucket, api, nil)
 		}


Test patch: