Skip to content

Commit

Permalink
Merge pull request #5795 from HSLdevcom/http-error-logging
Browse files Browse the repository at this point in the history
Add trace log for HTTP error responses
  • Loading branch information
optionsome authored May 16, 2024
2 parents 5a5fd21 + fd8b26c commit dac395d
Show file tree
Hide file tree
Showing 25 changed files with 284 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opentripplanner.ext.ridehailing.service.oauth.UrlEncodedOAuthService;
import org.opentripplanner.framework.geometry.WgsCoordinate;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.framework.json.ObjectMappers;
import org.opentripplanner.transit.model.basic.Money;
import org.slf4j.Logger;
Expand Down Expand Up @@ -79,7 +80,7 @@ public UberService(RideHailingServiceParameters config) {
this.timeEstimateUri = timeEstimateUri;
this.bannedTypes = bannedTypes;
this.wheelchairAccessibleProductId = wheelchairAccessibleProductId;
this.otpHttpClient = new OtpHttpClient();
this.otpHttpClient = new OtpHttpClientFactory().create(LOG);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.opentripplanner.ext.siri.SiriFuzzyTripMatcher;
import org.opentripplanner.ext.siri.SiriTimetableSnapshotSource;
import org.opentripplanner.framework.application.ApplicationShutdownSupport;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.framework.retry.OtpRetry;
import org.opentripplanner.framework.retry.OtpRetryBuilder;
import org.opentripplanner.framework.text.FileSizeToTextConverter;
Expand Down Expand Up @@ -312,7 +312,8 @@ private void initializeData() {
}

private ByteString fetchInitialData() {
try (OtpHttpClient otpHttpClient = new OtpHttpClient()) {
try (OtpHttpClientFactory otpHttpClientFactory = new OtpHttpClientFactory()) {
var otpHttpClient = otpHttpClientFactory.create(LOG);
return otpHttpClient.getAndMap(
dataInitializationUrl,
initialGetDataTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.Duration;
import java.util.Optional;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.updater.spi.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -35,7 +36,7 @@ public SiriHttpLoader(
this.timeout = timeout;
this.requestHeaders = requestHeaders;
this.previewInterval = previewInterval;
this.otpHttpClient = new OtpHttpClient(timeout, timeout);
this.otpHttpClient = new OtpHttpClientFactory(timeout, timeout).create(LOG);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.opentripplanner.ext.siri.EntityResolver;
import org.opentripplanner.ext.siri.SiriFuzzyTripMatcher;
import org.opentripplanner.framework.application.ApplicationShutdownSupport;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
Expand Down Expand Up @@ -206,7 +206,8 @@ public String getConfigRef() {
protected Optional<ServiceDelivery> fetchInitialSiriData(URI uri) {
var headers = HttpHeaders.of().acceptApplicationXML().build().asMap();

try (OtpHttpClient otpHttpClient = new OtpHttpClient()) {
try (OtpHttpClientFactory otpHttpClientFactory = new OtpHttpClientFactory()) {
var otpHttpClient = otpHttpClientFactory.create(LOG);
var t1 = System.currentTimeMillis();
var siriOptional = otpHttpClient.executeAndMapOptional(
new HttpGet(uri),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import org.opentripplanner.framework.i18n.NonLocalizedString;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleType;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalPlace;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStation;
Expand All @@ -23,7 +24,7 @@ public class SmooveBikeRentalDataSource
extends GenericJsonDataSource<VehicleRentalPlace>
implements VehicleRentalDatasource {

private static final Logger log = LoggerFactory.getLogger(SmooveBikeRentalDataSource.class);
private static final Logger LOG = LoggerFactory.getLogger(SmooveBikeRentalDataSource.class);

public static final String DEFAULT_NETWORK_NAME = "smoove";

Expand All @@ -33,14 +34,14 @@ public class SmooveBikeRentalDataSource
private final RentalVehicleType vehicleType;

public SmooveBikeRentalDataSource(SmooveBikeRentalDataSourceParameters config) {
this(config, new OtpHttpClient());
this(config, new OtpHttpClientFactory());
}

public SmooveBikeRentalDataSource(
SmooveBikeRentalDataSourceParameters config,
OtpHttpClient otpHttpClient
OtpHttpClientFactory otpHttpClientFactory
) {
super(config.url(), "result", config.httpHeaders(), otpHttpClient);
super(config.url(), "result", config.httpHeaders(), otpHttpClientFactory.create(LOG));
networkName = config.getNetwork(DEFAULT_NETWORK_NAME);
vehicleType = RentalVehicleType.getDefaultType(networkName);
overloadingAllowed = config.overloadingAllowed();
Expand Down Expand Up @@ -76,7 +77,7 @@ protected VehicleRentalStation parseElement(JsonNode node) {
station.longitude = Double.parseDouble(coordinates[1].trim());
} catch (NumberFormatException e) {
// E.g. coordinates is empty
log.warn("Error parsing bike rental station {}", station.id, e);
LOG.warn("Error parsing bike rental station {}", station.id, e);
return null;
}
if (!node.path("style").asText().equals("Station on")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.opentripplanner.framework.i18n.LocalizedString;
import org.opentripplanner.framework.i18n.NonLocalizedString;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.framework.json.ObjectMappers;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingSpaces;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class BikelyUpdater implements DataSource<VehicleParking> {
.put("lonMax", 0)
.put("latMin", 0)
.put("latMax", 0);
private final OtpHttpClient httpClient = new OtpHttpClient();
private final OtpHttpClient httpClient = new OtpHttpClientFactory().create(LOG);
private final BikelyUpdaterParameters parameters;
private List<VehicleParking> lots;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.BiFunction;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientException;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingGroup;
import org.opentripplanner.transit.model.framework.FeedScopedId;
Expand All @@ -20,7 +21,7 @@

public class HslFacilitiesDownloader {

private static final Logger log = LoggerFactory.getLogger(HslFacilitiesDownloader.class);
private static final Logger LOG = LoggerFactory.getLogger(HslFacilitiesDownloader.class);
private static final ObjectMapper mapper = new ObjectMapper();

private final String jsonParsePath;
Expand All @@ -31,19 +32,20 @@ public class HslFacilitiesDownloader {
public HslFacilitiesDownloader(
String url,
String jsonParsePath,
BiFunction<JsonNode, Map<FeedScopedId, VehicleParkingGroup>, VehicleParking> facilitiesParser
BiFunction<JsonNode, Map<FeedScopedId, VehicleParkingGroup>, VehicleParking> facilitiesParser,
OtpHttpClientFactory otpHttpClientFactory
) {
this.url = url;
this.jsonParsePath = jsonParsePath;
this.facilitiesParser = facilitiesParser;
this.otpHttpClient = new OtpHttpClient();
this.otpHttpClient = otpHttpClientFactory.create(LOG);
}

public List<VehicleParking> downloadFacilities(
Map<FeedScopedId, VehicleParkingGroup> hubForPark
) {
if (url == null) {
log.warn("Cannot download updates, because url is null!");
LOG.warn("Cannot download updates, because url is null!");
return null;
}

Expand All @@ -55,17 +57,17 @@ public List<VehicleParking> downloadFacilities(
try {
return parseJSON(is, hubForPark);
} catch (IllegalArgumentException e) {
log.warn("Error parsing facilities from {}", url, e);
LOG.warn("Error parsing facilities from {}", url, e);
} catch (JsonProcessingException e) {
log.warn("Error parsing facilities from {} (bad JSON of some sort)", url, e);
LOG.warn("Error parsing facilities from {} (bad JSON of some sort)", url, e);
} catch (IOException e) {
log.warn("Error reading facilities from {}", url, e);
LOG.warn("Error reading facilities from {}", url, e);
}
return null;
}
);
} catch (OtpHttpClientException e) {
log.warn("Failed to get data from url {}", url);
LOG.warn("Failed to get data from url {}", url);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import java.util.function.Function;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientException;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingGroup;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HslHubsDownloader {

private static final Logger log = LoggerFactory.getLogger(HslHubsDownloader.class);
private static final Logger LOG = LoggerFactory.getLogger(HslHubsDownloader.class);
private static final ObjectMapper mapper = new ObjectMapper();

private final String jsonParsePath;
Expand All @@ -29,17 +30,18 @@ public class HslHubsDownloader {
public HslHubsDownloader(
String url,
String jsonParsePath,
Function<JsonNode, Map<FeedScopedId, VehicleParkingGroup>> hubsParser
Function<JsonNode, Map<FeedScopedId, VehicleParkingGroup>> hubsParser,
OtpHttpClientFactory otpHttpClientFactory
) {
this.url = url;
this.jsonParsePath = jsonParsePath;
this.hubsParser = hubsParser;
otpHttpClient = new OtpHttpClient();
otpHttpClient = otpHttpClientFactory.create(LOG);
}

public Map<FeedScopedId, VehicleParkingGroup> downloadHubs() {
if (url == null) {
log.warn("Cannot download updates, because url is null!");
LOG.warn("Cannot download updates, because url is null!");
return null;
}
try {
Expand All @@ -50,17 +52,17 @@ public Map<FeedScopedId, VehicleParkingGroup> downloadHubs() {
try {
return parseJSON(is);
} catch (IllegalArgumentException e) {
log.warn("Error parsing hubs from {}", url, e);
LOG.warn("Error parsing hubs from {}", url, e);
} catch (JsonProcessingException e) {
log.warn("Error parsing hubs from {} (bad JSON of some sort)", url, e);
LOG.warn("Error parsing hubs from {} (bad JSON of some sort)", url, e);
} catch (IOException e) {
log.warn("Error reading hubs from {}", url, e);
LOG.warn("Error reading hubs from {}", url, e);
}
return null;
}
);
} catch (OtpHttpClientException e) {
log.warn("Failed to get data from url {}", url);
LOG.warn("Failed to get data from url {}", url);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import java.util.Map;
import java.util.stream.Collectors;
import org.opentripplanner.framework.io.JsonDataListDownloader;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.model.calendar.openinghours.OpeningHoursCalendarService;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingGroup;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingSpaces;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingSpaces.VehicleParkingSpacesBuilder;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.updater.spi.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Vehicle parking updater class for https://github.com/HSLdevcom/parkandrideAPI format APIs. There
Expand All @@ -19,6 +22,8 @@
*/
public class HslParkUpdater implements DataSource<VehicleParking> {

private static final Logger LOG = LoggerFactory.getLogger(HslParkUpdater.class);

private static final String JSON_PARSE_PATH = "results";

private final HslFacilitiesDownloader facilitiesDownloader;
Expand All @@ -43,24 +48,28 @@ public HslParkUpdater(
new HslParkToVehicleParkingMapper(feedId, openingHoursCalendarService, parameters.timeZone());
vehicleParkingGroupMapper = new HslHubToVehicleParkingGroupMapper(feedId);
parkPatchMapper = new HslParkUtilizationToPatchMapper(feedId);
var otpHttpClientFactory = new OtpHttpClientFactory();
facilitiesDownloader =
new HslFacilitiesDownloader(
parameters.facilitiesUrl(),
JSON_PARSE_PATH,
vehicleParkingMapper::parsePark
vehicleParkingMapper::parsePark,
otpHttpClientFactory
);
hubsDownloader =
new HslHubsDownloader(
parameters.hubsUrl(),
JSON_PARSE_PATH,
vehicleParkingGroupMapper::parseHub
vehicleParkingGroupMapper::parseHub,
otpHttpClientFactory
);
utilizationsDownloader =
new JsonDataListDownloader<>(
parameters.utilizationsUrl(),
"",
parkPatchMapper::parseUtilization,
Map.of()
Map.of(),
otpHttpClientFactory.create(LOG)
);
this.facilitiesFrequencySec = parameters.facilitiesFrequencySec();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.Map;
import java.util.Optional;
import org.opentripplanner.ext.vehiclerentalservicedirectory.api.VehicleRentalServiceDirectoryFetcherParameters;
import org.opentripplanner.framework.io.OtpHttpClient;
import org.opentripplanner.framework.io.OtpHttpClientException;
import org.opentripplanner.framework.io.OtpHttpClientFactory;
import org.opentripplanner.framework.json.JsonUtils;
import org.opentripplanner.routing.linking.VertexLinker;
import org.opentripplanner.service.vehiclerental.VehicleRentalRepository;
Expand All @@ -35,16 +35,16 @@ public class VehicleRentalServiceDirectoryFetcher {

private final VertexLinker vertexLinker;
private final VehicleRentalRepository repository;
private final OtpHttpClient otpHttpClient;
private final OtpHttpClientFactory otpHttpClientFactory;

public VehicleRentalServiceDirectoryFetcher(
VertexLinker vertexLinker,
VehicleRentalRepository repository,
OtpHttpClient otpHttpClient
OtpHttpClientFactory otpHttpClientFactory
) {
this.vertexLinker = vertexLinker;
this.repository = repository;
this.otpHttpClient = otpHttpClient;
this.otpHttpClientFactory = otpHttpClientFactory;
}

public static List<GraphUpdater> createUpdatersFromEndpoint(
Expand All @@ -61,12 +61,12 @@ public static List<GraphUpdater> createUpdatersFromEndpoint(
}

int maxHttpConnections = sources.size();
var otpHttpClient = new OtpHttpClient(maxHttpConnections);
var otpHttpClientFactory = new OtpHttpClientFactory(maxHttpConnections);

var serviceDirectory = new VehicleRentalServiceDirectoryFetcher(
vertexLinker,
repository,
otpHttpClient
otpHttpClientFactory
);
return serviceDirectory.createUpdatersFromEndpoint(parameters, sources);
}
Expand Down Expand Up @@ -146,15 +146,16 @@ private VehicleRentalUpdater fetchAndCreateUpdater(

var dataSource = VehicleRentalDataSourceFactory.create(
vehicleRentalParameters.sourceParameters(),
otpHttpClient
otpHttpClientFactory
);
return new VehicleRentalUpdater(vehicleRentalParameters, dataSource, vertexLinker, repository);
}

private static JsonNode listSources(VehicleRentalServiceDirectoryFetcherParameters parameters) {
JsonNode node;
URI url = parameters.getUrl();
try (OtpHttpClient otpHttpClient = new OtpHttpClient()) {
try {
var otpHttpClient = new OtpHttpClientFactory().create(LOG);
node = otpHttpClient.getAndMapAsJsonNode(url, Map.of(), new ObjectMapper());
} catch (OtpHttpClientException e) {
LOG.warn("Error fetching list of vehicle rental endpoints from {}", url, e);
Expand Down
Loading

0 comments on commit dac395d

Please sign in to comment.