Skip to content

Conversation

@txabman42
Copy link
Contributor

@txabman42 txabman42 commented Nov 19, 2025

Contributing to #169

More info in ☝️ issue about why Hooks use interface{} as argument type:

func GenericExample[K comparable, V any](value T) map[K]V { ... }

func GenericHookBefore(ictx inst.HookContext, k interface{}, v interface{}) { ... }

func GenericHookAfter(ictx inst.HookContext, r interface{}) { ... }

This PR also add the support for parameter grouping in hooks, example:

func MyHookGenericBefore(ictx inst.HookContext, _, _ interface{}) { ... }

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


  • Make sure the code changes pass the pre-commit checks.
  • Sign-off your commit by using -s when doing git commit
  • Try to classify PRs for easy understanding of the type of changes, such as [Bugfix], [Feat], and [CI].
Detailed Checklist (Click to Expand)

Thank you for your contribution to semantic-router! Before submitting the pull request, please ensure the PR meets the following criteria. This helps us maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Please try to classify PRs for easy understanding of the type of changes. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Feat] for new features in the cluster (e.g., autoscaling, disaggregated prefill, etc.).
  • [Router] for changes to the vllm_router (e.g., routing algorithm, router observability, etc.).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • Pass all linter checks. Please use pre-commit to format your code. See README.md for installation.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Please include sufficient tests to ensure the change is stay correct and robust. This includes both unit tests and integration tests.

DCO and Signed-off-by

When contributing changes to this project, you must agree to the DCO. Commits must include a Signed-off-by: header which certifies agreement with the terms of the DCO.

Using -s with git commit will automatically add this header.

What to Expect for the Reviews

@txabman42 txabman42 requested a review from a team as a code owner November 19, 2025 15:04
@github-actions github-actions bot added the scope:feat A new feature being added label Nov 19, 2025
@pdelewski
Copy link
Member

Good Job!

Copy link
Contributor

@y1yang0 y1yang0 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

I didn't expect the implementation to be this clean and elegant

println("Hello, Generic World!", key, value)
return value
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Add test case func (recv *Recv[T]) GenericExample

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test added.

There were some extra changes needed to support this case:

  • Extract receiver type parameters
  • Set hook ctx params as a combination of receiver and function types
  • Preserve generics in trampolines

This new code would require some new 👀 63a9420

clause = getParamClause(idx, paramType)
methodGetParamBody.List = append(methodGetParamBody.List, clause)
idx++
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The SetParam/SetReturnVal function is intended to modify the parameters of the original function, but the current implementation doesn't allow this. We should rewrite it to panic (or something similar) to reject user calls and make it clear this operation is unsupported.

Copy link
Contributor Author

@txabman42 txabman42 Nov 21, 2025

Choose a reason for hiding this comment

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

This would require a extra bit of code. Do you see any direct approach?

WDYT about moving this to a new issue? This PR is starting to get big

docs/rules.md Outdated
This rule will inject `MyHookBefore` at the start of the `Example` function in the `main` package, and `MyHookAfter` at the end. The hook functions are located in the specified `path`.

**Generic Function Support:**
Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation needs to be updated to state that SetParam and SetReturnVal cannot be used with generic hook functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can document these limitations as part of #194. They should be more related to hooks than rules itself.

// in the linkname declaration (since linkname doesn't support generics)
for _, field := range paramTypes.List {
field.Type = replaceTypeParamsWithAny(field.Type, ip.targetFunc.Type.TypeParams)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

rectifyAnyType() already does this. Why do we need to repeat it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are not exactly the same.

For example:
func (t *T) Func1(p1 string, p2 int)

The receiver trait is marked as IsInterfaceAny and replaced here, so it ends up in:

func H3Before(ctx inst.HookContext, recv interface{}, p1 string, p2 int)

TestInstrumentation_Integration/method-receiver covers this case.

However, it could be simplified 58aa8e3

@txabman42 txabman42 requested a review from y1yang0 November 21, 2025 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope:feat A new feature being added

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants