Skip to content

Commit

Permalink
Added betterstack
Browse files Browse the repository at this point in the history
  • Loading branch information
JexSrs committed May 13, 2024
1 parent 9be263a commit 95ad254
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -92,6 +94,50 @@ private String migrateUniversityName(String universityName) {
}
}

private static String getStackTraceAsString(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
throwable.printStackTrace(printWriter);
return stringWriter.toString();
}

private void logError(Exception exception, Throwable th, String university) {
OkHttpClient client = new OkHttpClient.Builder().build();

JSONObject payload = new JSONObject()
.put("environment", "production")
.put("from", "student-service-api")
.put("name", exception.getMessage())
.put("stack", getStackTraceAsString(exception))
.put("university", university)
.put("error", th != null ? th : exception);

if(th != null) {
payload.put("nested_error", th.getMessage())
.put("nested_stack", getStackTraceAsString(th));
}


RequestBody body = RequestBody.create(payload.toString(), MediaType.parse("application/json"));
Request req = new Request.Builder()
.url("https://in.logs.betterstack.com")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer HLYgHryob4L1jLayxDyu9REW")
.build();

client.newCall(req).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}

@Override
public void onResponse(Call call, Response response) {
}
});
}

public ResponseEntity<Object> get(String university, String system, LoginForm loginForm, boolean getDocuments) {
if (isGuest(loginForm))
return getGuestStudent();
Expand Down Expand Up @@ -122,6 +168,7 @@ public ResponseEntity<Object> get(String university, String system, LoginForm lo

// Print stack trace
th.printStackTrace();
logError(e, e.exception, university);

Sentry.captureException(th, scope -> {
scope.setTag("university", university);
Expand All @@ -137,6 +184,7 @@ public ResponseEntity<Object> get(String university, String system, LoginForm lo

// Print stack trace
th.printStackTrace();
logError(e, e.exception, university);

// Send to analytics
Sentry.captureException(th, scope -> {
Expand All @@ -155,6 +203,7 @@ public ResponseEntity<Object> get(String university, String system, LoginForm lo

// Print stack trace
th.printStackTrace();
logError(e, e.exception, university);

// if (e.document != null && e.document.length() > 0) {
// String docUrl = this.uploadDocuments(university, system, e.document);
Expand Down Expand Up @@ -188,6 +237,7 @@ public ResponseEntity<Object> get(String university, String system, LoginForm lo

// Print stack trace
th.printStackTrace();
logError(e, e.exception, university);

// Send to analytics
Sentry.captureException(th, scope -> {
Expand All @@ -201,6 +251,7 @@ public ResponseEntity<Object> get(String university, String system, LoginForm lo
} catch (Exception e) {
// Print stack trace
e.printStackTrace();
logError(e, null, university);

// Send to analytics
Sentry.captureException(e, scope -> {
Expand Down

0 comments on commit 95ad254

Please sign in to comment.