Skip to content

Conversation

@DmitrySadchikov
Copy link
Contributor

Реализация задачи #144

@DmitrySadchikov DmitrySadchikov changed the title Mdc java aspect implementation Mdc aspect implementation Oct 13, 2025
@GoodforGod GoodforGod linked an issue Oct 13, 2025 that may be closed by this pull request
@GoodforGod GoodforGod added the new feature New feature request label Oct 13, 2025
@GoodforGod GoodforGod added the module: telemetry Related to Telemetry module label Oct 23, 2025
GoodforGod
GoodforGod previously approved these changes Oct 29, 2025
final Set<String> keys = new HashSet<>();
for (AnnotationMirror annotation : methodAnnotations) {
final String key = extractParameter(annotation, String.class, "key")
.orElseThrow(() -> new ProcessingErrorException("@Mdc annotation must have 'key' attribute", annotation.getAnnotationType().asElement()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Раз уж он обязательный, то может быть и сделать его обязательным в аннотации и пусть его проверяет компилятор.

Copy link
Contributor

Choose a reason for hiding this comment

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

вроде договорились что он в итоге не обязательный тк имя берется как имя аргумента

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Тут смысл в том, что для аннотации над методом оба параметры обязательны. А для аннотации на аргументе уже оба необязательны:

@Mdc // непонятно, что брать в качестве ключа и значения?
void test() {...}

// здесь наоборот, все понятно, вызовем MDC.put("name", name);
void test(@Mdc String name) {...}

}

private static <T> Optional<T> extractParameter(AnnotationMirror annotation, Class<T> type, String name) {
return annotation.getElementValues()
Copy link
Contributor

Choose a reason for hiding this comment

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

AnnotationUtils.parseAnnotationValueWithoutDefault

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Поправил

currentContextBuilder.addStatement("var __$N = $N.get($S)", key, MDC_CONTEXT_VAR_NAME, key);
}
if (value.startsWith("${") && value.endsWith("}")) {
fillMdcBuilder.addStatement("$T.put($S, $N)", mdc, key, value.substring(2, value.length() - 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

$N - это для имени переменной, а у вас там судя по всему свободный код, для него есть $L.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Поправил

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: telemetry Related to Telemetry module new feature New feature request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@Mdc annotation

3 participants