forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure Monitor Exporter - Add IngestionResponsePolicy (Azure#16032)
* Added IngestionResponsePolicy * check for object is int * Added constant to response code * Added method summary.
- Loading branch information
1 parent
8317a67
commit fdc2bd7
Showing
4 changed files
with
188 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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
116 changes: 116 additions & 0 deletions
116
sdk/monitor/OpenTelemetry.Exporter.AzureMonitor/src/IngestionResponsePolicy.cs
This file contains 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,116 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
using OpenTelemetry.Exporter.AzureMonitor.Models; | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor | ||
{ | ||
internal class IngestionResponsePolicy : HttpPipelineSynchronousPolicy | ||
{ | ||
public override void OnReceivedResponse(HttpMessage message) | ||
{ | ||
base.OnReceivedResponse(message); | ||
|
||
int itemsAccepted; | ||
|
||
if (message.TryGetProperty("TelemetryItems", out var telemetryItems)) | ||
{ | ||
itemsAccepted = ParseResponse(message, (IEnumerable<TelemetryItem>)telemetryItems); | ||
} | ||
else | ||
{ | ||
itemsAccepted = ParseResponse(message); | ||
} | ||
|
||
message.SetProperty("ItemsAccepted", itemsAccepted); | ||
} | ||
|
||
internal static int ParseResponse(HttpMessage message, IEnumerable<TelemetryItem> telemetryItems) | ||
{ | ||
var httpStatus = message?.Response?.Status; | ||
int itemsAccepted = 0; | ||
|
||
switch (httpStatus) | ||
{ | ||
case ResponseStatusCodes.Success: | ||
itemsAccepted = telemetryItems.Count(); | ||
break; | ||
case ResponseStatusCodes.PartialSuccess: | ||
// Parse retry-after header | ||
// Send Failed Messages To Storage | ||
break; | ||
case ResponseStatusCodes.RequestTimeout: | ||
case ResponseStatusCodes.ResponseCodeTooManyRequests: | ||
case ResponseStatusCodes.ResponseCodeTooManyRequestsAndRefreshCache: | ||
// Parse retry-after header | ||
// Send Messages To Storage | ||
break; | ||
case ResponseStatusCodes.InternalServerError: | ||
case ResponseStatusCodes.BadGateway: | ||
case ResponseStatusCodes.ServiceUnavailable: | ||
case ResponseStatusCodes.GatewayTimeout: | ||
// Send Messages To Storage | ||
break; | ||
case null: // UnknownNetworkError | ||
// No HttpMessage. Send TelemetryItems To Storage | ||
break; | ||
default: | ||
// Log Non-Retriable Status and don't retry or store; | ||
break; | ||
} | ||
|
||
return itemsAccepted; | ||
} | ||
|
||
internal static int ParseResponse(HttpMessage message) | ||
{ | ||
var httpStatus = message?.Response?.Status; | ||
int itemsAccepted = 0; | ||
|
||
switch (httpStatus) | ||
{ | ||
case ResponseStatusCodes.Success: | ||
itemsAccepted = GetItemsAccepted(message); | ||
break; | ||
case ResponseStatusCodes.PartialSuccess: | ||
// Send Failed Messages To Storage | ||
break; | ||
case ResponseStatusCodes.RequestTimeout: | ||
case ResponseStatusCodes.ResponseCodeTooManyRequests: | ||
case ResponseStatusCodes.ResponseCodeTooManyRequestsAndRefreshCache: | ||
case ResponseStatusCodes.InternalServerError: | ||
case ResponseStatusCodes.BadGateway: | ||
case ResponseStatusCodes.ServiceUnavailable: | ||
case ResponseStatusCodes.GatewayTimeout: | ||
case null: // UnknownNetworkError | ||
itemsAccepted = 0; | ||
// Request body is already in storage. No need to store again. | ||
break; | ||
default: | ||
// Log Non-Retriable Status and don't retry or store; | ||
break; | ||
} | ||
|
||
return itemsAccepted; | ||
} | ||
|
||
internal static int GetItemsAccepted(HttpMessage message) | ||
{ | ||
int itemsAccepted = 0; | ||
using (JsonDocument document = JsonDocument.Parse(message.Response.ContentStream, default)) | ||
{ | ||
var value = TrackResponse.DeserializeTrackResponse(document.RootElement); | ||
Response.FromValue(value, message.Response); | ||
itemsAccepted = value.ItemsAccepted.GetValueOrDefault(); | ||
} | ||
|
||
return itemsAccepted; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
sdk/monitor/OpenTelemetry.Exporter.AzureMonitor/src/ResponseStatusCodes.cs
This file contains 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,18 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor | ||
{ | ||
internal class ResponseStatusCodes | ||
{ | ||
public const int Success = 200; | ||
public const int PartialSuccess = 206; | ||
public const int RequestTimeout = 408; | ||
public const int ResponseCodeTooManyRequests = 429; | ||
public const int ResponseCodeTooManyRequestsAndRefreshCache = 439; | ||
public const int InternalServerError = 500; | ||
public const int BadGateway = 502; | ||
public const int ServiceUnavailable = 503; | ||
public const int GatewayTimeout = 504; | ||
} | ||
} |