-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/software/amazon/awssdk/services/cloudwatchlogs/emf/config/Configuration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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 is only used for the Cloudwatch Agent in agent-based environment. | ||
*/ | ||
@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; | ||
} |
14 changes: 14 additions & 0 deletions
14
...ain/java/software/amazon/awssdk/services/cloudwatchlogs/emf/config/ConfigurationKeys.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
57 changes: 57 additions & 0 deletions
57
...re/amazon/awssdk/services/cloudwatchlogs/emf/config/EnvironmentConfigurationProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/software/amazon/awssdk/services/cloudwatchlogs/emf/config/SystemWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package software.amazon.awssdk.services.cloudwatchlogs.emf.config; | ||
|
||
class SystemWrapper { | ||
static String getenv(String name) { | ||
return System.getenv(name); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...va/software/amazon/awssdk/services/cloudwatchlogs/emf/environment/DefaultEnvironment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...main/java/software/amazon/awssdk/services/cloudwatchlogs/emf/environment/Environment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
11 changes: 11 additions & 0 deletions
11
...a/software/amazon/awssdk/services/cloudwatchlogs/emf/environment/EnvironmentProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ain/java/software/amazon/awssdk/services/cloudwatchlogs/emf/environment/Environments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why do we need this type?
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.
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.