diff --git a/api/src/main/java/org/commonjava/indy/conf/EnvironmentConfig.java b/api/src/main/java/org/commonjava/indy/conf/EnvironmentConfig.java new file mode 100644 index 0000000000..cafacaf9ae --- /dev/null +++ b/api/src/main/java/org/commonjava/indy/conf/EnvironmentConfig.java @@ -0,0 +1,97 @@ +/** + * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * 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.commonjava.indy.conf; + +import org.commonjava.web.config.ConfigurationException; +import org.commonjava.web.config.annotation.SectionName; +import org.commonjava.web.config.section.MapSectionListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.enterprise.context.ApplicationScoped; +import java.io.File; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by yma on 2019/3/19. + */ + +@ApplicationScoped +@SectionName( EnvironmentConfig.SECTION_NAME ) +public class EnvironmentConfig + extends MapSectionListener + implements IndyConfigInfo +{ + public static final String SECTION_NAME = "environment"; + + public static final String ENV_PREFIX = "mdc.env"; + + public static final String HOSTNAME = "HOSTNAME"; + + public static final String UNKNOWN = "UNKNOWN"; + + private final Logger logger = LoggerFactory.getLogger( getClass() ); + + private Map envars = new HashMap<>(); + + public EnvironmentConfig() + { + } + + @Override + public String getDefaultConfigFileName() + { + return new File( IndyConfigInfo.CONF_INCLUDES_DIR, "environment.conf" ).getPath(); + } + + @Override + public InputStream getDefaultConfig() + { + return Thread.currentThread().getContextClassLoader().getResourceAsStream( "default-environment.conf" ); + } + + @Override + public synchronized void parameter( final String name, final String value ) + throws ConfigurationException + { + if ( name.startsWith( ENV_PREFIX ) ) + { + String envKey = name.substring( ENV_PREFIX.length() ); + String enValue = System.getenv( envKey ); + if ( envKey.equals( HOSTNAME ) && enValue == null ) + { + try + { + enValue = InetAddress.getLocalHost().getHostName(); + } + catch ( UnknownHostException e ) + { + logger.error( String.format( "Unknown host. Reason: %s", e.getMessage() ), e ); + } + } + envars.put( value, enValue == null ? UNKNOWN : enValue ); + } + } + + public Map getEnvars() + { + return envars; + } +} diff --git a/core/src/main/conf/conf.d/environment.conf b/core/src/main/conf/conf.d/environment.conf new file mode 100644 index 0000000000..7c64d72b46 --- /dev/null +++ b/core/src/main/conf/conf.d/environment.conf @@ -0,0 +1,5 @@ +[environment] +mdc.env.OPENSHIFT_BUILD_NAMESPACE = NAMESPACE +mdc.env.OPENSHIFT_BUILD_NAME = BUILD_NAME +mdc.env.OPENSHIFT_BUILD_COMMIT = BUILD_COMMIT +mdc.env.HOSTNAME = HOSTNAME \ No newline at end of file diff --git a/core/src/main/resources/default-environment.conf b/core/src/main/resources/default-environment.conf new file mode 100644 index 0000000000..7c64d72b46 --- /dev/null +++ b/core/src/main/resources/default-environment.conf @@ -0,0 +1,5 @@ +[environment] +mdc.env.OPENSHIFT_BUILD_NAMESPACE = NAMESPACE +mdc.env.OPENSHIFT_BUILD_NAME = BUILD_NAME +mdc.env.OPENSHIFT_BUILD_COMMIT = BUILD_COMMIT +mdc.env.HOSTNAME = HOSTNAME \ No newline at end of file diff --git a/core/src/test/java/org/commonjava/indy/core/conf/EnvironmentConfigTest.java b/core/src/test/java/org/commonjava/indy/core/conf/EnvironmentConfigTest.java new file mode 100644 index 0000000000..d41e25499a --- /dev/null +++ b/core/src/test/java/org/commonjava/indy/core/conf/EnvironmentConfigTest.java @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * 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.commonjava.indy.core.conf; + +import org.commonjava.indy.conf.EnvironmentConfig; +import org.commonjava.indy.test.utils.WeldJUnit4Runner; +import org.commonjava.web.config.ConfigUtils; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.enterprise.inject.Instance; +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by yma on 2019/3/21. + */ +@RunWith( WeldJUnit4Runner.class ) +public class EnvironmentConfigTest +{ + @Inject + private Instance instance; + + @Test + public void weldInjection_IterateIndyLoggingConfigurators() + { + List sections = new ArrayList<>(); + instance.iterator().forEachRemaining( ( instance)->{ + String section = ConfigUtils.getSectionName( instance ); + System.out.printf( "Got instance: %s with section: %s\n", instance, section ); + sections.add( section ); + } ); + + System.out.println(sections); + assertThat( sections.contains( EnvironmentConfig.SECTION_NAME ), equalTo( true ) ); + } +} diff --git a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java new file mode 100644 index 0000000000..c910ab3ced --- /dev/null +++ b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java @@ -0,0 +1,61 @@ +/** + * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * 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.commonjava.indy.bind.jaxrs; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.contrib.json.classic.JsonLayout; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.commonjava.indy.conf.EnvironmentConfig; +import org.commonjava.indy.model.core.io.IndyObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.enterprise.inject.spi.CDI; +import java.util.Map; + +import static org.commonjava.indy.bind.jaxrs.RequestContextConstants.ENVIRONMENT; + +/** + * Created by yma on 2019/3/26. + */ +public class CustomJsonLayout + extends JsonLayout +{ + private final Logger logger = LoggerFactory.getLogger( getClass() ); + + @Override + protected void addCustomDataToJsonMap( Map map, ILoggingEvent iLoggingEvent ) + { + IndyObjectMapper objectMapper = new IndyObjectMapper( true ); + super.addCustomDataToJsonMap( map, iLoggingEvent ); + + if ( !iLoggingEvent.getMDCPropertyMap().isEmpty() ) + { + Map mdcs = (Map) map.get( MDC_ATTR_NAME ); + try + { + Map envars = CDI.current().select( EnvironmentConfig.class ).get().getEnvars(); + mdcs.put( ENVIRONMENT, objectMapper.writeValueAsString( envars ) ); + } + catch ( JsonProcessingException e ) + { + mdcs.put( ENVIRONMENT, "{error: \"Envars could not be processed by Jackson.\"}" ); + logger.error( String.format( "Failed to create environment mdc. Reason: %s", e.getMessage() ), e ); + } + map.put( MDC_ATTR_NAME, mdcs ); + } + } +} diff --git a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/MDCManager.java b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/MDCManager.java index 650a4a657e..0f231dc542 100644 --- a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/MDCManager.java +++ b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/MDCManager.java @@ -15,9 +15,12 @@ */ package org.commonjava.indy.bind.jaxrs; +import com.fasterxml.jackson.core.JsonProcessingException; import org.apache.http.Header; import org.apache.http.HttpRequest; import org.commonjava.indy.conf.IndyConfiguration; +import org.commonjava.indy.conf.EnvironmentConfig; +import org.commonjava.indy.model.core.io.IndyObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; diff --git a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java index d9f17ac0e1..33e6985c15 100644 --- a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java +++ b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java @@ -61,4 +61,6 @@ public class RequestContextConstants @Thread @MDC public static final String PREFERRED_ID = "preferred-id"; + @Thread @MDC + public static final String ENVIRONMENT = "environment"; }