Skip to content

Commit

Permalink
handle new metrics better
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd committed Feb 21, 2024
1 parent 1f1c464 commit c8a4b90
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/artifact-size-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ concurrency:
cancel-in-progress: true

env:
BOOTSTRAP_SERVICES: +s3,+dynamodb,+sts,+polly
# BOOTSTRAP_SERVICES: +s3,+dynamodb,+sts,+polly
BOOTSTRAP_SERVICES: +s3

jobs:
check-sizes:
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure JDK
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: 17
cache: 'gradle'
- name: Check artifact sizes
run: |
./gradlew :codegen:sdk:bootstrap -Paws.services=${{ env.BOOTSTRAP_SERVICES }}
Expand Down Expand Up @@ -54,6 +61,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Configure JDK
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: 17
cache: 'gradle'
- name: Check artifact sizes
run: |
./gradlew :codegen:sdk:bootstrap -Paws.services=${{ env.BOOTSTRAP_SERVICES }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private abstract class CheckMetricsTask : DefaultTask() {
namespace = CW_NAMESPACE
}.metrics.orEmpty()

val metricsAvailable = localMetrics.filter { lmetric ->
val metricsWithHistory = localMetrics.filter { lmetric ->
metrics.find { rmetric ->
val ldimensions = lmetric.dimensions.map(CloudwatchDimension::toSdkDimension).sortedBy { it.name }
val rdimensions = rmetric.dimensions.orEmpty().sortedBy { it.name }
Expand All @@ -175,33 +175,32 @@ private abstract class CheckMetricsTask : DefaultTask() {
} != null
}

val newMetrics = localMetrics.filterNot { metricsWithHistory.contains(it) }

val content = StringBuilder()

// MetricName, Dimensions, KB, DeltaPct
content.append(String.format("| %22s | %40s | %12s | %12s |\n", "MetricName", "Dimensions", "Bytes", "DeltaPct"))
content.append("| --- | --- | --- | --- |\n")
val fmt = "| %22s | %40s | %,12d | %+12.2f%% |\n"
metricsAvailable.forEach { lmetric ->

val metricSummaries = metricsWithHistory.map { lmetric ->
val data = getMetricStats(cw, lmetric)
val minAvg = data.maxBy { it.average!! }.average!!
val maxAvg = data.minBy { it.average!! }.average!!
MetricSummary(lmetric, data)
}

val avg = if (abs(lmetric.value.toDouble() - minAvg) > abs(lmetric.value.toDouble() - maxAvg)) {
minAvg
} else {
maxAvg
}
val newMetricSummaries = newMetrics.map { MetricSummary(it, emptyList()) }

val deltaPct = (lmetric.value.toDouble() - avg) / avg * 100
val allSummaries = metricSummaries + newMetricSummaries

val formattedDimensions = lmetric
allSummaries.forEach { summary ->
val formattedDimensions = summary.metric
.dimensions
.sortedBy { it.name }
.joinToString(separator = ",") {
"${it.name}=${it.value}"
}

val row = fmt.format(lmetric.metricName, formattedDimensions, lmetric.value, deltaPct)
val row = fmt.format(summary.metric.metricName, formattedDimensions, summary.metric.value, summary.deltaPctChange())
content.append(row)
}

Expand All @@ -210,6 +209,29 @@ private abstract class CheckMetricsTask : DefaultTask() {
}
}

private data class MetricSummary(
val metric: CloudwatchMetric,
val historicalData: List<Datapoint>,
) {

fun deltaPctChange(): Double {
val minAvg = historicalData.maxByOrNull { it.average!! }?.average
val maxAvg = historicalData.minByOrNull { it.average!! }?.average

return if (minAvg != null && maxAvg != null) {
val avg = if (abs(metric.value.toDouble() - minAvg) > abs(metric.value.toDouble() - maxAvg)) {
minAvg
} else {
maxAvg
}

(metric.value.toDouble() - avg) / avg * 100
} else {
0.0
}
}
}

private suspend fun getMetricStats(cw: CloudWatchClient, metric: CloudwatchMetric): List<Datapoint> {
val result = cw.getMetricStatistics {
metricName = metric.metricName
Expand Down

0 comments on commit c8a4b90

Please sign in to comment.