Skip to content

Commit

Permalink
Update logger and tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Sep 11, 2024
2 parents 5bd99be + 5263016 commit 038c9a7
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 5 deletions.
37 changes: 37 additions & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,43 @@ const docTemplate = `{
}
}
}
},
"/test/tracing": {
"get": {
"description": "Test tracing to Tumblebug",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Test] Utility"
],
"summary": "Test tracing to Tumblebug",
"parameters": [
{
"type": "string",
"description": "Custom request ID (NOTE: It will be used as a trace ID.)",
"name": "x-request-id",
"in": "header"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMessage"
}
},
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/common.SimpleMessage"
}
}
}
}
}
},
"definitions": {
Expand Down
37 changes: 37 additions & 0 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,43 @@
}
}
}
},
"/test/tracing": {
"get": {
"description": "Test tracing to Tumblebug",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Test] Utility"
],
"summary": "Test tracing to Tumblebug",
"parameters": [
{
"type": "string",
"description": "Custom request ID (NOTE: It will be used as a trace ID.)",
"name": "x-request-id",
"in": "header"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.SimpleMessage"
}
},
"503": {
"description": "Service Unavailable",
"schema": {
"$ref": "#/definitions/common.SimpleMessage"
}
}
}
}
}
},
"definitions": {
Expand Down
24 changes: 24 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,30 @@ paths:
summary: Update a user
tags:
- '[Sample API] Users'
/test/tracing:
get:
consumes:
- application/json
description: Test tracing to Tumblebug
parameters:
- description: 'Custom request ID (NOTE: It will be used as a trace ID.)'
in: header
name: x-request-id
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/common.SimpleMessage'
"503":
description: Service Unavailable
schema:
$ref: '#/definitions/common.SimpleMessage'
summary: Test tracing to Tumblebug
tags:
- '[Test] Utility'
securityDefinitions:
BasicAuth:
type: basic
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdW
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
Expand Down
66 changes: 66 additions & 0 deletions pkg/api/rest/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ limitations under the License.
package common

import (
"fmt"
"net/http"

"github.com/go-playground/validator/v10"
"github.com/go-resty/resty/v2"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"

"github.com/cloud-barista/cb-tumblebug/src/core/model"
"github.com/cloud-barista/cm-beetle/pkg/config"
"github.com/cloud-barista/cm-beetle/pkg/core/common"
"github.com/cloud-barista/cm-beetle/pkg/logger"
)

var validate *validator.Validate
Expand Down Expand Up @@ -116,6 +121,67 @@ func RestCheckHTTPVersion(c echo.Context) error {
return c.JSON(http.StatusOK, &okMessage)
}

// RestGetTestTracingToTumblebug godoc
// @Summary Test tracing to Tumblebug
// @Description Test tracing to Tumblebug
// @Tags [Test] Utility
// @Accept json
// @Produce json
// @Param x-request-id header string false "Custom request ID (NOTE: It will be used as a trace ID.)"
// @Success 200 {object} SimpleMessage
// @Failure 503 {object} SimpleMessage
// @Router /test/tracing [get]
func RestGetTestTracing(c echo.Context) error {

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

// Initialize resty client with basic auth
client := resty.New()
apiUser := config.Tumblebug.API.Username
apiPass := config.Tumblebug.API.Password
client.SetBasicAuth(apiUser, apiPass)

// set tumblebug rest url
epTumblebug := config.Tumblebug.RestUrl

// Search and set a target VM spec
method := "GET"
url := fmt.Sprintf("%s/readyz", epTumblebug)

// Headers
headers := map[string]string{
"x-request-id": ctx.Value(logger.TraceIdKey).(string),
}

// Request body
tbReqt := common.NoBody

// Response body
tbResp := model.SimpleMsg{}

err := common.ExecuteHttpRequest(
client,
method,
url,
headers,
common.SetUseBody(tbReqt),
&tbReqt,
&tbResp,
common.VeryShortDuration,
)

resp := SimpleMessage{}
if err != nil {
log.Err(err).Msg("")
return c.JSON(http.StatusInternalServerError, resp)
}

resp.Message = tbResp.Message

return c.JSON(http.StatusOK, resp)
}

// /*
// // RestGetSwagger func is to get API document web.
// // RestGetSwagger godoc
Expand Down
18 changes: 14 additions & 4 deletions pkg/api/rest/middlewares/trancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,30 @@ func TracingMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
ctx = context.WithValue(ctx, logger.SpanIdKey, spanId)

// Create a logger with trace_id and span_id and store it in the context
logger := log.With().Str(string(logger.TraceIdKey), traceId).Str(string(logger.SpanIdKey), spanId).Logger()
ctx = logger.WithContext(ctx)
childLogger := log.With().Str(string(logger.TraceIdKey), traceId).Str(string(logger.SpanIdKey), spanId).Logger()
ctx = childLogger.WithContext(ctx)

// Set the context in the request
c.SetRequest(c.Request().WithContext(ctx))

// [Tracing log] when the request is received
log.Ctx(ctx).Info().Msg("[tracing] receive request")
traceLogger := logger.GetTraceLogger()

traceLogger.Trace().
Str(string(logger.TraceIdKey), traceId).
Str(string(logger.SpanIdKey), spanId).
Str("URI", c.Request().RequestURI).
Msg("[tracing] receive request")

// [Tracing log] before the response is sent
// Hooks: Before Response
c.Response().Before(func() {
// Log the request details
log.Ctx(ctx).Info().Msg("[tracing] send response")
traceLogger.Trace().
Str(string(logger.TraceIdKey), traceId).
Str(string(logger.SpanIdKey), spanId).
Str("URI", c.Request().RequestURI).
Msg("[tracing] send response")
})

// Call the next handler
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func RunServer(port string) {
gBeetle.GET("/readyz", rest_common.RestGetReadyz)
gBeetle.GET("/httpVersion", rest_common.RestCheckHTTPVersion)

// Test utility APIs
gBeetle.GET("/test/tracing", rest_common.RestGetTestTracing)

// Namespace API group
// gNamespace := gBeetle.Group("/ns")
// gNamespace.POST("", controller.RestPostNs)
Expand Down
12 changes: 11 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
var (
sharedLogFile *lumberjack.Logger
once sync.Once
traceLogger zerolog.Logger
)

type Config struct {
Expand Down Expand Up @@ -82,7 +83,7 @@ func NewLogger(config Config) *zerolog.Logger {
config.LogFilePath = "./log/app.log"
}
if config.MaxSize == 0 {
config.MaxSize = 100 // in MB
config.MaxSize = 1000 // in MB
}
if config.MaxBackups == 0 {
config.MaxBackups = 3
Expand Down Expand Up @@ -115,6 +116,10 @@ func NewLogger(config Config) *zerolog.Logger {
if err := os.Chmod(config.LogFilePath, 0644); err != nil {
log.Fatal().Msgf("Failed to change file permissions: %v", err)
}

// traceLogger = zerolog.New(sharedLogFile).Level(zerolog.TraceLevel).With().Timestamp().Caller().Logger()
traceLogger = zerolog.New(sharedLogFile).Level(zerolog.TraceLevel).With().Timestamp().Logger()
traceLogger.Hook(TracingHook{})
})

level := getLogLevel(config.LogLevel)
Expand All @@ -131,6 +136,11 @@ func NewLogger(config Config) *zerolog.Logger {
return logger
}

// GetTraceLogger returns the trace logger
func GetTraceLogger() *zerolog.Logger {
return &traceLogger
}

// getLogLevel returns the zerolog.Level based on the string level
func getLogLevel(logLevel string) zerolog.Level {
switch logLevel {
Expand Down

0 comments on commit 038c9a7

Please sign in to comment.