Skip to content

Commit d6ec219

Browse files
committed
Refactor code and add more info logs
1 parent c46136a commit d6ec219

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

src/main/java/com/github/tadayosi/torchserve/client/Configuration.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@ public class Configuration {
2525
public static final String METRICS_ADDRESS = "metrics.address";
2626
public static final String METRICS_PORT = "metrics.port";
2727

28-
private Optional<String> inferenceKey;
29-
private Optional<String> inferenceAddress;
30-
private Optional<Integer> inferencePort;
28+
private final Optional<String> inferenceKey;
29+
private final Optional<String> inferenceAddress;
30+
private final Optional<Integer> inferencePort;
3131

32-
private Optional<String> managementKey;
33-
private Optional<String> managementAddress;
34-
private Optional<Integer> managementPort;
32+
private final Optional<String> managementKey;
33+
private final Optional<String> managementAddress;
34+
private final Optional<Integer> managementPort;
3535

36-
private Optional<String> metricsAddress;
37-
private Optional<Integer> metricsPort;
36+
private final Optional<String> metricsAddress;
37+
private final Optional<Integer> metricsPort;
3838

3939
private Configuration() {
4040
Properties props = loadProperties();
4141

4242
this.inferenceKey = loadProperty(INFERENCE_KEY, props);
4343
this.inferenceAddress = loadProperty(INFERENCE_ADDRESS, props);
4444
this.inferencePort = loadProperty(INFERENCE_PORT, props).map(Integer::parseInt);
45+
4546
this.managementKey = loadProperty(MANAGEMENT_KEY, props);
4647
this.managementAddress = loadProperty(MANAGEMENT_ADDRESS, props);
4748
this.managementPort = loadProperty(MANAGEMENT_PORT, props).map(Integer::parseInt);
49+
4850
this.metricsAddress = loadProperty(METRICS_ADDRESS, props);
4951
this.metricsPort = loadProperty(METRICS_PORT, props).map(Integer::parseInt);
5052
}
@@ -66,9 +68,11 @@ static Properties loadProperties() {
6668
*/
6769
static Optional<String> loadProperty(String key, Properties properties) {
6870
String tsc4jKey = TSC4J_PREFIX + key;
69-
return Optional.ofNullable(System.getProperty(tsc4jKey))
71+
Optional<String> value = Optional.ofNullable(System.getProperty(tsc4jKey))
7072
.or(() -> Optional.ofNullable(System.getenv(tsc4jKey.toUpperCase().replace(".", "_"))))
7173
.or(() -> Optional.ofNullable(properties.getProperty(key)));
74+
LOG.debug("Loaded property {}: {}", key, value.orElse(null));
75+
return value;
7276
}
7377

7478
public static Configuration load() {

src/main/java/com/github/tadayosi/torchserve/client/TorchServeClient.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ public class TorchServeClient {
1212
private final Management management;
1313
private final Metrics metrics;
1414

15-
private TorchServeClient() {
16-
this(new DefaultInference(), new DefaultManagement(), new DefaultMetrics());
17-
}
18-
1915
private TorchServeClient(Inference inference, Management management, Metrics metrics) {
2016
this.inference = inference;
2117
this.management = management;
@@ -98,19 +94,20 @@ public Builder metricsPort(Integer port) {
9894
}
9995

10096
public TorchServeClient build() {
97+
System.out.println(inferencePort.get());
10198
DefaultInference inference = inferenceAddress.map(DefaultInference::new)
102-
.orElse(inferencePort.map(DefaultInference::new)
103-
.orElse(new DefaultInference()));
99+
.or(() -> inferencePort.map(DefaultInference::new))
100+
.orElse(new DefaultInference());
104101
inferenceKey.ifPresent(inference::setAuthToken);
105102

106103
DefaultManagement management = managementAddress.map(DefaultManagement::new)
107-
.orElse(managementPort.map(DefaultManagement::new)
108-
.orElse(new DefaultManagement()));
104+
.or(() -> managementPort.map(DefaultManagement::new))
105+
.orElse(new DefaultManagement());
109106
managementKey.ifPresent(management::setAuthToken);
110107

111108
DefaultMetrics metrics = metricsAddress.map(DefaultMetrics::new)
112-
.orElse(metricsPort.map(DefaultMetrics::new)
113-
.orElse(new DefaultMetrics()));
109+
.or(() -> metricsPort.map(DefaultMetrics::new))
110+
.orElse(new DefaultMetrics());
114111
return new TorchServeClient(inference, management, metrics);
115112
}
116113
}

src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java

+5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import com.github.tadayosi.torchserve.client.model.Api;
77
import com.github.tadayosi.torchserve.client.model.ApiException;
88
import com.github.tadayosi.torchserve.client.model.Response;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
911

1012
public class DefaultInference implements Inference {
1113

14+
private static final Logger LOG = LoggerFactory.getLogger(DefaultInference.class);
15+
1216
private final DefaultApi api;
1317

1418
public DefaultInference() {
@@ -22,6 +26,7 @@ public DefaultInference(int port) {
2226
public DefaultInference(String address) {
2327
ApiClient client = new ApiClient().setBasePath(address);
2428
this.api = new DefaultApi(client);
29+
LOG.info("Inference API address: {}", address);
2530
}
2631

2732
public void setAuthToken(String token) {

src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultManagement.java

+5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
import com.github.tadayosi.torchserve.client.model.Response;
1414
import com.github.tadayosi.torchserve.client.model.SetAutoScaleOptions;
1515
import com.github.tadayosi.torchserve.client.model.UnregisterModelOptions;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1618

1719
public class DefaultManagement implements Management {
1820

21+
private static final Logger LOG = LoggerFactory.getLogger(DefaultManagement.class);
22+
1923
private final DefaultApi api;
2024

2125
public DefaultManagement() {
@@ -29,6 +33,7 @@ public DefaultManagement(int port) {
2933
public DefaultManagement(String address) {
3034
ApiClient client = new ApiClient().setBasePath(address);
3135
this.api = new DefaultApi(client);
36+
LOG.info("Management API address: {}", address);
3237
}
3338

3439
public void setAuthToken(String token) {

src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultMetrics.java

+5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import com.github.tadayosi.torchserve.client.metrics.api.DefaultApi;
55
import com.github.tadayosi.torchserve.client.metrics.invoker.ApiClient;
66
import com.github.tadayosi.torchserve.client.model.ApiException;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
79

810
public class DefaultMetrics implements Metrics {
911

12+
private static final Logger LOG = LoggerFactory.getLogger(DefaultMetrics.class);
13+
1014
private final DefaultApi api;
1115

1216
public DefaultMetrics() {
@@ -20,6 +24,7 @@ public DefaultMetrics(int port) {
2024
public DefaultMetrics(String address) {
2125
ApiClient client = new ApiClient().setBasePath(address);
2226
this.api = new DefaultApi(client);
27+
LOG.info("Metrics API address: {}", address);
2328
}
2429

2530
@Override

src/test/java/com/github/tadayosi/torchserve/client/ConfigurationTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.tadayosi.torchserve.client;
22

3-
import com.github.tadayosi.torchserve.client.model.ApiException;
43
import org.junit.jupiter.api.Test;
54

65
import static org.junit.jupiter.api.Assertions.assertEquals;

0 commit comments

Comments
 (0)