Skip to content

Commit

Permalink
Improve billable usage logging to include full aggregate object
Browse files Browse the repository at this point in the history
  • Loading branch information
kflahert committed Oct 18, 2024
1 parent bfc53f6 commit 3372d8e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public long processRetries(OffsetDateTime asOf) {
List<BillableUsageRemittanceEntity> remittances =
remittanceRepository.findByRetryAfterLessThan(asOf);
for (BillableUsageRemittanceEntity remittance : remittances) {
log.info("Processing retry of remittance {}", remittance);
// re-trigger billable usage
billingProducer.produce(toBillableUsage(remittance));
// reset the retry after column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void process(BillableUsageAggregate billableUsageAggregate) {
}

private void updateStatus(BillableUsageAggregate billableUsageAggregate) {
log.info("Updating status for aggregate: {}", billableUsageAggregate);
var status = RemittanceStatus.fromString(billableUsageAggregate.getStatus().value());
var errorCode =
ofNullable(billableUsageAggregate.getErrorCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,43 +127,26 @@ public void process(BillableUsageAggregate billableUsageAggregate) {
} catch (SubscriptionCanNotBeDeterminedException e) {
if (!isUsageDateValid(Clock.systemUTC(), billableUsageAggregate)) {
log.warn(
"Skipping billable usage with id={} orgId={} remittanceUUIDs={} because the subscription was not found and the snapshot '{}' is past the aws time window of '{}'",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
"Skipping billable usage aggregate {} because the subscription was not found and the snapshot '{}' is past the aws time window of '{}'",
billableUsageAggregate,
billableUsageAggregate.getWindowTimestamp(),
awsUsageWindow,
e);
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.INACTIVE);
} else {
log.warn(
"Subscription not found for for aggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
log.warn("Subscription not found for for aggregate={}", billableUsageAggregate, e);
emitRetryableStatusOnUsage(
billableUsageAggregate, BillableUsage.ErrorCode.SUBSCRIPTION_NOT_FOUND);
}
return;
} catch (SubscriptionRecentlyTerminatedException e) {
emitErrorStatusOnUsage(
billableUsageAggregate, BillableUsage.ErrorCode.SUBSCRIPTION_TERMINATED);
log.info(
"Subscription recently terminated for billableUsageAggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
log.info("Subscription recently terminated for aggregate={}", billableUsageAggregate, e);
return;
} catch (AwsUsageContextLookupException e) {
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.USAGE_CONTEXT_LOOKUP);
log.error(
"Error looking up aws usage context for aggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
log.error("Error looking up aws usage context for aggregate={}", billableUsageAggregate, e);
return;
}
try {
Expand All @@ -172,41 +155,32 @@ public void process(BillableUsageAggregate billableUsageAggregate) {
} catch (UsageTimestampOutOfBoundsException e) {
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.REDUNDANT);
log.warn(
"{} orgId={} remittanceUUIDs={} aggregateId={} productId={} windowTimestamp={} rhSubscriptionId={} awsCustomerId={} awsProductCode={} subscriptionStartDate={} value={}",
"{} aggregate={}, rhSubscriptionId={} awsCustomerId={} awsProductCode={} subscriptionStartDate={}",
e.getMessage(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getProductId(),
billableUsageAggregate.getWindowTimestamp(),
billableUsageAggregate,
context.getRhSubscriptionId(),
context.getCustomerId(),
context.getProductCode(),
context.getSubscriptionStartDate(),
billableUsageAggregate.getTotalValue());
context.getSubscriptionStartDate());
ignoreCounter.increment();
} catch (AwsThrottlingException e) {
emitRetryableStatusOnUsage(
billableUsageAggregate, BillableUsage.ErrorCode.MARKETPLACE_RATE_LIMIT);
log.error(
"Error sending aws usage due to rate limit for rhSubscriptionId={} aggregateId={} awsCustomerId={} awsProductCode={} orgId={} remittanceUUIDs={}",
"Error sending aws usage due to rate limit for rhSubscriptionId={} aggregate={} awsCustomerId={} awsProductCode={}",
context.getRhSubscriptionId(),
billableUsageAggregate.getAggregateId(),
billableUsageAggregate,
context.getCustomerId(),
context.getProductCode(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
} catch (Exception e) {
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.UNKNOWN);
log.error(
"Error sending aws usage for rhSubscriptionId={} aggregateId={} awsCustomerId={} awsProductCode={} orgId={} remittanceUUIDs={}",
"Error sending aws usage for rhSubscriptionId={} aggregate={} awsCustomerId={} awsProductCode={}",
context.getRhSubscriptionId(),
billableUsageAggregate.getAggregateId(),
billableUsageAggregate,
context.getCustomerId(),
context.getProductCode(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
}
}
Expand Down Expand Up @@ -254,19 +228,12 @@ private void transformAndSend(

if (isDryRun.isPresent() && Boolean.TRUE.equals(isDryRun.get())) {
log.info(
"[DRY RUN] Sending usage request to AWS: {}, organization={}, remittanceUUIDs={}, product_id={}",
"[DRY RUN] Sending usage request to AWS: {}, aggregate={}",
request,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateKey().getProductId());
billableUsageAggregate);
return;
} else {
log.info(
"Sending usage request to AWS: {}, organization={}, remittanceUUIDs={}, product_id={}",
request,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateKey().getProductId());
log.info("Sending usage request to AWS: {}, aggregate={}", request, billableUsageAggregate);
}

try {
Expand All @@ -280,23 +247,13 @@ private void transformAndSend(
result -> {
if (result.status() == UsageRecordResultStatus.CUSTOMER_NOT_SUBSCRIBED) {
log.warn(
"No subscription found for organization={}, remittanceUUIDs={} product_id={}, result={}",
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateKey().getProductId(),
"No subscription found for aggregate={}, result={}",
billableUsageAggregate,
result);
} else if (result.status() != UsageRecordResultStatus.SUCCESS) {
log.warn(
"{}, organization={} remittanceUUIDs={}",
result,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids());
log.warn("{}, aggregate={}", result, billableUsageAggregate);
} else {
log.info(
"{}, organization={}, remittanceUUIDs={},",
result,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids());
log.info("{}, aggregate={}", result, billableUsageAggregate);
acceptedCounter.increment(response.results().size());
}
});
Expand All @@ -312,10 +269,9 @@ private void transformAndSend(
throw new AwsUnprocessedRecordsException(request.usageRecords().size(), e);
} catch (AwsMissingCredentialsException e) {
log.warn(
"{} for organization={}, remittanceUUIDs={}, awsCustomerId={}",
"{} for aggregate={}, awsCustomerId={}",
e.getMessage(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate,
context.getCustomerId());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,28 @@ public void process(BillableUsageAggregate billableUsageAggregate) {
emitErrorStatusOnUsage(
billableUsageAggregate, BillableUsage.ErrorCode.SUBSCRIPTION_TERMINATED);
log.info(
"Subscription recently terminated for billableUsageAggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
"Subscription recently terminated for billableUsageAggregate={}",
billableUsageAggregate,
e);
return;
} catch (SubscriptionCanNotBeDeterminedException e) {
if (!isUsageDateValid(billableUsageAggregate)) {
log.warn(
"Skipping billable usage with id={} orgId={} remittanceUUIDs={} because the subscription was not found and the snapshot '{}' is past the azure time window of '{}'",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
"Skipping billable usage aggregate={} because the subscription was not found and the snapshot '{}' is past the azure time window of '{}'",
billableUsageAggregate,
billableUsageAggregate.getWindowTimestamp(),
azureUsageWindow,
e);
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.INACTIVE);
} else {
log.warn(
"Subscription not found for for aggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
log.warn("Subscription not found for for aggregate={}", billableUsageAggregate, e);
emitRetryableStatusOnUsage(
billableUsageAggregate, BillableUsage.ErrorCode.SUBSCRIPTION_NOT_FOUND);
}
return;
} catch (AzureUsageContextLookupException e) {
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.USAGE_CONTEXT_LOOKUP);
log.error(
"Error looking up usage context for aggregateId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
log.error("Error looking up usage context for aggregate={}", billableUsageAggregate, e);
return;
}
try {
Expand All @@ -168,11 +154,9 @@ public void process(BillableUsageAggregate billableUsageAggregate) {
} catch (Exception e) {
emitErrorStatusOnUsage(billableUsageAggregate, BillableUsage.ErrorCode.UNKNOWN);
log.error(
"Error sending azure usage for aggregateId={} azureResourceId={} orgId={} remittanceUUIDs={}",
billableUsageAggregate.getAggregateId(),
"Error sending azure usage for aggregate={}, azureResourceId={}",
billableUsageAggregate,
context.getAzureResourceId(),
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
e);
}
}
Expand All @@ -191,36 +175,24 @@ private void transformAndSend(
var usageEvent = transformToAzureUsage(context, billableUsageAggregate, metric);
if (isDryRun.isPresent() && Boolean.TRUE.equals(isDryRun.get())) {
log.info(
"[DRY RUN] Sending usage request to Azure: {}, organization={}, remittanceUUIDs={}, product_id={}",
"[DRY RUN] Sending usage request to Azure: {}, for aggregate={}",
usageEvent,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateKey().getProductId());
billableUsageAggregate);
return;
} else {
log.info(
"Sending usage request to Azure: {}, organization={}, remittanceUUIDs={}, product_id={}",
"Sending usage request to Azure: {}, for aggregate={}",
usageEvent,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids(),
billableUsageAggregate.getAggregateKey().getProductId());
billableUsageAggregate);
}

try {
var response = azureMarketplaceService.sendUsageEventToAzureMarketplace(usageEvent);
log.debug("{}", response);
if (response.getStatus() != UsageEventStatusEnum.ACCEPTED) {
log.warn(
"{}, organization={}, remittanceUUIDs={}",
response,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids());
log.warn("{}, aggregate={}", response, billableUsageAggregate);
} else {
log.info(
"{}, organization={}, remittanceUUIDs={},",
response,
billableUsageAggregate.getAggregateKey().getOrgId(),
billableUsageAggregate.getRemittanceUuids());
log.info("{}, aggregate={}", response, billableUsageAggregate);
acceptedCounter.increment();
}
} catch (AzureMarketplaceRequestFailedException e) {
Expand Down

0 comments on commit 3372d8e

Please sign in to comment.