How to Track Last Access Time for DynamoDB Table in Go using AWS SDK

40 views Asked by At

Problem Statement:

I am developing a Go application where I need to implement a mechanism to track the last access time for a DynamoDB table. I am using the AWS SDK for Go (github.com/aws/aws-sdk-go-v2).

Code Snippet:

Here is a simplified version of my Go program:

// getLastAccessTime retrieves the last access time of a DynamoDB table using CloudWatch metrics
func getLastAccessTime(cloudWatchClient *cloudwatch.CloudWatch, tableName string) (time.Time, error) {
    endTime := time.Now()
    startTime := endTime.Add(-1 * time.Hour) // Look back 1 hour for example, adjust as needed

    // Specify the CloudWatch metric query
    metricDataQuery := &cloudwatch.GetMetricDataInput{
        EndTime:    aws.Time(endTime),
        StartTime:  aws.Time(startTime),
        MetricDataQueries: []*cloudwatch.MetricDataQuery{
            {
                Id: aws.String("m1"),
                MetricStat: &cloudwatch.MetricStat{
                    Metric: &cloudwatch.Metric{
                        Namespace:  aws.String("AWS/DynamoDB"),
                        MetricName: aws.String("ConsumedReadCapacityUnits"),
                        Dimensions: []*cloudwatch.Dimension{
                            {
                                Name:  aws.String("TableName"),
                                Value: aws.String(tableName),
                            },
                        },
                    },
                    Period: aws.Int64(60), // The granularity of the returned data in seconds
                    Stat:   aws.String("Sum"),
                },
            },
        },
    }

    // Retrieve CloudWatch metric data
    result, err := cloudWatchClient.GetMetricData(metricDataQuery)
    if err != nil {
        return time.Time{}, err
    }

    // Extract the timestamp of the last data point
    if len(result.MetricDataResults[0].Timestamps) > 0 {
        return *result.MetricDataResults[0].Timestamps[len(result.MetricDataResults[0].Timestamps)-1], nil
    }

    return time.Time{}, fmt.Errorf("no data points found for table %s", tableName)
}

--It shows same time for every table.How can i got last access time accurately.Is there any mistake on this code can you provide me your version.

Is there a direct method in the AWS SDK for Go to retrieve the last access time for a DynamoDB table? If there isn't a direct method, what are the recommended approaches to implement this functionality in a Go application?

2

There are 2 answers

1
Leeroy Hannigan On

You should use the SuccessfulRequestLatency with SampleCount metric instead of ConsumedCapacity, it would yield more accurate results.

0
kairoon bodanapu On

func getLastAccessTime(cloudWatchClient *cloudwatch.CloudWatch, tableName string, creationTime time.Time) (time.Time, error) { endTime := time.Now() startTime := creationTime

// Specify the CloudWatch metric query
metricDataQuery := &cloudwatch.GetMetricDataInput{
    EndTime:    aws.Time(endTime),
    StartTime:  aws.Time(startTime),
    MetricDataQueries: []*cloudwatch.MetricDataQuery{
        {
            Id: aws.String("m1"),
            MetricStat: &cloudwatch.MetricStat{
                Metric: &cloudwatch.Metric{
                    Namespace:  aws.String("AWS/DynamoDB"),
                    MetricName: aws.String("SuccessfulRequestLatency"),
                    Dimensions: []*cloudwatch.Dimension{
                        {
                            Name:  aws.String("TableName"),
                            Value: aws.String(tableName),
                        },
                    },
                },
                Period: aws.Int64(60),
                Stat:   aws.String("Maximum"),
            },
        },
    },
}

// Retrieve CloudWatch metric data
result, err := cloudWatchClient.GetMetricData(metricDataQuery)
if err != nil {
    return time.Time{}, err
}

// Extract the timestamp of the last data point
if len(result.MetricDataResults[0].Timestamps) > 0 {
    return *result.MetricDataResults[0].Timestamps[len(result.MetricDataResults[0].Timestamps)-1], nil
}

return time.Time{}, fmt.Errorf("no data points found for table %s", tableName)

} ----It shows no data points for every table ,can you please rectify it.