From 806dedbdb4a45354548eccfccff783adf3935c1c Mon Sep 17 00:00:00 2001 From: Matthias Fischer Date: Thu, 1 Feb 2024 03:38:42 +0100 Subject: [PATCH] feat(imp):[#214] Move logging to business classes and remove some debug infos Moved logging from helper class to business class because this way the log entry shows the business class and not the helper class. --- .../util/concurrent/StopwatchUtils.java | 52 ------------------ .../DecentralDigitalTwinRegistryService.java | 54 ++++++++++++------- .../EndpointDataForConnectorsService.java | 23 ++++---- 3 files changed, 48 insertions(+), 81 deletions(-) delete mode 100644 irs-common/src/main/java/org/eclipse/tractusx/irs/common/util/concurrent/StopwatchUtils.java diff --git a/irs-common/src/main/java/org/eclipse/tractusx/irs/common/util/concurrent/StopwatchUtils.java b/irs-common/src/main/java/org/eclipse/tractusx/irs/common/util/concurrent/StopwatchUtils.java deleted file mode 100644 index 2c02d897b2..0000000000 --- a/irs-common/src/main/java/org/eclipse/tractusx/irs/common/util/concurrent/StopwatchUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * 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. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -package org.eclipse.tractusx.irs.common.util.concurrent; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.springframework.util.StopWatch; - -/** - * Utilities for stop watches. - */ -public final class StopwatchUtils { - - /** - * utility class, therefore private constructor - */ - private StopwatchUtils() { - super(); - } - - public static void startWatch(final Logger log, final StopWatch stopWatch, final String msg) { - stopWatch.start(msg); - log.info(msg); - } - - public static void stopWatch(final Logger log, final StopWatch stopWatch) { - stopWatch(log, stopWatch, ""); - } - - public static void stopWatch(final Logger log, final StopWatch stopWatch, final String messagePrefix) { - stopWatch.stop(); - final String prefix = StringUtils.isNotBlank(messagePrefix) ? messagePrefix + " - " : ""; - log.info("{}{} took {} ms", prefix, stopWatch.getLastTaskName(), stopWatch.getLastTaskTimeMillis()); - } -} diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java index 9e59b9b43f..eebf9836fc 100644 --- a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryService.java @@ -39,7 +39,6 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference; import org.eclipse.tractusx.irs.common.util.concurrent.ResultFinder; -import org.eclipse.tractusx.irs.common.util.concurrent.StopwatchUtils; import org.eclipse.tractusx.irs.component.assetadministrationshell.AssetAdministrationShellDescriptor; import org.eclipse.tractusx.irs.component.assetadministrationshell.IdentifierKeyValuePair; import org.eclipse.tractusx.irs.registryclient.DigitalTwinRegistryKey; @@ -85,8 +84,9 @@ public Collection fetchShells(final Collecti throws RegistryServiceException { final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, - LOGPREFIX_TO_BE_REMOVED_LATER + "Fetching shell(s) for %s key(s)".formatted(keys.size())); + final String msg = LOGPREFIX_TO_BE_REMOVED_LATER + "Fetching shell(s) for %s key(s)".formatted(keys.size()); + watch.start(msg); + log.info(msg); try { final var calledEndpoints = new HashSet(); @@ -114,7 +114,8 @@ public Collection fetchShells(final Collecti } } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -141,8 +142,10 @@ private CompletableFuture> fetchShellDe final Set calledEndpoints, final String bpn, final List keys) { final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, - LOGPREFIX_TO_BE_REMOVED_LATER + "Fetching %s shells for bpn '%s'".formatted(keys.size(), bpn)); + final String msg = + LOGPREFIX_TO_BE_REMOVED_LATER + "Fetching %s shells for bpn '%s'".formatted(keys.size(), bpn); + watch.start(msg); + log.info(msg); try { final var connectorEndpoints = connectorEndpointsService.fetchConnectorEndpoints(bpn); @@ -154,7 +157,8 @@ private CompletableFuture> fetchShellDe return fetchShellDescriptorsForConnectorEndpoints(keys, connectorEndpoints); } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -181,13 +185,15 @@ private List fetchShellDescriptorsForKey( final var logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + " fetchShellDescriptorsForKey - "; final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, - logPrefix + "Fetching shell descriptors for keys %s from endpoint '%s'".formatted(keys, - endpointDataReference.getEndpoint())); + final String msg = logPrefix + "Fetching shell descriptors for keys %s from endpoint '%s'".formatted(keys, + endpointDataReference.getEndpoint()); + watch.start(msg); + log.info(msg); try { return keys.stream().map(key -> fetchShellDescriptor(endpointDataReference, key)).toList(); } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -197,14 +203,16 @@ private AssetAdministrationShellDescriptor fetchShellDescriptor(final EndpointDa final var logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + " fetchShellDescriptor - "; final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, - logPrefix + "Retrieving AAS identification for DigitalTwinRegistryKey: '%s'".formatted(key)); + final String msg = logPrefix + "Retrieving AAS identification for DigitalTwinRegistryKey: '%s'".formatted(key); + watch.start(msg); + log.info(msg); try { final String aaShellIdentification = mapToShellId(endpointDataReference, key.shellId()); return decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(endpointDataReference, aaShellIdentification); } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -223,8 +231,10 @@ private String mapToShellId(final EndpointDataReference endpointDataReference, f final var logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + "mapToShellId - "; final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, logPrefix + "Mapping '%s' to shell ID for endpoint '%s'".formatted(key, - endpointDataReference.getEndpoint())); + final String msg = logPrefix + "Mapping '%s' to shell ID for endpoint '%s'".formatted(key, + endpointDataReference.getEndpoint()); + watch.start(msg); + log.info(msg); try { @@ -248,7 +258,8 @@ private String mapToShellId(final EndpointDataReference endpointDataReference, f return aaShellIdentification; } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -313,16 +324,19 @@ private Collection lookupShellIds(final String bpn, final EndpointDataRe final String logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + "lookupShellIds - "; final var watch = new StopWatch(); - StopwatchUtils.startWatch(log, watch, + final String msg = logPrefix + "Looking up shell IDs for bpn '%s' with endpointDataReference '%s'".formatted(bpn, - endpointDataReference)); + endpointDataReference); + watch.start(msg); + log.info(msg); try { return decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink( endpointDataReference, List.of(IdentifierKeyValuePair.builder().name("manufacturerId").value(bpn).build())).getResult(); } finally { - StopwatchUtils.stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/EndpointDataForConnectorsService.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/EndpointDataForConnectorsService.java index e9262c6820..b11c8b312e 100644 --- a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/EndpointDataForConnectorsService.java +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/EndpointDataForConnectorsService.java @@ -25,8 +25,6 @@ import static java.util.concurrent.CompletableFuture.supplyAsync; import static org.eclipse.tractusx.irs.common.util.concurrent.ResultFinder.LOGPREFIX_TO_BE_REMOVED_LATER; -import static org.eclipse.tractusx.irs.common.util.concurrent.StopwatchUtils.startWatch; -import static org.eclipse.tractusx.irs.common.util.concurrent.StopwatchUtils.stopWatch; import java.util.Collections; import java.util.List; @@ -54,19 +52,23 @@ public class EndpointDataForConnectorsService { public List> createFindEndpointDataForConnectorsFutures( final List connectorEndpoints) { - final String logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + "createFindEndpointDataForConnectorsFutures - "; + final var watch = new StopWatch(); + final String msg = "Creating futures to get EndpointDataReferences for endpoints: %s".formatted( + connectorEndpoints); + watch.start(msg); + log.info(msg); List> futures = Collections.emptyList(); try { - log.info(logPrefix + "Creating futures to get EndpointDataReferences for endpoints: {}", - connectorEndpoints); futures = connectorEndpoints.stream() .map(connectorEndpoint -> supplyAsync( () -> getEndpointReferenceForAsset(connectorEndpoint))) .toList(); return futures; } finally { - log.info(logPrefix + "Created {} futures", futures.size()); + log.info("Created {} futures", futures.size()); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } } @@ -75,8 +77,10 @@ private EndpointDataReference getEndpointReferenceForAsset(final String connecto final String logPrefix = LOGPREFIX_TO_BE_REMOVED_LATER + "getEndpointReferenceForAsset - "; final var watch = new StopWatch(); - startWatch(log, watch, - logPrefix + "Trying to retrieve EndpointDataReference for connector '%s'".formatted(connector)); + final String msg = + logPrefix + "Trying to retrieve EndpointDataReference for connector '%s'".formatted(connector); + watch.start(msg); + log.info(msg); try { return edcSubmodelFacade.getEndpointReferenceForAsset(connector, DT_REGISTRY_ASSET_TYPE, @@ -86,7 +90,8 @@ private EndpointDataReference getEndpointReferenceForAsset(final String connecto connector, e); throw new CompletionException(e.getMessage(), e); } finally { - stopWatch(log, watch); + watch.stop(); + log.info("{} took {} ms", watch.getLastTaskName(), watch.getLastTaskTimeMillis()); } }