Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
usfalami committed Jun 23, 2023
1 parent 23b4408 commit 303fd51
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
21 changes: 7 additions & 14 deletions src/main/java/org/usf/traceapi/core/RemoteTraceSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.web.client.RestTemplate;

Expand All @@ -25,32 +24,26 @@ public final class RemoteTraceSender implements TraceSender {

static final ScheduledExecutorService executor = newSingleThreadScheduledExecutor();

private final String host;
private final int delay;
private final TimeUnit unit;
private final TraceConfigurationProperties properties;
private final RestTemplate template;

public RemoteTraceSender(TraceConfig config) {
this(normalizeHost(config.getHost()), config.getDelay(), TimeUnit.valueOf(config.getUnit()), new RestTemplate());
public RemoteTraceSender(TraceConfigurationProperties properties) {
this(properties, new RestTemplate());
}

@Override
public void send(Metric trc) {
var uri = join("/", host, TRACE_ENDPOINT, endpointFor(trc));
executor.schedule(()-> template.put(uri, trc), delay, unit); //wait for sending response
var uri = join("/", properties.getHost(), TRACE_ENDPOINT, endpointFor(trc));
executor.schedule(()-> template.put(uri, trc), properties.getDelay(), properties.getUnit()); //wait for sending response
}

private static String endpointFor(@NonNull Metric trc) {
if(trc.getClass() == IncomingRequest.class) {
return INCOMING_ENDPOINT;
}
else if(trc.getClass() == OutcomingRequest.class) { //super after
else if(trc.getClass() == OutcomingRequest.class) {
return OUTCOMING_ENDPOINT;
}
throw new UnsupportedOperationException(trc.getClass().getSimpleName() + " : " + trc);
}

private static String normalizeHost(String host) {
return host.endsWith("/") ? host.substring(0, host.length()-1) : host;
}
}
22 changes: 0 additions & 22 deletions src/main/java/org/usf/traceapi/core/TraceConfig.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/usf/traceapi/core/TraceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/
@Configuration
@EnableConfigurationProperties(TraceConfig.class)
@EnableConfigurationProperties(TraceConfigurationProperties.class)
@ConditionalOnProperty(prefix = "api.tracing", name = "enabled", havingValue = "true")
public class TraceConfiguration implements WebMvcConfigurer {

Expand Down Expand Up @@ -61,7 +61,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
}

@Bean
public TraceSender sender(TraceConfig config) {
public TraceSender sender(TraceConfigurationProperties config) {
return config.getHost().isBlank()
? res-> {}
: new RemoteTraceSender(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.usf.traceapi.core;

import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.concurrent.TimeUnit;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Getter;
import lombok.Setter;

/**
*
* @author u$f
*
*/
@Getter
@Setter
@ConfigurationProperties(prefix = "api.tracing")
public final class TraceConfigurationProperties {

private String host = "";
private int delay = 5;
private TimeUnit unit = SECONDS;

public void setHost(String host) {
this.host = normalizeHost(host);
}

public void setUnit(String unit){
this.unit = TimeUnit.valueOf(unit.toLowerCase());
}

private static String normalizeHost(String host) {
return host.endsWith("/") ? host.substring(0, host.length()-1) : host;
}
}

0 comments on commit 303fd51

Please sign in to comment.