Commit f9341335 authored by Kelly Domico's avatar Kelly Domico Committed by Kishore Battula
Browse files

Use TelemetryClient to log dependencies to App Insights dependencies table

parent ec06e610
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.46</version>
<version>0.0.47</version>
<name>core-lib-azure</name>
<properties>
......
package org.opengroup.osdu.azure.logging;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.internal.util.MapUtil;
import com.microsoft.applicationinsights.telemetry.Duration;
import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.slf4j.Logger;
import org.slf4j.MDC;
/**
* Logger wrapper around SLF4J APIs.
......@@ -9,12 +14,15 @@ import org.slf4j.Logger;
public class CoreLogger implements ICoreLogger {
private final Logger logger;
private final TelemetryClient telemetryClient;
/**
* @param traceLogger the logger instance
* @param traceLogger the logger instance
* @param appInsightsClient the Application Insights telemetry client
*/
public CoreLogger(final Logger traceLogger) {
public CoreLogger(final Logger traceLogger, final TelemetryClient appInsightsClient) {
this.logger = traceLogger;
this.telemetryClient = appInsightsClient;
}
/**
......@@ -119,6 +127,22 @@ public class CoreLogger implements ICoreLogger {
*/
@Override
public void logDependency(final DependencyPayload payload) {
this.logger.info("{}", payload);
this.telemetryClient.trackDependency(getRemoteDependencyTelemetry(payload));
}
/**
* Returns a RemoteDependencyTelemetry object from DependencyPayload object.
*
* @param payload a DependencyPayload object
* @return a RemoteDependencyTelemetry object
*/
private RemoteDependencyTelemetry getRemoteDependencyTelemetry(final DependencyPayload payload) {
RemoteDependencyTelemetry telemetry = new RemoteDependencyTelemetry(payload.getName(), payload.getData(), new Duration(payload.getDuration().toMillis()), payload.isSuccess());
telemetry.setResultCode(payload.getResultCode());
telemetry.setType(payload.getType());
telemetry.setTarget(payload.getTarget());
MapUtil.copy(MDC.getCopyOfContextMap(), telemetry.getContext().getProperties());
return telemetry;
}
}
package org.opengroup.osdu.azure.logging;
import com.microsoft.applicationinsights.TelemetryClient;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
......@@ -14,6 +15,8 @@ public final class CoreLoggerFactory implements ICoreLoggerFactory {
private Map<String, ICoreLogger> loggers = new HashMap<>();
private TelemetryClient telemetryClient = new TelemetryClient();
/**
* CoreLoggerFactory singleton.
*/
......@@ -35,7 +38,7 @@ public final class CoreLoggerFactory implements ICoreLoggerFactory {
*/
public ICoreLogger getLogger(final String name) {
if (!loggers.containsKey(name)) {
CoreLogger logger = new CoreLogger(LoggerFactory.getLogger(name));
CoreLogger logger = new CoreLogger(LoggerFactory.getLogger(name), this.telemetryClient);
loggers.put(name, logger);
}
......
......@@ -12,9 +12,11 @@ import java.time.Duration;
@Setter
public final class DependencyPayload {
private String name;
private String type = "HTTP";
private String data;
private Duration duration;
private String resultCode;
private String target;
private boolean success;
/**
......@@ -38,6 +40,7 @@ public final class DependencyPayload {
this.duration = dependencyDuration;
this.resultCode = dependencyResultCode;
this.success = dependencySuccess;
this.target = dependencyData;
}
@Override
......
package org.opengroup.osdu.azure.logging;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.logging.audit.AuditAction;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
......@@ -15,6 +18,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Contains tests for {@link CoreLogger}
......@@ -29,12 +33,14 @@ public class CoreLoggerTest {
private static final String ARG3 = "Argument 3 value";
private static Logger spySlf4jLogger;
private static TelemetryClient telemetryClient;
private static CoreLogger coreLogger;
@BeforeAll
public static void setup() {
spySlf4jLogger = spy(LoggerFactory.getLogger(LOGGER_NAME));
coreLogger = mock(CoreLogger.class, withSettings().useConstructor(spySlf4jLogger).defaultAnswer(CALLS_REAL_METHODS));
telemetryClient = mock(TelemetryClient.class);
coreLogger = mock(CoreLogger.class, withSettings().useConstructor(spySlf4jLogger, telemetryClient).defaultAnswer(CALLS_REAL_METHODS));
}
@Test
......@@ -107,8 +113,24 @@ public class CoreLoggerTest {
@Test
public void testLogDependency() {
DependencyPayload payload = new DependencyPayload("DependencyName", "DependencyPayload", Duration.ofMillis(1000), "200", true);
RemoteDependencyTelemetry telemetry = new RemoteDependencyTelemetry("DependencyName", "Dependency/Command/Name", new com.microsoft.applicationinsights.telemetry.Duration((long) 1000), true);
telemetry.setResultCode("200");
telemetry.setType("HTTP");
telemetry.setTarget("Dependency/Command/Name");
final ArgumentCaptor<RemoteDependencyTelemetry> telemetryCaptor = ArgumentCaptor.forClass(RemoteDependencyTelemetry.class);
doNothing().when(telemetryClient).trackDependency(telemetryCaptor.capture());
DependencyPayload payload = new DependencyPayload("DependencyName", "Dependency/Command/Name", Duration.ofMillis(1000), "200", true);
coreLogger.logDependency(payload);
verify(spySlf4jLogger).info("{}", payload);
assertEquals(1, telemetryCaptor.getAllValues().size());
assertEquals("DependencyName", telemetryCaptor.getAllValues().get(0).getName());
assertEquals("Dependency/Command/Name", telemetryCaptor.getAllValues().get(0).getCommandName());
assertEquals("HTTP", telemetryCaptor.getAllValues().get(0).getType());
assertEquals("Dependency/Command/Name", telemetryCaptor.getAllValues().get(0).getTarget());
assertEquals(1000, telemetryCaptor.getAllValues().get(0).getDuration().getTotalMilliseconds());
assertEquals("200", telemetryCaptor.getAllValues().get(0).getResultCode());
assertEquals(true, telemetryCaptor.getAllValues().get(0).getSuccess());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment