Browse Source

handling health chacks in logs

Richard Köhl 2 năm trước cách đây
mục cha
commit
537197808d
2 tập tin đã thay đổi với 15 bổ sung4 xóa
  1. 2 1
      docker-compose.yml
  2. 13 3
      src/interfaces/controllers/controller.class.ts

+ 2 - 1
docker-compose.yml

@@ -9,12 +9,13 @@ services:
       - APPLICATION_NAME=${APPLICATION_NAME-My Deno Application}
       - VERSION=${VERSION-0.1.0}
       - DEBUG_LEVEL=${DEBUG_LEVEL-INFO}
+      - DOCKER_USER_AGENT=${DOCKER_USER_AGENT-docker wget}
       - GREETING=${GREETING-hello world!}
     volumes:
       - ./src:/app/src:ro
     command: ["deno", "task", "dev"]
     healthcheck:
-      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:1993/health"]
+      test: ["CMD", "wget", "--user-agent=${DOCKER_USER_AGENT-docker wget}", "--no-verbose", "--tries=1", "--spider", "http://localhost:1993/health"]
       interval: 10s
       timeout: 2s
       retries: 3

+ 13 - 3
src/interfaces/controllers/controller.class.ts

@@ -33,16 +33,21 @@ export default class Controller {
     if (options.json) {
       response.headers.set('Content-Type', 'application/json');
     }
-
+    const userAgent = req.headers.get('User-Agent') ?? '-';
     const logMessage = this.getLogMessage(
       req.method,
       url.pathname,
       response.status,
       output.byteLength,
+      userAgent,
       userName,
     );
     if (isSuccessfulStatus(response.status)) {
-      logger.info(logMessage);
+      if (this.isHealthCheck(userAgent)) {
+        logger.debug(logMessage);
+      } else {
+        logger.info(logMessage);
+      }
     } else {
       logger.error(logMessage);
     }
@@ -79,9 +84,14 @@ export default class Controller {
     path: string,
     status: number,
     length: number,
+    userAgent: string,
     userName?: string,
   ) {
     const user = userName ? userName + ' ' : '';
-    return `${method} ${path} ${user}${status} ${length}`;
+    return `${method} ${path} ${user}${status} ${length} (${userAgent})`;
+  }
+
+  private static isHealthCheck(userAgent: string): boolean {
+    return userAgent === Deno.env.get('DOCKER_USER_AGENT');
   }
 }