Skip to content
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

dynamodb: Auto discover AWS Account #2148

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public interface CloudApi extends Cloud {
* If no data was recorded for the SDK client, the general account information will be returned.
*/
String getAccountInfo(Object sdkClient, CloudAccountInfo cloudAccountInfo);

/**
* Decode the account id from the given access key.
* This method becomes a noop and always returns null if the config "cloud.aws.account_decoding" is set to false.
*/
String decodeAwsAccountId(String accessKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
* This implementation of {@link CollectionFactory} will only be used if the agent-bridge
* is being used by an application and the agent is NOT being loaded. Thus, it is unlikely
* that the objects created by this implementation are going to receive much use.
* So methods in this implementation do not need to implement all functional requirements
* of the methods in the interface, but they should not break under low use.
*/
public class DefaultCollectionFactory implements CollectionFactory {

@Override
public <K, V> Map<K, V> createConcurrentWeakKeyedMap() {
return Collections.synchronizedMap(new WeakHashMap<K, V>());
return Collections.synchronizedMap(new WeakHashMap<>());
}

/**
Expand All @@ -36,12 +43,14 @@ public <K, V> Map<K, V> createConcurrentTimeBasedEvictionMap(long ageInSeconds)
@Override
public <K, V> Function<K, V> memorize(Function<K, V> loader, int maxSize) {
Map<K, V> map = new ConcurrentHashMap<>();
return k -> map.computeIfAbsent(k, k1 -> {

return k -> {
if (map.size() >= maxSize) {
map.remove(map.keySet().iterator().next());
V value = map.get(k);
return value == null ? loader.apply(k) : value;
}
return loader.apply(k1);
});
return map.computeIfAbsent(k, loader);
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public String getAccountInfo(CloudAccountInfo cloudAccountInfo) {
public String getAccountInfo(Object sdkClient, CloudAccountInfo cloudAccountInfo) {
return null;
}

@Override
public String decodeAwsAccountId(String accessKey) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.newrelic.agent.bridge;

import org.junit.Test;
import org.mockito.Mockito;

import java.util.Map;
import java.util.function.Function;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* This implementation of {@link CollectionFactory} is not really meant to be used.
* It is only implemented in case the agent-bridge is used without the agent, which is an unsupported use case.
*
* Therefore, the implementation does not keep the contract from the interface, but returns functional objects
* so the application will still run.
*/
public class DefaultCollectionFactoryTest {

@Test
public void createConcurrentWeakKeyedMap() {
Map<Object, Object> concurrentWeakKeyedMap = new DefaultCollectionFactory().createConcurrentWeakKeyedMap();
assertThat(concurrentWeakKeyedMap, instanceOf(Map.class));
}

@Test
public void createConcurrentTimeBasedEvictionMap() {
Map<Object, Object> concurrentTimeBasedEvictionMap = new DefaultCollectionFactory().createConcurrentTimeBasedEvictionMap(1L);
assertThat(concurrentTimeBasedEvictionMap, instanceOf(Map.class));
}

@Test
public void memorize() {
Function<Object, Object> f = mock(Function.class);
when(f.apply("1")).thenReturn("1");
when(f.apply("2")).thenReturn("2");
Function<Object, Object> cache = new DefaultCollectionFactory().memorize(f, 1);
cache.apply("1");
cache.apply("1");
cache.apply("2");
cache.apply("2");

// the first call should have been cached, so the function should only have been called once
Mockito.verify(f, Mockito.times(1)).apply("1");
// max cache size is 1, so second call should not be cached
Mockito.verify(f, Mockito.times(2)).apply("2");
}

@Test
public void createAccessTimeBasedCache() {
Function<Object, Object> accessTimeBasedCache = new DefaultCollectionFactory().createAccessTimeBasedCache(1L, 1, k -> k);
assertThat(accessTimeBasedCache, instanceOf(Function.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,149 +66,151 @@ public AmazonDynamoDBClient_Instrumentation(ClientConfiguration clientConfigurat
super(clientConfiguration);
}

private final AWSCredentialsProvider awsCredentialsProvider = Weaver.callOriginal();

@Trace(async = true, leaf = true)
final CreateTableResult executeCreateTable(CreateTableRequest createTableRequest) {
linkAndExpire(createTableRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "createTable",
createTableRequest.getTableName(), endpoint, this);
createTableRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final BatchGetItemResult executeBatchGetItem(BatchGetItemRequest batchGetItemRequest) {
linkAndExpire(batchGetItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "batchGetItem", "batch", endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "batchGetItem", "batch", endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final BatchWriteItemResult executeBatchWriteItem(BatchWriteItemRequest batchWriteItemRequest) {
linkAndExpire(batchWriteItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "batchWriteItem", "batch", endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "batchWriteItem", "batch", endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final DeleteItemResult executeDeleteItem(DeleteItemRequest deleteItemRequest) {
linkAndExpire(deleteItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "deleteItem",
deleteItemRequest.getTableName(), endpoint, this);
deleteItemRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final DeleteTableResult executeDeleteTable(DeleteTableRequest deleteTableRequest) {
linkAndExpire(deleteTableRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "deleteTable",
deleteTableRequest.getTableName(), endpoint, this);
deleteTableRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final DescribeLimitsResult executeDescribeLimits(DescribeLimitsRequest describeLimitsRequest) {
linkAndExpire(describeLimitsRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "describeLimits", null, endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "describeLimits", null, endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final DescribeTableResult executeDescribeTable(DescribeTableRequest describeTableRequest) {
linkAndExpire(describeTableRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "describeTable",
describeTableRequest.getTableName(), endpoint, this);
describeTableRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final DescribeTimeToLiveResult executeDescribeTimeToLive(DescribeTimeToLiveRequest describeTimeToLiveRequest) {
linkAndExpire(describeTimeToLiveRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "describeTimeToLive",
describeTimeToLiveRequest.getTableName(), endpoint, this);
describeTimeToLiveRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final GetItemResult executeGetItem(GetItemRequest getItemRequest) {
linkAndExpire(getItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "getItem", getItemRequest.getTableName(),
endpoint, this);
endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final ListTablesResult executeListTables(ListTablesRequest listTablesRequest) {
linkAndExpire(listTablesRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "listTables",
listTablesRequest.getExclusiveStartTableName(), endpoint, this);
listTablesRequest.getExclusiveStartTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final ListTagsOfResourceResult executeListTagsOfResource(ListTagsOfResourceRequest listTagsOfResourceRequest) {
linkAndExpire(listTagsOfResourceRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "listTagsOfResource", null, endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "listTagsOfResource", null, endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final PutItemResult executePutItem(PutItemRequest putItemRequest) {
linkAndExpire(putItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "putItem", putItemRequest.getTableName(),
endpoint, this);
endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final QueryResult executeQuery(QueryRequest queryRequest) {
linkAndExpire(queryRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "query", queryRequest.getTableName(),
endpoint, this);
endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();

}

@Trace(async = true, leaf = true)
final ScanResult executeScan(ScanRequest scanRequest) {
linkAndExpire(scanRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "scan", scanRequest.getTableName(), endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "scan", scanRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final TagResourceResult executeTagResource(TagResourceRequest tagResourceRequest) {
linkAndExpire(tagResourceRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "tagResource", null, endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "tagResource", null, endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final UntagResourceResult executeUntagResource(UntagResourceRequest untagResourceRequest) {
linkAndExpire(untagResourceRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "untagResource", null, endpoint, this);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "untagResource", null, endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final UpdateItemResult executeUpdateItem(UpdateItemRequest updateItemRequest) {
linkAndExpire(updateItemRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "updateItem",
updateItemRequest.getTableName(), endpoint, this);
updateItemRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final UpdateTableResult executeUpdateTable(UpdateTableRequest updateTableRequest) {
linkAndExpire(updateTableRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "updateTable",
updateTableRequest.getTableName(), endpoint, this);
updateTableRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

@Trace(async = true, leaf = true)
final UpdateTimeToLiveResult executeUpdateTimeToLive(UpdateTimeToLiveRequest updateTimeToLiveRequest) {
linkAndExpire(updateTimeToLiveRequest);
DynamoDBMetricUtil.metrics(NewRelic.getAgent().getTracedMethod(), "updateTimeToLive",
updateTimeToLiveRequest.getTableName(), endpoint, this);
updateTimeToLiveRequest.getTableName(), endpoint, this, awsCredentialsProvider);
return Weaver.callOriginal();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.nr.instrumentation.dynamodb_1_11_106;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.util.AwsHostNameUtils;
import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.datastore.DatastoreVendor;
Expand All @@ -28,14 +30,15 @@ public abstract class DynamoDBMetricUtil {
private static final String INSTANCE_HOST = "amazon";


public static void metrics(TracedMethod tracedMethod, String operation, String collection, URI endpoint, Object sdkClient) {
public static void metrics(TracedMethod tracedMethod, String operation, String collection, URI endpoint, Object sdkClient,
AWSCredentialsProvider credentialsProvider) {
String host = INSTANCE_HOST;
String arn = null;
Integer port = null;
if (endpoint != null) {
host = endpoint.getHost();
port = getPort(endpoint);
arn = getArn(collection, sdkClient, host);
arn = getArn(collection, sdkClient, host, credentialsProvider);
}
DatastoreParameters params = DatastoreParameters
.product(PRODUCT)
Expand All @@ -50,18 +53,12 @@ public static void metrics(TracedMethod tracedMethod, String operation, String c
}

// visible for testing
static String getArn(String tableName, Object sdkClient, String host) {
static String getArn(String tableName, Object sdkClient, String host, AWSCredentialsProvider credentialsProvider) {
if (host == null) {
NewRelic.getAgent().getLogger().log(Level.FINEST, "Unable to assemble ARN. Host is null.");
return null;
}

String accountId = AgentBridge.cloud.getAccountInfo(sdkClient, CloudAccountInfo.AWS_ACCOUNT_ID);
if (accountId == null) {
NewRelic.getAgent().getLogger().log(Level.FINEST, "Unable to assemble ARN. No account information provided.");
return null;
}

if (tableName == null) {
NewRelic.getAgent().getLogger().log(Level.FINEST, "Unable to assemble ARN. Unable to determine table.");
return null;
Expand All @@ -72,11 +69,31 @@ static String getArn(String tableName, Object sdkClient, String host) {
NewRelic.getAgent().getLogger().log(Level.FINEST, "Unable to assemble ARN. Unable to determine region.");
return null;
}

String accountId = getAccountId(sdkClient, credentialsProvider);
if (accountId == null) {
NewRelic.getAgent().getLogger().log(Level.FINEST, "Unable to assemble ARN. Unable to retrieve account information.");
return null;
}
// arn:${Partition}:dynamodb:${Region}:${Account}:table/${TableName}
return "arn:aws:dynamodb:" + region + ":" + accountId + ":table/" + tableName;
}

private static String getAccountId(Object sdkClient, AWSCredentialsProvider credentialsProvider) {
String accountId = AgentBridge.cloud.getAccountInfo(sdkClient, CloudAccountInfo.AWS_ACCOUNT_ID);
if (accountId != null) {
return accountId;
}

AWSCredentials credentials = credentialsProvider.getCredentials();
if (credentials != null) {
String accessKey = credentials.getAWSAccessKeyId();
if (accessKey != null) {
return AgentBridge.cloud.decodeAwsAccountId(accessKey);
}
}
return null;
}

private static Integer getPort(URI endpoint) {
if (endpoint.getPort() > 0) {
return endpoint.getPort();
Expand All @@ -90,5 +107,4 @@ private static Integer getPort(URI endpoint) {
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

@RunWith(InstrumentationTestRunner.class)
@InstrumentationTestConfig(includePrefixes = { "com.amazonaws", "com.nr.instrumentation" })
@Ignore("This test is running into some incompatibilities with a dependency.")
public class DynamoApiTest {

private static String hostName;
Expand Down
Loading
Loading