Skip to content

Commit

Permalink
Unngår kall fra sensu-klient når kjører lokalt, da det bare skaper st… (
Browse files Browse the repository at this point in the history
#607)

* Unngår kall fra sensu-klient når kjører lokalt, da det bare skaper støy i loggene

* Fix for test

* Fix for test
  • Loading branch information
tendestad authored Jan 12, 2021
1 parent 54c83b8 commit 9b8fac4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import no.nav.vedtak.apptjeneste.AppServiceHandler;
import no.nav.vedtak.konfig.KonfigVerdi;
import no.nav.vedtak.log.mdc.MDCOperations;
import no.nav.vedtak.util.env.Environment;

@ApplicationScoped
public class SensuKlient implements AppServiceHandler {
Expand All @@ -40,7 +41,7 @@ public class SensuKlient implements AppServiceHandler {

@Inject
public SensuKlient(@KonfigVerdi(value = "sensu.host", defaultVerdi = "sensu.nais") String sensuHost,
@KonfigVerdi(value = "sensu.port", defaultVerdi = "3030") Integer sensuPort) {
@KonfigVerdi(value = "sensu.port", defaultVerdi = "3030") Integer sensuPort) {
this.sensuHost = sensuHost;
this.sensuPort = sensuPort;
}
Expand All @@ -49,7 +50,9 @@ public void logMetrics(SensuEvent metrics) {
logMetrics(List.of(metrics));
}

/** Sender et set med events samlet til Sensu. */
/**
* Sender et set med events samlet til Sensu.
*/
public void logMetrics(List<SensuEvent> metrics) {
var event = SensuEvent.createBatchSensuRequest(metrics);
logMetrics(event);
Expand Down Expand Up @@ -86,7 +89,7 @@ public void logMetrics(SensuEvent.SensuRequest sensuRequest) {
// ink. SocketException
if (rounds <= 0) {
LOG.warn("Feil ved tilkobling til metrikkendepunkt. Kan ikke publisere melding fra callId[" + callId + "]: "
+ jsonForEx, ex);
+ jsonForEx, ex);
break;
}
} catch (Exception ex) {
Expand Down Expand Up @@ -136,6 +139,14 @@ private synchronized Socket establishSocketConnectionIfNeeded() throws Exception

@Override
public synchronized void start() {
if (Environment.current().isLocal()) {
LOG.info("Kjører lokalt, kobler ikke opp mot sensu-server.");
} else {
startService();
}
}

synchronized void startService() {
if (executorService != null) {
throw new IllegalArgumentException("Service allerede startet, stopp først.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void init() throws IOException {
serverSocket = new ServerSocket(0);
serverSocket.setSoTimeout(1000);
sensuKlient = new SensuKlient("localhost", serverSocket.getLocalPort());
sensuKlient.start();
sensuKlient.startService();

new Thread(() -> {
try (Socket socket = serverSocket.accept()) {
Expand Down Expand Up @@ -81,4 +81,4 @@ public void logMetrics() throws Exception {
private String readFromSocket() throws InterruptedException {
return socketOutput.poll(10, TimeUnit.SECONDS);
}
}
}

0 comments on commit 9b8fac4

Please sign in to comment.