Skip to content

Commit

Permalink
refactor: adjust logging
Browse files Browse the repository at this point in the history
The type checks need to be with basic values for the logger to be able
to pick them up and print them.

RHCLOUD-36959
  • Loading branch information
MikelAlejoBR committed Jan 16, 2025
1 parent 82edaa0 commit bec66dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 10 additions & 10 deletions logger/logger_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,43 +82,43 @@ const httpHeadersCtxKey httpHeadersCtxKeyType = "http_headers"
func LogWithContext(ctx context.Context) *logrus.Entry {
logFields := logrus.Fields{}

if tenantId, ok := ctx.Value(tenantIdCtxKey).(tenantIdCtxKeyType); ok {
if tenantId, ok := ctx.Value(tenantIdCtxKey).(string); ok {
logFields["tenant_id"] = tenantId
}

if sourceId, ok := ctx.Value(sourceIdCtxKey).(sourceIdCtxKeyType); ok {
if sourceId, ok := ctx.Value(sourceIdCtxKey).(string); ok {
logFields["source_id"] = sourceId
}

if applicationId, ok := ctx.Value(applicationIdCtxKey).(applicationIdCtxKeyType); ok {
if applicationId, ok := ctx.Value(applicationIdCtxKey).(string); ok {
logFields["application_id"] = applicationId
}

if applicationType, ok := ctx.Value(applicationTypeCtxKey).(applicationTypeCtxKeyType); ok {
if applicationType, ok := ctx.Value(applicationTypeCtxKey).(string); ok {
logFields["application_type"] = applicationType
}

if authenticationId, ok := ctx.Value(authenticationIdCtxKey).(authenticationIdCtxKeyType); ok {
if authenticationId, ok := ctx.Value(authenticationIdCtxKey).(string); ok {
logFields["authentication_id"] = authenticationId
}

if resourceType, ok := ctx.Value(resourceTypeCtxKey).(resourceTypeCtxKeyType); ok {
if resourceType, ok := ctx.Value(resourceTypeCtxKey).(string); ok {
logFields["resource_type"] = resourceType
}

if resourceId, ok := ctx.Value(resourceIdCtxKey).(resourceIdCtxKeyType); ok {
if resourceId, ok := ctx.Value(resourceIdCtxKey).(string); ok {
logFields["resource_id"] = resourceId
}

if httpMethod, ok := ctx.Value(httpMethodCtxKey).(authenticationIdCtxKeyType); ok {
if httpMethod, ok := ctx.Value(httpMethodCtxKey).(string); ok {
logFields["http_method"] = httpMethod
}

if urlVar, ok := ctx.Value(urlCtxKey).(urlCtxKeyType); ok {
if urlVar, ok := ctx.Value(urlCtxKey).(string); ok {
logFields["url"] = urlVar
}

if httpHeaders, ok := ctx.Value(httpHeadersCtxKey).(httpHeadersCtxKeyType); ok {
if httpHeaders, ok := ctx.Value(httpHeadersCtxKey).(http.Header); ok {
logFields["http_headers"] = httpHeaders
}

Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func main() {
kafka.Consume(
reader,
func(msg kafka.Message) {
l.Log.Infof("Started processing message %s", string(msg.Value))
processSuperkeyRequest(msg)
l.Log.Infof("Finished processing message %s", string(msg.Value))
},
)
}()
Expand All @@ -94,7 +92,7 @@ func processSuperkeyRequest(msg kafka.Message) {
orgIdHeader := msg.GetHeader("x-rh-sources-org-id")

if identityHeader == "" && orgIdHeader == "" {
l.Log.WithFields(logrus.Fields{"kafka_message": msg}).Error(`Skipping Superkey request because no "x-rh-identity" or "x-rh-sources-org-id" headers were found`)
l.Log.WithFields(logrus.Fields{"kafka_message": string(msg.Value)}).Error(`Skipping Superkey request because no "x-rh-identity" or "x-rh-sources-org-id" headers were found`)

return
}
Expand Down

0 comments on commit bec66dc

Please sign in to comment.