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

fix(Counter): Resolve IndexOutOfBoundsException (see #14) #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.List;

import org.jboss.logging.Logger;
import org.keycloak.events.Event;
Expand Down Expand Up @@ -93,9 +94,12 @@ private synchronized File getOrCreateCounterFile(String fileName) {

private void increaseCounter(File counterFile) {
try {
Long count = Long.parseLong(Files.readAllLines(counterFile.toPath()).get(0));
count++;
Files.write(counterFile.toPath(), count.toString().getBytes(Charset.forName("UTF-8")));
List<String> lines = Files.readAllLines(counterFile.toPath());
if (lines != null && lines.size() > 0){
Copy link
Member

Choose a reason for hiding this comment

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

Hi Martin, in theory, this condition should never be false - increaseCounter is never called before the file was created (see callsite) So the error situation you are seeing seems to be some kind of illegal state and I have no clue how this can even occur.

I know that the change you propose will fix the errors you see, but also the plugin will just silently fail to write the metric - making the whole metric collection inherently unreliable, with a margin of error we couldn't even quantify. Only thing I can imagine (if we find no proper way to fix the actual faulty condition) would be a config flag "ignore errors" or something like this.

What do you think? Happy to here your proposals!

Long count = Long.parseLong(lines.get(0));
count++;
Files.write(counterFile.toPath(), count.toString().getBytes(Charset.forName("UTF-8")));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down