Skip to content

Default environment #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.11.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.1'
implementation 'org.slf4j:slf4j-api:1.7.30'

// Use JUnit test framework
testImplementation 'junit:junit:4.13'
testImplementation 'org.apache.commons:commons-lang3:3.10'
testImplementation "org.mockito:mockito-core:2.+"
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
}
6 changes: 3 additions & 3 deletions examples/ErrorHandlingExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.EMFLogger;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.FlushException;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.ConsoleSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.MultiSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.ConsoleSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.MultiSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.CloudwatchMetricCollection;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.EMFLogItem;

Expand Down
6 changes: 3 additions & 3 deletions examples/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.EMFLogger;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.FlushException;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.ConsoleSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.MultiSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.ConsoleSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.MultiSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.Aggregation;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.CloudwatchMetricCollection;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.EMFLogItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.EMFLogger;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.CloudWatchLogsClientSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.testutils.EMFTestUtilities;

public class IntegrationTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.CloudWatchLimits;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.EMFLogger;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.FlushException;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.SinkUtilities;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.SinkUtilities;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.CloudwatchMetricCollection;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.EMFLogItem;
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.config;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import software.amazon.awssdk.services.cloudwatchlogs.emf.environment.Environments;

import java.util.Optional;

@AllArgsConstructor
public class Configuration {
/**
* Whether or not internal logging should be enabled.
*/
@Getter @Setter
boolean debuggingLoggingEnabled;

/**
* The name of the service to use in the default dimensions.
*/
@Getter @Setter
Optional<String> serviceName;

/**
* The type of the service to use in the default dimensions.
*/
@Getter @Setter
Optional<String> serviceType;

/**
* The LogGroup name to use. This will be ignored when using the
* Lambda scope.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you probably copied this from the other repos, but we can be explicit in the javadocs and say that this is really just for the CWAgent.

*/
@Getter @Setter
Optional<String> logGroupName;

/**
* The LogStream name to use. This will be ignored when using the
* Lambda scope.
*/
@Getter @Setter
Optional<String> logStreamName;

/**
* The endpoint to use to connect to the CloudWatch Agent
*/
@Getter @Setter
Optional<String> agentEndpoint;

/**
* Environment override. This will short circuit auto-environment detection.
* Valid values include:
* - Local: no decoration and sends over stdout
* - Lambda: decorates logs with Lambda metadata and sends over stdout
* - Agent: no decoration and sends over TCP
* - EC2: decorates logs with EC2 metadata and sends over TCP
*/
@Getter @Setter
Environments environmentOverride;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.config;

public class ConfigurationKeys {

public static final String ENV_VAR_PREFIX = "AWS_EMF";

public static final String ENABLE_DEBUG_LOGGING = "ENABLE_DEBUG_LOGGING";
public static final String SERVICE_NAME = "SERVICE_NAME";
public static final String SERVICE_TYPE = "SERVICE_TYPE";
public static final String LOG_GROUP_NAME = "LOG_GROUP_NAME";
public static final String LOG_STREAM_NAME = "LOG_STREAM_NAME";
public static final String AGENT_ENDPOINT = "AGENT_ENDPOINT";
public static final String ENVIRONMENT_OVERRIDE = "ENVIRONMENT";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.config;

import software.amazon.awssdk.services.cloudwatchlogs.emf.environment.Environments;

import java.util.Optional;

/**
* Loads configuration from environment variables
*/
public class EnvironmentConfigurationProvider {
private static Configuration config;

protected EnvironmentConfigurationProvider() {}

public static Configuration getConfig() {
if (config == null) {
config = new Configuration(
getBoolEnvVar(ConfigurationKeys.ENABLE_DEBUG_LOGGING),
getEnvVar(ConfigurationKeys.SERVICE_NAME),
getEnvVar(ConfigurationKeys.SERVICE_TYPE),
getEnvVar(ConfigurationKeys.LOG_GROUP_NAME),
getEnvVar(ConfigurationKeys.LOG_STREAM_NAME),
getEnvVar(ConfigurationKeys.AGENT_ENDPOINT),
getEnvironmentOverride()
);
}
return config;
}

private static Optional<String> getEnvVar(String key) {
String name = String.join("", ConfigurationKeys.ENV_VAR_PREFIX, "_", key);
return Optional.ofNullable(getEnv(name));
}

private static boolean getBoolEnvVar(String key) {
String name = String.join("", ConfigurationKeys.ENV_VAR_PREFIX, "_", key);
return Optional.ofNullable(getEnv(name)).map(str -> str.equalsIgnoreCase("true")).orElse(false);
}

private static Environments getEnvironmentOverride() {
Optional<String> environmentName = getEnvVar(ConfigurationKeys.ENVIRONMENT_OVERRIDE);
if (!environmentName.isPresent()) {
return Environments.Unknown;
}

try {
return Environments.valueOf(environmentName.get());
} catch (Exception e) {
return Environments.Unknown;
}
}

private static String getEnv(String name) {
return SystemWrapper.getenv(name);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.config;

class SystemWrapper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Configuration is a singleton and is created by EnvironmentConfigurationProvider. The Provider reads environment variables from System. It's difficult to mock the System to test the EnvironmentConfigurationProvider, so I used SystemWrapper to make it possible to mock System.

static String getenv(String name) {
return System.getenv(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.environment;

import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.services.cloudwatchlogs.emf.config.Configuration;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.AgentSink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.Endpoint;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.ISink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.MetricsContext;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.SocketClientFactory;


@Slf4j
public class DefaultEnvironment implements Environment {
private Configuration config;
private ISink sink;

public DefaultEnvironment(Configuration config) {
this.config = config;
}


@Override
public boolean probe() {
return true;
}

@Override
public String getName() {
if (!config.getServiceName().isPresent()) {
log.info("Unknown ServiceName");
return "Unknown";
}
return config.getServiceName().get();
}

@Override
public String getType() {
if (!config.getServiceType().isPresent()) {
log.info("Unknown ServiceType");
return "Unknown";
}
return config.getServiceType().get();
}

public String getLogStreamName() {
return config.getLogStreamName().orElse(getName() + "-stream");
}

@Override
public String getLogGroupName() {
return config.getLogGroupName().orElse(getName() + "-metrics");
}

@Override
public void configureContext(MetricsContext context) {
// no-op
}

@Override
public ISink getSink() {
if (sink == null) {
Endpoint endpoint;
if (!config.getAgentEndpoint().isPresent()) {
log.info("Endpoint is not defined. Using default: {}", Endpoint.DEFAULT_TCP_ENDPOINT);
endpoint = Endpoint.DEFAULT_TCP_ENDPOINT;
} else {
endpoint = Endpoint.fromURL(config.getAgentEndpoint().get());
}
sink = new AgentSink(getLogGroupName(), getLogStreamName(), endpoint, new SocketClientFactory());
}
return sink;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.environment;

import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.ISink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.MetricsContext;

/**
* A runtime environment (e.g. Lambda, EKS, ECS, EC2)
*/
public interface Environment {
/**
* Determines whether or not we are executing in this environment
*/
boolean probe();

/**
* Get the environment name. This will be used to set the ServiceName dimension.
*/
String getName();

/**
* Get the environment type. This will be used to set the ServiceType dimension.
*/
String getType();

/**
* Get log group name. This will be used to set the LogGroup dimension.
*/
String getLogGroupName();

/**
* Configure the context with environment properties.
*
* @param context
*/
void configureContext(MetricsContext context);

/**
* Create the appropriate sink for this environment.
*/
ISink getSink();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.environment;

import software.amazon.awssdk.services.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;

public class EnvironmentProvider {

//TODO: Support more environments
public Environment resolveEnvironment() {
return new DefaultEnvironment(EnvironmentConfigurationProvider.getConfig());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package software.amazon.awssdk.services.cloudwatchlogs.emf.environment;

public enum Environments {
Local, Lambda, Agent, EC2, ECS, Unknown
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import software.amazon.awssdk.services.cloudwatchlogs.emf.logger.sinks.ISink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.sinks.ISink;
import software.amazon.awssdk.services.cloudwatchlogs.emf.model.EMFLogItem;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public FlushException(List<EMFLogItem> failedLogItems, List<EMFLogItem> unproces
this.unprocessedLogItems = unprocessedLogItems;
}

public FlushException(String message, Throwable cause) {
super(message, cause);
}

// Internal constructor for handling errors before all information is available
protected FlushException(String message) {
super(message);
Expand Down
Loading