Skip to content

Feat/trace generator #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

oleksandrkhmil
Copy link
Collaborator

@oleksandrkhmil oleksandrkhmil commented May 23, 2025

🎫 Ticket Reference

No ticket.

📝 Changes

Refactor logging system. Introduced request trace.

POST /posts loggs

{
  "time": "2025-05-23T08:13:49.11079111Z",
  "level": "DEBUG",
  "source": {
    "function": "github.com/nix-united/golang-echo-boilerplate/internal/server/middleware.requestDebugger.handle.func1",
    "file": "/app/internal/server/middleware/request_debugger.go",
    "line": 60
  },
  "msg": "Request/response data",
  "application": "my_awesome_app",
  "hostname": "bf3764f3455c",
  "request_body": {
    "title": "my unique post",
    "content": "some useful information"
  },
  "response_body": { "code": 201, "message": "Post successfully created" },
  "trace": { "trace": "0196fc34-9eaf-7c10-b10c-81ee41b12872", "index": 1 }
}
{
  "time": "2025-05-23T08:13:49.110867959Z",
  "level": "INFO",
  "source": {
    "function": "github.com/nix-united/golang-echo-boilerplate/internal/server/middleware.requestLogger.handle-fm.requestLogger.handle.func1",
    "file": "/app/internal/server/middleware/request_logger.go",
    "line": 53
  },
  "msg": "Request",
  "application": "my_awesome_app",
  "hostname": "bf3764f3455c",
  "http": { "method": "POST", "status": 201, "path": "/posts" },
  "trace": { "trace": "0196fc34-9eaf-7c10-b10c-81ee41b12872", "index": 2 }
}

Example for 500 status code

{
  "time": "2025-05-23T08:08:40.51182056Z",
  "level": "ERROR",
  "source": {
    "function": "github.com/nix-united/golang-echo-boilerplate/internal/server/middleware.requestLogger.handle-fm.requestLogger.handle.func1",
    "file": "/app/internal/server/middleware/request_logger.go",
    "line": 53
  },
  "msg": "Request",
  "application": "my_awesome_app",
  "hostname": "bf3764f3455c",
  "http": {
    "method": "POST",
    "status": 500,
    "path": "/posts",
    "error": "handle request with request debugger: code=400, message=missing or malformed jwt, internal=missing value in request header"
  },
  "trace": { "trace": "0196fc2f-e93f-78a1-a736-bef0a1771708", "index": 3 }
}

}

if strings.HasPrefix(c.Response().Header().Get(echo.HeaderContentType), "application/json") {
return json.RawMessage(storer.storedResponse)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.RawMessage provided a little bit better formatting for JSON values than just string.

Example

{
  message: "some message"
}

For string

{\n\"message\":\"some message\"\n}

}

type TraceStarter struct {
newUUID func() (uuid.UUID, error)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to pass request trace ID in HTTP headers to connect requests between services

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge with the current solution now.

@oleksandrkhmil
Copy link
Collaborator Author

oleksandrkhmil commented May 23, 2025

Also, it is possible to achieve this with the built-in echo middleware, but it does not log the request body and requires workarounds to achieve the same functionality.

Check please if we want to use it instead:

https://echo.labstack.com/docs/middleware/logger#examples

Here's example logg from it with tracer (it can be customized)

{
  "time": "2025-05-23T08:08:40.511738769Z",
  "level": "INFO",
  "source": {
    "function": "github.com/nix-united/golang-echo-boilerplate/internal/server/routes.ConfigureRoutes.func2",
    "file": "/app/internal/server/routes/routes.go",
    "line": 52
  },
  "msg": "Received request",
  "application": "my_awesome_app",
  "hostname": "bf3764f3455c",
  "val": {
    "StartTime": "2025-05-23T08:08:40.511565142Z",
    "Latency": 169407,
    "Protocol": "HTTP/1.1",
    "RemoteIP": "172.18.0.1",
    "Host": "localhost:7788",
    "Method": "POST",
    "URI": "/posts",
    "URIPath": "/posts",
    "RoutePath": "/posts",
    "RequestID": "",
    "Referer": "",
    "UserAgent": "PostmanRuntime/7.44.0",
    "Status": 500,
    "Error": {},
    "ContentLength": "60",
    "ResponseSize": 36,
    "Headers": null,
    "QueryParams": null,
    "FormValues": null
  },
  "http": {
    "request_body": "{\n    \"title\": \"post title\",\n    \"content\": \"post content\"\n}"
  },
  "trace": { "trace": "0196fc2f-e93f-78a1-a736-bef0a1771708", "index": 2 }
}

@tural-nixs tural-nixs self-requested a review May 30, 2025 12:46
@eugene-burenkov eugene-burenkov merged commit d75a3da into nix-united:master Jun 6, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants