Commit e0de5bcd authored by Krishna Nikhil Vedurumudi's avatar Krishna Nikhil Vedurumudi
Browse files

Merge branch 'Enable_Logging_For_Health_Check_Fails' into 'master'

Enable logging for health check fails

See merge request !113
parents ce80bcd5 607293d8
Pipeline #51726 passed with stages
in 6 minutes and 1 second
......@@ -130,6 +130,9 @@ The following software have components provided under the terms of this license:
- Spring AOP (from https://github.com/spring-projects/spring-framework)
- Spring Beans (from https://github.com/spring-projects/spring-framework)
- Spring Boot (from http://projects.spring.io/spring-boot/)
- Spring Boot Actuator (from http://projects.spring.io/spring-boot/)
- Spring Boot Actuator AutoConfigure (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-actuator-autoconfigure)
- Spring Boot Actuator Starter (from http://projects.spring.io/spring-boot/)
- Spring Boot AutoConfigure (from http://projects.spring.io/spring-boot/)
- Spring Boot Json Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-json)
- Spring Boot Log4J2 Starter (from http://projects.spring.io/spring-boot/)
......@@ -161,7 +164,6 @@ The following software have components provided under the terms of this license:
- error-prone annotations (from )
- io.grpc:grpc-context (from https://github.com/grpc/grpc-java)
- jackson-databind (from http://github.com/FasterXML/jackson)
- jakarta.xml.bind-api (from )
- javatuples (from http://www.javatuples.org)
- javax.inject (from http://code.google.com/p/atinject/)
- lang-mustache (from https://github.com/elastic/elasticsearch)
......@@ -200,7 +202,7 @@ BSD-3-Clause
The following software have components provided under the terms of this license:
- ASM Core (from )
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- Apache Commons Codec (from http://commons.apache.org/proper/commons-codec/)
- JavaBeans Activation Framework API jar (from )
- Lucene Common Analyzers (from )
- Lucene Core (from )
......@@ -210,8 +212,9 @@ The following software have components provided under the terms of this license:
- Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java)
- Netty/Codec/HTTP (from )
- Reflections (from http://github.com/ronmamo/reflections)
- SnakeYAML (from http://www.snakeyaml.org)
- Spring Core (from https://github.com/spring-projects/spring-framework)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- jakarta.xml.bind-api (from )
========================================================================
......@@ -236,24 +239,15 @@ CC0-1.0
========================================================================
The following software have components provided under the terms of this license:
- Guava: Google Core Libraries for Java (from https://github.com/google/guava.git)
- HdrHistogram (from http://hdrhistogram.github.io/HdrHistogram/)
- LatencyUtils (from http://latencyutils.github.io/LatencyUtils/)
- Netty/Common (from )
- reactive-streams (from http://www.reactive-streams.org/)
========================================================================
CDDL-1.0
========================================================================
The following software have components provided under the terms of this license:
- Apache Log4j Core (from )
========================================================================
CDDL-1.1
========================================================================
The following software have components provided under the terms of this license:
- JavaBeans Activation Framework (from )
- javax.annotation-api (from http://jcp.org/en/jsr/detail?id=250)
- tomcat-embed-core (from http://tomcat.apache.org/)
========================================================================
......@@ -261,7 +255,8 @@ DOC
========================================================================
The following software have components provided under the terms of this license:
- aalto-xml (from )
- Lucene Core (from )
- Woodstox (from https://github.com/FasterXML/woodstox)
========================================================================
EPL-1.0
......@@ -274,6 +269,7 @@ The following software have components provided under the terms of this license:
- Microsoft Application Insights Java SDK Spring Boot starter (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java)
- SnakeYAML (from http://www.snakeyaml.org)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- org.junit.jupiter:junit-jupiter-api (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-engine (from http://junit.org/junit5/)
......@@ -300,18 +296,26 @@ GPL-2.0-only
========================================================================
The following software have components provided under the terms of this license:
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- JavaBeans Activation Framework (from )
- javax.annotation-api (from http://jcp.org/en/jsr/detail?id=250)
- tomcat-embed-core (from http://tomcat.apache.org/)
========================================================================
GPL-2.0-or-later
========================================================================
The following software have components provided under the terms of this license:
- SnakeYAML (from http://www.snakeyaml.org)
========================================================================
GPL-2.0-with-classpath-exception
========================================================================
The following software have components provided under the terms of this license:
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- JavaBeans Activation Framework (from )
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- javax.annotation-api (from http://jcp.org/en/jsr/detail?id=250)
- tomcat-embed-core (from http://tomcat.apache.org/)
========================================================================
......@@ -320,6 +324,7 @@ GPL-3.0-only
The following software have components provided under the terms of this license:
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- Project Lombok (from https://projectlombok.org)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
========================================================================
......@@ -328,7 +333,7 @@ ImageMagick
The following software have components provided under the terms of this license:
- Stax2 API (from http://github.com/FasterXML/stax2-api)
- aalto-xml (from )
- Woodstox (from https://github.com/FasterXML/woodstox)
========================================================================
JSON
......@@ -351,12 +356,27 @@ The following software have components provided under the terms of this license:
- Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java)
========================================================================
LGPL-2.1-or-later
========================================================================
The following software have components provided under the terms of this license:
- Javassist (from http://www.javassist.org/)
- SnakeYAML (from http://www.snakeyaml.org)
========================================================================
LGPL-3.0-only
========================================================================
The following software have components provided under the terms of this license:
- Apache Log4j API (from )
- Apache Log4j Core (from )
========================================================================
MIT
========================================================================
The following software have components provided under the terms of this license:
- Apache Log4j SLF4J Binding (from )
- Azure Java Client Authentication Library for AutoRest (from https://github.com/Azure/autorest-clientruntime-for-java)
- Azure Java Client Runtime for ARM (from https://github.com/Azure/autorest-clientruntime-for-java)
- Azure Java Client Runtime for AutoRest (from https://github.com/Azure/autorest-clientruntime-for-java)
......@@ -394,18 +414,33 @@ The following software have components provided under the terms of this license:
- adal4j (from https://github.com/AzureAD/azure-activedirectory-library-for-java)
- azure-documentdb (from https://azure.microsoft.com/en-us/services/cosmos-db/)
- documentdb-bulkexecutor (from http://azure.microsoft.com/en-us/services/documentdb/)
- jakarta.xml.bind-api (from )
- micrometer-core (from https://github.com/micrometer-metrics/micrometer)
- mockito-junit-jupiter (from https://github.com/mockito/mockito)
- msal4j (from https://github.com/AzureAD/microsoft-authentication-library-for-java)
- msal4j-persistence-extension (from https://github.com/AzureAD/microsoft-authentication-extensions-for-java)
========================================================================
MPL-1.1
========================================================================
The following software have components provided under the terms of this license:
- Javassist (from http://www.javassist.org/)
========================================================================
MPL-2.0
========================================================================
The following software have components provided under the terms of this license:
- Javassist (from http://www.javassist.org/)
- OkHttp (from )
========================================================================
PHP-3.01
========================================================================
The following software have components provided under the terms of this license:
- JavaBeans Activation Framework API jar (from )
- jakarta.xml.bind-api (from )
========================================================================
Public-Domain
......@@ -414,6 +449,20 @@ The following software have components provided under the terms of this license:
- Spongy Castle (from http://rtyley.github.io/spongycastle/)
========================================================================
SPL-1.0
========================================================================
The following software have components provided under the terms of this license:
- Checker Qual (from https://checkerframework.org)
========================================================================
SunPro
========================================================================
The following software have components provided under the terms of this license:
- Lucene Core (from )
========================================================================
WTFPL
========================================================================
......@@ -426,7 +475,7 @@ X11
========================================================================
The following software have components provided under the terms of this license:
- Guava: Google Core Libraries for Java (from https://github.com/google/guava.git)
- Lucene Core (from )
========================================================================
public-domain
......@@ -434,6 +483,7 @@ public-domain
The following software have components provided under the terms of this license:
- Asynchronous Http Client (from )
- Guava: Google Core Libraries for Java (from https://github.com/google/guava.git)
- HdrHistogram (from http://hdrhistogram.github.io/HdrHistogram/)
- Joda-Time (from http://www.joda.org/joda-time/)
- LatencyUtils (from http://latencyutils.github.io/LatencyUtils/)
......@@ -441,9 +491,11 @@ The following software have components provided under the terms of this license:
- Microsoft Azure SDK for EventGrid Management (from https://github.com/Azure/azure-sdk-for-java)
- Microsoft Azure SDK for SQL API of Azure Cosmos DB Service (from https://github.com/Azure/azure-sdk-for-java)
- Microsoft Azure client library for Blob Storage (from https://github.com/Azure/azure-sdk-for-java)
- Project Lombok (from https://projectlombok.org)
- Spring Web (from https://github.com/spring-projects/spring-framework)
- azure-documentdb (from https://azure.microsoft.com/en-us/services/cosmos-db/)
- msal4j (from https://github.com/AzureAD/microsoft-authentication-library-for-java)
- reactive-streams (from http://www.reactive-streams.org/)
========================================================================
unknown
......@@ -451,7 +503,16 @@ unknown
The following software have components provided under the terms of this license:
- Byte Buddy (without dependencies) (from )
- Checker Qual (from https://checkerframework.org)
- JSON in Java (from https://github.com/douglascrockford/JSON-java)
- JUnit Jupiter (Aggregator) (from https://junit.org/junit5/)
- JavaBeans Activation Framework API jar (from )
- Spongy Castle (from http://rtyley.github.io/spongycastle/)
- jakarta.xml.bind-api (from )
- org.junit.jupiter:junit-jupiter-api (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-engine (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-params (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-commons (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-engine (from http://junit.org/junit5/)
......@@ -253,6 +253,20 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<repositories>
......
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.azure.health;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.boot.actuate.endpoint.http.ApiVersion;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.health.CompositeHealth;
import org.springframework.boot.actuate.health.HealthComponent;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthEndpointGroups;
import org.springframework.boot.actuate.health.HealthEndpointWebExtension;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
/**
* Implementation for logging features of health check failures.
* All services can pick up this class from core-lib-azure from the usual dependency that’s added.
*/
@Configuration
public class AzureHealthEndpointWebExtension extends HealthEndpointWebExtension {
private static final String LOGGER_NAME = AzureHealthEndpointWebExtension.class.getName();
/**
* @param registry the HealthContributorRegistry
* @param groups the HealthEndpointGroups
*/
public AzureHealthEndpointWebExtension(final HealthContributorRegistry registry, final HealthEndpointGroups groups) {
super(registry, groups);
}
/**
* @param apiVersion
* @param securityContext
* @param showAll
* @param path
* @return
*/
@Override
public WebEndpointResponse<HealthComponent> health(final ApiVersion apiVersion, final SecurityContext securityContext,
final boolean showAll, final String... path) {
WebEndpointResponse<HealthComponent> response = superClassCall(apiVersion, securityContext, showAll, path);
HealthComponent health = response.getBody();
Status status = health.getStatus();
if (status != Status.UP) {
Map<String, HealthComponent> map = ((CompositeHealth) health).getComponents();
for (Map.Entry<String, HealthComponent> entry : map.entrySet()) {
Status componentStatus = entry.getValue().getStatus();
String componentLabel = entry.getKey();
if (componentStatus == Status.DOWN) {
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).error("Health component {} has status {}", componentLabel, componentStatus);
}
}
}
return response;
}
/**
* @param apiVersion the Api Version
* @param securityContext the security Context
* @param showAll the boolean flag
* @param path the path
* @return the webEndpointResponse object
*/
WebEndpointResponse<HealthComponent> superClassCall(final ApiVersion apiVersion, final SecurityContext securityContext, final boolean showAll, final String... path) {
return super.health(apiVersion, securityContext, showAll, path);
}
}
# Copyright © Microsoft Corporation
# Copyright Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -26,6 +26,9 @@ server.tomcat.mbeanregistry.enabled=true
#Tomcat threads configuration
server.tomcat.min-spare-threads=100
#Run Actuator on different port
management.server.port=8081
#Name of keys in key-vault for system resources/cosmos db
osdu.azure.system.cosmosDBAccountKeyName=system-cosmos-endpoint
osdu.azure.system.cosmosPrimaryKeyName=system-cosmos-primary-key
......
package org.opengroup.osdu.azure.health;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.logging.CoreLogger;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.boot.actuate.endpoint.http.ApiVersion;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.health.CompositeHealth;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthComponent;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthEndpointGroups;
import org.springframework.boot.actuate.health.Status;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Tests for LoggingHealthEndPointWebExtension.
*/
@ExtendWith(MockitoExtension.class)
public class AzureHealthEndpointWebExtensionTest {
private static AzureHealthEndpointWebExtension loggingHealthEndpointWebExtension;
private static AzureHealthEndpointWebExtension spy;
private static HealthContributorRegistry registry;
private static HealthEndpointGroups groups;
private static Map<String, HealthComponent> componentMap; //Contains the 'components' of the health endpoint response
@Mock
private CoreLoggerFactory coreLoggerFactory;
@Mock
private CoreLogger logger;
@Mock
SecurityContext securityContext;
@Mock
Health health;
@Mock
CompositeHealth compositeHealth;
/**
* Workaround for inability to mock static methods like getInstance().
*
* @param mock CoreLoggerFactory mock instance
*/
private void mockSingleton(CoreLoggerFactory mock) {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, mock);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@BeforeEach
public void setup() {
registry = mock(HealthContributorRegistry.class);
groups = mock(HealthEndpointGroups.class);
loggingHealthEndpointWebExtension = new AzureHealthEndpointWebExtension(registry,groups);
spy = Mockito.spy(loggingHealthEndpointWebExtension);
componentMap = new HashMap<>();
componentMap.put("keyvault", health);
}
/**
Verify that nothing is logged when Status of health endpoint is UP.
*/
@Test
public void healthTest_whenStatusIs_UP() {
Mockito.when(health.getStatus()).thenReturn(Status.UP);
Mockito.when(compositeHealth.getComponents()).thenReturn(componentMap);
WebEndpointResponse<HealthComponent> expected = new WebEndpointResponse<>(compositeHealth, WebEndpointResponse.STATUS_OK);
doReturn(expected).when(spy).superClassCall(ApiVersion.LATEST, securityContext, true);
spy.health(ApiVersion.LATEST, securityContext, true, new String[0]);
verify(logger, times(0)).error(anyString(), any(), any());
}
/**
Verify that a single line is logged when ONE HealthComponent has Status DOWN.
*/
@Test
public void healthTest_whenStatusIs_DOWN() throws Exception{
mockSingleton(coreLoggerFactory);
when(coreLoggerFactory.getLogger(anyString())).thenReturn(logger);
Mockito.when(health.getStatus()).thenReturn(Status.DOWN);
Mockito.when(compositeHealth.getComponents()).thenReturn(componentMap);
WebEndpointResponse<HealthComponent> expected = new WebEndpointResponse<>(compositeHealth, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
doReturn(expected).when(spy).superClassCall(ApiVersion.LATEST, securityContext, true);
spy.health(ApiVersion.LATEST, securityContext, true, new String[0]);
verify(logger, times(1)).error(anyString(), any(), any());
}
}
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