|
| 1 | +/* |
| 2 | +© Copyright IBM Corporation 2025 |
| 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 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +type auditEvent struct { |
| 28 | + Timestamp string `json:"timestamp"` |
| 29 | + Event string `json:"event"` |
| 30 | + Pod string `json:"pod"` |
| 31 | + RemoteAddr string `json:"remote_addr"` |
| 32 | + Endpoint string `json:"endpoint"` |
| 33 | + Result string `json:"result"` |
| 34 | + StatusCode int `json:"status_code"` |
| 35 | + QueuemanagerName string `json:"queuemanager_name"` |
| 36 | +} |
| 37 | + |
| 38 | +// passthroughHandlerFuncWrapper does not modify the base handler |
| 39 | +func passthroughHandlerFuncWrapper(base http.HandlerFunc) http.HandlerFunc { |
| 40 | + return base |
| 41 | +} |
| 42 | + |
| 43 | +// newAuditingHandlerFuncWrapper generates a handlerFuncWrapper which allows creation of handlers that log audit entries for every request |
| 44 | +func newAuditingHandlerFuncWrapper(qmName string, logger logHandler) handlerFuncWrapper { |
| 45 | + return func(base http.HandlerFunc) http.HandlerFunc { |
| 46 | + return func(w http.ResponseWriter, req *http.Request) { |
| 47 | + podName, _ := os.Hostname() |
| 48 | + event := auditEvent{ |
| 49 | + Timestamp: time.Now().UTC().Format(time.RFC3339), |
| 50 | + Event: "metrics", |
| 51 | + Pod: podName, |
| 52 | + Endpoint: req.URL.RequestURI(), |
| 53 | + QueuemanagerName: qmName, |
| 54 | + RemoteAddr: req.RemoteAddr, |
| 55 | + } |
| 56 | + |
| 57 | + capWriter := newStatusCapturingResponseWriter(w) |
| 58 | + base(capWriter, req) |
| 59 | + statusCode := capWriter.statusCode |
| 60 | + event.StatusCode = statusCode |
| 61 | + event.Result = http.StatusText(statusCode) |
| 62 | + |
| 63 | + eventBytes, err := json.Marshal(event) |
| 64 | + if err != nil { |
| 65 | + logger.Append(fmt.Sprintf("Error writing audit log; next event may contain incomplete data: %s", err.Error()), false) |
| 66 | + fmt.Printf("Error constructing audit log event: %s\n", err.Error()) |
| 67 | + } |
| 68 | + logger.Append(string(eventBytes), false) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// wrappedHandler implements http.Handler using a stored http.HandlerFunc for the ServeHTTP method |
| 74 | +type wrappedHandler struct { |
| 75 | + handlerFunc http.HandlerFunc |
| 76 | +} |
| 77 | + |
| 78 | +func (wh wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 79 | + wh.handlerFunc(w, r) |
| 80 | +} |
| 81 | + |
| 82 | +// wrapHandler creates a new http.Handler with the function passed as wrapper around the base handler's ServeHTTP method, allowing augmentation of an existing http.Handler's ServeHTTP behaviour |
| 83 | +func wrapHandler(base http.Handler, wrapperFunc handlerFuncWrapper) wrappedHandler { |
| 84 | + return wrappedHandler{ |
| 85 | + handlerFunc: wrapperFunc(base.ServeHTTP), |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +type handlerFuncWrapper func(base http.HandlerFunc) http.HandlerFunc |
| 90 | + |
| 91 | +// statusCapturingResponseWriter captures the status code sent to the client |
| 92 | +type statusCapturingResponseWriter struct { |
| 93 | + http.ResponseWriter |
| 94 | + statusCode int |
| 95 | +} |
| 96 | + |
| 97 | +func newStatusCapturingResponseWriter(base http.ResponseWriter) *statusCapturingResponseWriter { |
| 98 | + return &statusCapturingResponseWriter{ |
| 99 | + ResponseWriter: base, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (c *statusCapturingResponseWriter) WriteHeader(statusCode int) { |
| 104 | + c.statusCode = statusCode |
| 105 | + c.ResponseWriter.WriteHeader(statusCode) |
| 106 | +} |
| 107 | + |
| 108 | +type logHandler interface { |
| 109 | + Append(messageLine string, deduplicateLine bool) |
| 110 | +} |
0 commit comments