Users can create their own settings, for example, a setting for probabilistic sampling, by annotating a boolean-returning method in their event with @SettingDefinition.
public class FooEvent extends Event {
String domain;
@SettingDefinition
public boolean sample(ProbalisticSetting ps) {
return ps,shouldSample();
}
}
The method is executed when the commit() method is invoked, and if it returns true, and the duration exceeds the threshold and other user-defined settings also return true, the event is written to a thread-local JFR buffer.
The problem arises when shouldCommit() is used to avoid an expensive operation, for example:
FooEvent foo = new FooEvent();
foo.begin();
...
foo.end();
if (foo.shouldCommit()) {
int index = email.indexOf("@");
foo.domain = index == -1 ? null : email.substring(index + 1);
foo.commit();
}
When commit() is called, the shouldCommit() method is called internally, but it may not return the same result as the first time, which means probabilistic sampling will not work as expected.
One way to avoid this issue is to store the result of shouldCommit() so it can be reused by commit().
public class FooEvent extends Event {
String domain;
@SettingDefinition
public boolean sample(ProbalisticSetting ps) {
return ps,shouldSample();
}
}
The method is executed when the commit() method is invoked, and if it returns true, and the duration exceeds the threshold and other user-defined settings also return true, the event is written to a thread-local JFR buffer.
The problem arises when shouldCommit() is used to avoid an expensive operation, for example:
FooEvent foo = new FooEvent();
foo.begin();
...
foo.end();
if (foo.shouldCommit()) {
int index = email.indexOf("@");
foo.domain = index == -1 ? null : email.substring(index + 1);
foo.commit();
}
When commit() is called, the shouldCommit() method is called internally, but it may not return the same result as the first time, which means probabilistic sampling will not work as expected.
One way to avoid this issue is to store the result of shouldCommit() so it can be reused by commit().