-
Notifications
You must be signed in to change notification settings - Fork 92
chore: Add powertools specific user-agent-suffix to the AWS SDK v2 clients #1306
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
jeromevdl
merged 11 commits into
aws-powertools:main
from
eldimi:chore/user_agent_sdk_clients
Aug 2, 2023
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7f74a9f
chore: add powertools user-agent-suffix to the AWS SDK v2 clients
eldimi 44f6b9b
Merge branch 'main' into chore/user_agent_sdk_clients
eldimi b1809cc
fix wrong import for logger
eldimi 3bf973c
fix review comments: Add comments and javadoc for readability, rename…
eldimi 0c1f723
Merge branch 'main' into chore/user_agent_sdk_clients
eldimi 825b41c
fix typo
eldimi 51313a3
Merge branch 'main' into chore/user_agent_sdk_clients
eldimi ecaa919
fix code smells and formatting issues
eldimi 5c9f462
Merge branch 'main' into chore/user_agent_sdk_clients
eldimi 7371857
Merge branch 'main' into chore/user_agent_sdk_clients
jeromevdl 8f0e137
Merge branch 'main' into chore/user_agent_sdk_clients
jeromevdl 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
90 changes: 90 additions & 0 deletions
90
...re/src/main/java/software/amazon/lambda/powertools/core/internal/UserAgentConfigurer.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,90 @@ | ||
package software.amazon.lambda.powertools.core.internal; | ||
|
||
import com.sun.org.slf4j.internal.Logger; | ||
import com.sun.org.slf4j.internal.LoggerFactory; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Properties; | ||
|
||
import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv; | ||
|
||
public class UserAgentConfigurer { | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static final String NA = "NA"; | ||
public static final String VERSION_KEY = "powertools.version"; | ||
public static final String PT_FEATURE_VARIABLE = "${PT_FEATURE}"; | ||
public static final String PT_EXEC_ENV_VARIABLE = "${PT_EXEC_ENV}"; | ||
public static final String VERSION_PROPERTIES_FILENAME = "version.properties"; | ||
public static final String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV"; | ||
private static final Logger LOG = LoggerFactory.getLogger(UserAgentConfigurer.class); | ||
private static final String NO_OP = "no-op"; | ||
private static String PT_VERSION = getProjectVersion(); | ||
private static String USER_AGENT_PATTERN = "PT/" + PT_FEATURE_VARIABLE + "/" + PT_VERSION + " PTEnv/" + PT_EXEC_ENV_VARIABLE; | ||
|
||
/** | ||
* Retrieves the project version from the version.properties file | ||
* | ||
* @return the project version | ||
*/ | ||
static String getProjectVersion() { | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return getVersionFromProperties(VERSION_PROPERTIES_FILENAME, VERSION_KEY); | ||
} | ||
|
||
|
||
/** | ||
* Retrieves the project version from a properties file. | ||
* The file should be in the resources folder. | ||
* The version is retrieved from the property with the given key. | ||
* | ||
* @param propertyFileName the name of the properties file | ||
* @param versionKey the key of the property that contains the version | ||
* @return the version of the project as configured in the given properties file | ||
*/ | ||
static String getVersionFromProperties(String propertyFileName, String versionKey) { | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName); | ||
if (propertiesFileURI != null) { | ||
try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) { | ||
Properties properties = new Properties(); | ||
properties.load(fis); | ||
String version = properties.getProperty(versionKey); | ||
if (version != null && !version.isEmpty()) { | ||
return version; | ||
} | ||
} catch (IOException e) { | ||
LOG.warn("Unable to read {} file. Using default version.", propertyFileName); | ||
LOG.debug("Exception:", e); | ||
} | ||
} | ||
return NA; | ||
} | ||
|
||
/** | ||
* Retrieves the user agent string for the Powertools for AWS Lambda. | ||
* It follows the pattern PT/{PT_FEATURE}/{PT_VERSION} PTEnv/{PT_EXEC_ENV} | ||
* The version of the project is automatically retrieved. | ||
* The PT_EXEC_ENV is automatically retrieved from the AWS_EXECUTION_ENV environment variable. | ||
* If it AWS_EXECUTION_ENV is not set, PT_EXEC_ENV defaults to "NA" | ||
* | ||
* @param ptFeature a custom feature to be added to the user agent string (e.g. idempotency). | ||
* If null or empty, the default PT_FEATURE is used. | ||
* The default PT_FEATURE is "no-op". | ||
* @return the user agent string | ||
*/ | ||
public static String getUserAgent(String ptFeature) { | ||
|
||
String awsExecutionEnv = getenv(AWS_EXECUTION_ENV); | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String ptExecEnv = awsExecutionEnv != null ? awsExecutionEnv : NA; | ||
String userAgent = USER_AGENT_PATTERN.replace(PT_EXEC_ENV_VARIABLE, ptExecEnv); | ||
|
||
if (ptFeature == null || ptFeature.isEmpty()) { | ||
ptFeature = NO_OP; | ||
} | ||
return userAgent | ||
.replace(PT_FEATURE_VARIABLE, ptFeature) | ||
.replace(PT_EXEC_ENV_VARIABLE, ptExecEnv); | ||
} | ||
|
||
} |
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 @@ | ||
powertools.version=${project.version} | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
jeromevdl marked this conversation as resolved.
Show resolved
Hide resolved
|
102 changes: 102 additions & 0 deletions
102
...rc/test/java/software/amazon/lambda/powertools/core/internal/UserAgentConfigurerTest.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,102 @@ | ||
package software.amazon.lambda.powertools.core.internal; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv; | ||
import static software.amazon.lambda.powertools.core.internal.UserAgentConfigurer.VERSION_KEY; | ||
import static software.amazon.lambda.powertools.core.internal.UserAgentConfigurer.getVersionFromProperties; | ||
import static software.amazon.lambda.powertools.core.internal.UserAgentConfigurer.VERSION_PROPERTIES_FILENAME; | ||
import static software.amazon.lambda.powertools.core.internal.UserAgentConfigurer.AWS_EXECUTION_ENV; | ||
|
||
|
||
|
||
public class UserAgentConfigurerTest { | ||
scottgerring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final String SEM_VER_PATTERN = "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"; | ||
private static final String VERSION = UserAgentConfigurer.getProjectVersion(); | ||
|
||
|
||
@Test | ||
public void testGetVersion() { | ||
|
||
assertThat(VERSION).isNotNull(); | ||
assertThat(VERSION).isNotEmpty(); | ||
assertThat(Pattern.matches(SEM_VER_PATTERN, VERSION)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testGetVersionFromProperties_WrongKey() { | ||
String version = getVersionFromProperties(VERSION_PROPERTIES_FILENAME, "some invalid key"); | ||
|
||
assertThat(version).isNotNull(); | ||
assertThat(version).isEqualTo("NA"); | ||
} | ||
|
||
@Test | ||
public void testGetVersionFromProperties_FileNotExist() { | ||
String version = getVersionFromProperties("some file", VERSION_KEY); | ||
|
||
assertThat(version).isNotNull(); | ||
assertThat(version).isEqualTo("NA"); | ||
} | ||
|
||
@Test | ||
public void testGetVersionFromProperties_InvalidFile() { | ||
File f = new File(Thread.currentThread().getContextClassLoader().getResource("unreadable.properties").getPath()); | ||
f.setReadable(false); | ||
|
||
String version = getVersionFromProperties("unreadable.properties", VERSION_KEY); | ||
|
||
assertThat(version).isEqualTo("NA"); | ||
} | ||
|
||
@Test | ||
public void testGetVersionFromProperties_EmptyVersion() { | ||
String version = getVersionFromProperties("test.properties", VERSION_KEY); | ||
|
||
assertThat(version).isEqualTo("NA"); | ||
} | ||
|
||
@Test | ||
public void testGetUserAgent() { | ||
String userAgent = UserAgentConfigurer.getUserAgent("test-feature"); | ||
|
||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).isEqualTo("PT/test-feature/" + VERSION + " PTEnv/NA"); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetUserAgent_NoFeature() { | ||
String userAgent = UserAgentConfigurer.getUserAgent(""); | ||
|
||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).isEqualTo("PT/no-op/" + VERSION + " PTEnv/NA"); | ||
} | ||
|
||
@Test | ||
public void testGetUserAgent_NullFeature() { | ||
String userAgent = UserAgentConfigurer.getUserAgent(null); | ||
|
||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).isEqualTo("PT/no-op/" + VERSION + " PTEnv/NA"); | ||
} | ||
|
||
@Test | ||
public void testGetUserAgent_SetAWSExecutionEnv() { | ||
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) { | ||
mockedSystemWrapper.when(() -> getenv(AWS_EXECUTION_ENV)).thenReturn("AWS_Lambda_java8"); | ||
String userAgent = UserAgentConfigurer.getUserAgent("test-feature"); | ||
|
||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).isEqualTo("PT/test-feature/" + VERSION + " PTEnv/AWS_Lambda_java8"); | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
powertools.version= |
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 @@ | ||
powertools.version=${project.version} | ||
jeromevdl marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.