Multi-Application configuration with Contrast Profiles

  • Updated

Contrast Profiles are a Java Agent feature enabling multiple applications running on the same JVM to be configured differently. The following Contrast properties can be configured with Contrast Profiles:

  • contrast.application.code
  • contrast.application.group
  • contrast.application.metadata
  • contrast.application.name
  • contrast.application.session_id
  • contrast.application.session_metadata
  • contrast.application.tags
  • contrast.application.version
  • contrast.assess.tags
  • contrast.inventory.tags

Profile settings apply only to the application with a particular profile and take precedence over the general Contrast settings.

 

Disclaimer(s):

This functionality requires you have Java agent version 3.7.7.16760 or beyond.

When standalone_app_name is specified, the agent treats the entire JVM/server as a single application. Because only one app is created, it is not possible to utilize multi-tenant application configuration when standalone_app_name is enabled.

 

Setting the Application Profile

You can specify an application profile via Servlet initialization parameters in your application. A profile name must consist only of lower-case ASCII letters, numbers, or underscores.

The profile name can be set declaratively via web.xml:

<context-param>
<param-name>contrast.profile</param-name>
<param-value>app1</param-value>
</context-param>

Example web.xml with Contrast Profiles context-param:

<?xml version="1.0" encoding="UTF-8"?> 

<
web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

<context-param>

<param-name>contrast.profile</param-name>

<param-value>hello1</param-value>

</context-param>
</web-app>

Or programmatically via ServletContainerInitializer:

public final class ExampleServletContainerInitializer
implements ServletContainerInitializer {
/* ... */
@Override
public final void onStartup(
final Set<Class<?>> annotatedClasses,
final ServletContext servletContext) {
/* ... */
servletContext.setInitParameter("contrast.profile", "app1");
}
}
Or programmatically via SpringBootServletInitializer in a Spring Boot WAR:
@SpringBootApplication
public class ExampleSpringBootApplication
extends SpringBootServletInitializer {
/* ... */
@Override
public void onStartup(
final ServletContext servletContext)
throws ServletException {
/* ... */
servletContext.setInitParameter("contrast.profile", "app1");
super.onStartup(servletContext);
}
}

There may be additional ways to set Servlet initialization parameters for other application types.

 

Customizing An Application Profile

YAML Properties

You can set values for a specific application profile using YAML by setting properties under:

profile:
${profile.name}:

 

For example, to set Assess tags for the app1 profile above, you would provide the following:

profile:
app1:
assess:
tags: app1_assess_tags

 

Further examples:

assess:
tags: general_assess_tags
profile:
app1:
assess:
tags: app1_assess_tags
application:
name: myConfigAppName
tags: app1_tags
session_metadata: buildNumber=1
app2:
assess:
tags: app2tags

 

Application profile settings take precedence over general settings, so in the above case (and for the examples below), app1 would have Assess tags of app1_assess_tags, app2 would have Assess tags of app2tags, and all other applications would have Assess tags of general_assess_tags.

Overuse of application.name, where one is setting multiple different applications to the same name value, can lead to a truncated list of libraries

Environment Variables

You can set values for a specific application profile using environment variables by prepending the setting with CONTRAST__PROFILE__${UPPERCASE_PROFILE_NAME}__. For example, to set Assess tags for the app1 profile above, you would export the following:

export CONTRAST__PROFILE__APP1__ASSESS__TAGS=app1_assess_tags

 

More examples:

export CONTRAST__PROFILE__APP1__APPLICATION__NAME=myConfigAppName
export CONTRAST__PROFILE__APP1__APPLICATION__TAGS=app1_tags
export CONTRAST__PROFILE__APP1__APPLICATION__SESSION_METADATA='buildNumber=1'
export CONTRAST__PROFILE__APP2__ASSESS__TAGS=app2tags
export CONTRAST__ASSESS__TAGS=general_assess_tags

 

System Properties

You can set values for a specific application profile using system properties by prepending the setting with contrast.profile.${profile.name}.. For example, to set Assess tags for the app1 profile above, you would provide the following:

-Dcontrast.profile.app1.assess.tags=app1_assess_tags

 

Further examples:

-Dcontrast.profile.app1.application.name=myConfigAppName
-Dcontrast.profile.app1.application.tags=app1_tags
-Dcontrast.profile.app1.application.session_metadata='buildNumber=1'
-Dcontrast.profile.app2.assess.tags=app2tags
-Dcontrast.assess.tags=general_assess_tags

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request