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

feat(router): template expressions for access logs #1612

Draft
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

SkArchon
Copy link

@SkArchon SkArchon commented Feb 20, 2025

Motivation and Context

The goal is to extend the router to allow defining custom access log attributes by template expressions.

In order to do this the PR adds an "expression" field which can be set to process any log messages based on these exprlang expressions

However on top of this, the PR also goes ahead and adds the request error to the expressionContext.

This means that the user could now use expressions for access logs

access_logs:
  enabled: true
  router:
    fields:
    - key: "testthere"
      value_from:
        expression: "request.error ?? request.header.Get('X-WG-TRACE')"
  subgraphs:
    enabled: true
    fields:
      - key: "my-header"
        value_from:
          expression: "request.header.Get('X-WG-TRACE') == 'true' ? 'aa1' : 'aa2'"

This task was also previously mentioned in the PR: #1545 which was opened from a fork of mine, moved this to a branch in the repo so CI checks can run.

Checklist

  • I have discussed my proposed changes in an issue and have received approval to proceed.
  • I have followed the coding standards of the project.
  • Tests or benchmarks have been added or updated.
  • Documentation has been updated on https://github.com/wundergraph/cosmo-docs.
  • I have read the Contributors Guide.

alepane21 and others added 30 commits February 1, 2025 10:55
…ssion

# Conflicts:
#	router-tests/telemetry/telemetry_test.go
…he authentication code has been executed, and the other expressions as soon as possible
…s checks also auth usage in functions arguments
…tes-by-expression' into ale/eng-6258-custom-otel-attributes-by-expression
…s checks also auth usage in functions arguments
Copy link

github-actions bot commented Feb 20, 2025

Router image scan passed

✅ No security vulnerabilities found in image:

ghcr.io/wundergraph/cosmo/router:sha-5ffd3326bf2d0dede775a6f76708ab814b2afcd2

@SkArchon SkArchon force-pushed the milinda/eng-6254-add-the-capability-to-add-a-custom-log-field-through branch from 51ab90c to 58d494e Compare February 20, 2025 17:25
@endigma endigma self-requested a review February 21, 2025 09:37
Base automatically changed from ale/eng-6258-custom-otel-attributes-by-expression to main February 21, 2025 13:50
Comment on lines +265 to +280
func (c *requestContext) ResolveAnyExpressionWithWrappedError(expression *vm.Program) (any, error) {
// If an error exists already, wrap it and resolve the expression with the copied context
currExprContext := c.expressionContext.Request
if currExprContext.Error != nil {
copyExprContext := expr.Context{
Request: expr.Request{
Auth: currExprContext.Auth,
URL: currExprContext.URL,
Header: currExprContext.Header,
Error: &ExprWrapError{currExprContext.Error},
},
}
return expr.ResolveAnyExpression(expression, copyExprContext)
}
return expr.ResolveAnyExpression(expression, c.expressionContext)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest flipping the if conditional here and returning early in the simple case, to reduce nesting.

Comment on lines +282 to +284
func (c *requestContext) ResolveAnyExpression(expression *vm.Program) (any, error) {
return expr.ResolveAnyExpression(expression, c.expressionContext)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This function seems unused?

}

func GetAccessLogConfigExpressions(attributes []config.CustomAttribute) ([]ExpressionAttribute, error) {
exprSlice := make([]ExpressionAttribute, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exprSlice := make([]ExpressionAttribute, 0)
exprSlice := make([]ExpressionAttribute, 0, len(attributes))

Comment on lines +42 to +52
func CleanupExpressionAttributes(attributes []config.CustomAttribute) []config.CustomAttribute {
filtered := make([]config.CustomAttribute, 0, len(attributes))

for _, elem := range attributes {
if elem.ValueFrom == nil || elem.ValueFrom.Expression == "" {
filtered = append(filtered, elem)
}
}

return filtered
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is untested in access_log_expression_test.go

Copy link
Contributor

@endigma endigma left a comment

Choose a reason for hiding this comment

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

Looks like the changeset is a bit borked by GitHub, maybe try rebasing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants