Skip to content
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

Add middleware to support tracing-like functionalities #125

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

yunkon-kim
Copy link
Member

This PR includes the following changes to support tracing-like functionality.

  • Add TracingHook to logger for logging ctx
  • Add middlewares of RequestIdAndDetailsIssue and Tracing
  • Enable x-request-id header param on APIs
  • Update API docs

Note

  • Middleware to support tracing-like functions.
  • The x-request-id, entered by the client, is used as traceId, and spanId is generated internally.

(Example 1)
: Call GET /readyz without x-request-id
image

logs

cm-beetle          | 2:30AM INF pkg/api/rest/middlewares/trancing.go:48 > [tracing] receive request spanId=1725935428254176774 traceId=1725935428254116874
cm-beetle          | 2:30AM INF pkg/api/rest/common/utility.go:86 > RestGetReadyz called spanId=1725935428254176774 traceId=1725935428254116874
cm-beetle          | 2:30AM INF pkg/api/rest/middlewares/trancing.go:54 > [tracing] send response spanId=1725935428254176774 traceId=1725935428254116874
cm-beetle          | 2:30AM INF pkg/api/rest/middlewares/zerologger.go:56 > request URI=/beetle/readyz bytes_in= bytes_out=33 id=1725935428254116874 latency_human="467.3µs" method=GET remote_ip=172.22.0.1 status=200

(Example 2)
: Call GET /readyz with x-request-id = myrequestid
image

logs

cm-beetle          | 2:31AM INF pkg/api/rest/middlewares/trancing.go:48 > [tracing] receive request spanId=1725935474277776693 traceId=myrequestid
cm-beetle          | 2:31AM INF pkg/api/rest/common/utility.go:86 > RestGetReadyz called spanId=1725935474277776693 traceId=myrequestid
cm-beetle          | 2:31AM INF pkg/api/rest/middlewares/trancing.go:54 > [tracing] send response spanId=1725935474277776693 traceId=myrequestid
cm-beetle          | 2:31AM INF pkg/api/rest/middlewares/zerologger.go:56 > request URI=/beetle/readyz bytes_in= bytes_out=33 id=myrequestid latency_human="177.1µs" method=GET remote_ip=172.22.0.1 status=200

Need in-depth tracing?
Pass ctx as a parameter to the target function, and include ctx when logging inside the function.

See an example - Readyz handler

func RestGetReadyz(c echo.Context) error {

	ctx := c.Request().Context()                    // Get context
	log.Ctx(ctx).Info().Msg("RestGetReadyz called") // Log ctx to trace

	message := SimpleMessage{}
	message.Message = "CM-Beetle is ready"
	if !common.SystemReady {
		message.Message = "CM-Beetle is NOT ready"
		return c.JSON(http.StatusServiceUnavailable, &message)
	}
	return c.JSON(http.StatusOK, &message)
}

* Add TracingHook to logger for logging ctx
* Add middlewares of RequestIdAndDetailsIssue and Tracing
* Enable x-request-id header param on APIs
* Update API docs
@yunkon-kim
Copy link
Member Author

/approve

@github-actions github-actions bot added the approved This PR is approved and will be merged soon. label Sep 10, 2024
@cb-github-robot cb-github-robot merged commit 5bd99be into cloud-barista:main Sep 10, 2024
2 checks passed
@yunkon-kim yunkon-kim deleted the 240910-11 branch September 10, 2024 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR is approved and will be merged soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants