public void handleEvent()

in appender/loki/src/main/java/org/apache/karaf/decanter/appender/loki/LokiAppender.java [72:110]


    public void handleEvent(Event event) {
        if (EventFilter.match(event, config)) {
            String log;
            if (marshaller != null) {
                log = marshaller.marshal(event);
            } else {
                StringBuilder builder = new StringBuilder();
                for (String innerKey : event.getPropertyNames()) {
                    builder.append(innerKey).append(":").append(toString(event.getProperty(innerKey))).append(" | ");
                }
                log = builder.toString();
            }
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");
                if (tenant != null) {
                    connection.setRequestProperty("X-Scope-OrgId", tenant);
                }
                if (username != null) {
                    String authentication = username + ":" + password;
                    byte[] encodedAuthentication = Base64.getEncoder().encode(authentication.getBytes(StandardCharsets.UTF_8));
                    String authenticationHeader = "Basic " + new String(encodedAuthentication);
                    connection.setRequestProperty("Authorization", authenticationHeader);
                }
                String jsonPush = "{\"streams\": [{ \"stream\": { \"job\": \"decanter\" }, \"values\": [ [ \"" + System.currentTimeMillis() * 1000L * 1000L + "\", \"" + log + "\" ] ] }]}";
                try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()))) {
                    writer.write(jsonPush);
                    writer.flush();
                }
                if (connection.getResponseCode() != 204) {
                    LOGGER.warn("Can't push to Loki ({}): {}", connection.getResponseCode(), connection.getResponseMessage());
                }
            } catch (Exception e) {
                LOGGER.warn("Error occurred while pushing to Loki", e);
            }
        }
    }