-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
*/ | ||
@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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this type? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.