Skip to content

Commit

Permalink
fix broker logger causing db errors (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier authored Jun 2, 2023
1 parent 0956153 commit 043f615
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ create type broker_event_status as enum (
'OK',

-- Failures
'ERROR',

-- E.g. refreshes, that resulted in no changes
'UNCHANGED'
'ERROR'
);

create table broker_event_log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,53 @@
public class BrokerEventLogger {

public void logConnectorUpdateSuccess(DSLContext dsl, String connectorEndpoint, ConnectorChangeTracker changes) {
var logEntry = connectorUpdateEntry(dsl, connectorEndpoint);
logEntry.setEventStatus(getConnectorUpdateStatus(changes));
logEntry.setUserMessage(changes.toString());
var logEntry = logEntry(
dsl,
BrokerEventType.CONNECTOR_UPDATED,
connectorEndpoint,
changes.toString()
);
logEntry.insert();
}

public void logConnectorUpdateFailure(DSLContext dsl, String connectorEndpoint, BrokerEventErrorMessage errorMessage) {
var logEntry = connectorUpdateEntry(dsl, connectorEndpoint);
var logEntry = logEntry(
dsl,
BrokerEventType.CONNECTOR_UPDATED,
connectorEndpoint,
errorMessage.message()
);
logEntry.setEventStatus(BrokerEventStatus.ERROR);
logEntry.setUserMessage(errorMessage.message());
logEntry.setErrorStack(errorMessage.stackTraceOrNull());
logEntry.insert();
}

public void logConnectorUpdateStatusChange(DSLContext dsl, String connectorEndpoint, ConnectorOnlineStatus status) {
var logEntry = connectorUpdateEntry(dsl, connectorEndpoint);
switch (status) {
case ONLINE:
logEntry.setUserMessage("Connector is online: " + connectorEndpoint);
logEntry.setEvent(BrokerEventType.CONNECTOR_STATUS_CHANGE_ONLINE);
break;
case OFFLINE:
logEntry.setUserMessage("Connector is offline: " + connectorEndpoint);
logEntry.setEvent(BrokerEventType.CONNECTOR_STATUS_CHANGE_OFFLINE);
break;
default:
throw new IllegalArgumentException("Unknown status: " + status + " for connector: " + connectorEndpoint);
}
var logEntry = switch (status) {
case ONLINE -> logEntry(
dsl,
BrokerEventType.CONNECTOR_STATUS_CHANGE_ONLINE,
connectorEndpoint,
"Connector is online: " + connectorEndpoint
);
case OFFLINE -> logEntry(
dsl,
BrokerEventType.CONNECTOR_STATUS_CHANGE_OFFLINE,
connectorEndpoint,
"Connector is offline: " + connectorEndpoint
);
default -> throw new IllegalArgumentException("Unknown status: " + status + " for connector: " + connectorEndpoint);
};
logEntry.insert();
}

private BrokerEventLogRecord connectorUpdateEntry(DSLContext dsl, String connectorEndpoint) {
private BrokerEventLogRecord logEntry(DSLContext dsl, BrokerEventType eventType, String connectorEndpoint, String userMessage) {
var logEntry = dsl.newRecord(Tables.BROKER_EVENT_LOG);
logEntry.setEvent(BrokerEventType.CONNECTOR_UPDATED);
logEntry.setEventStatus(BrokerEventStatus.OK);
logEntry.setEvent(eventType);
logEntry.setConnectorEndpoint(connectorEndpoint);
logEntry.setCreatedAt(OffsetDateTime.now());
logEntry.setUserMessage(userMessage);
return logEntry;
}

private BrokerEventStatus getConnectorUpdateStatus(ConnectorChangeTracker changes) {
if (changes.isEmpty()) {
return BrokerEventStatus.UNCHANGED;
}

return BrokerEventStatus.OK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 sovity GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.ext.brokerserver.services.logging;

import de.sovity.edc.ext.brokerserver.BrokerServerExtension;
import de.sovity.edc.ext.brokerserver.db.TestDatabase;
import de.sovity.edc.ext.brokerserver.db.TestDatabaseFactory;
import de.sovity.edc.ext.brokerserver.db.jooq.enums.ConnectorOnlineStatus;
import org.eclipse.edc.junit.annotations.ApiTest;
import org.eclipse.edc.junit.extensions.EdcExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.Map;

import static de.sovity.edc.ext.brokerserver.TestUtils.createConfiguration;

@ApiTest
@ExtendWith(EdcExtension.class)
public class BrokerEventLoggerTest {

@RegisterExtension
private static final TestDatabase TEST_DATABASE = TestDatabaseFactory.getTestDatabase();

@BeforeEach
void setUp(EdcExtension extension) {
extension.setConfiguration(createConfiguration(TEST_DATABASE, Map.of(
BrokerServerExtension.KNOWN_CONNECTORS, "https://example.com/ids/data",
BrokerServerExtension.NUM_THREADS, "0"
)));
}

@Test
void testDataOfferWriter_allSortsOfUpdates() {
TEST_DATABASE.testTransaction(dsl -> {
var brokerEventLogger = new BrokerEventLogger();

// Test that insertions insert required fields and don't cause DB errors
brokerEventLogger.logConnectorUpdateSuccess(dsl, "https://example.com/ids/data", new ConnectorChangeTracker());
brokerEventLogger.logConnectorUpdateFailure(dsl, "https://example.com/ids/data", new BrokerEventErrorMessage("Message", "Stacktrace"));
brokerEventLogger.logConnectorUpdateStatusChange(dsl, "https://example.com/ids/data", ConnectorOnlineStatus.ONLINE);
brokerEventLogger.logConnectorUpdateStatusChange(dsl, "https://example.com/ids/data", ConnectorOnlineStatus.OFFLINE);
});
}
}

0 comments on commit 043f615

Please sign in to comment.