Azure 容器实例成功/失败时的电子邮件警报/日志



有没有办法设置单个 Azure 容器实例成功或失败(或基本上更改状态(的电子邮件警报?我有一些定期启动的运行一次性容器,并希望在它们完成时收到通知以及完成状态。如果电子邮件也可以包含来自容器的日志,则奖励。使用内置警报是否可以执行此操作?

到目前为止,我还没有能够让它与提供的信号一起工作。

不要相信有一种自动方法,所以我所做的是创建一个基于计时器的小函数,该函数每 5 分钟运行一次,并获取所有容器的列表并检查状态。如果有任何处于失败状态,则使用 SendGrid 发送警报电子邮件。

丹的更新

我在我的函数中使用托管服务标识,所以我有一个如下所示的容器任务类,不记得我从哪里得到帮助来生成 GetAzure 函数,因为很明显,在进行本地调试时,你不能使用 MSI 凭据和 Visual Studio 上的本地帐户,这似乎不是。但是我认为它可能在这里 - https://github.com/Azure/azure-sdk-for-net/issues/4968

public static class ContainerTasks
{
private static readonly IAzure azure = GetAzure();
private static IAzure GetAzure()
{
var tenantId = Environment.GetEnvironmentVariable("DevLocalDbgTenantId");
var clientId = Environment.GetEnvironmentVariable("DevLocalDbgClientId");
var clientSecret = Environment.GetEnvironmentVariable("DevLocalDbgClientSecret");
AzureCredentials credentials;
if (!string.IsNullOrEmpty(tenantId) &&
!string.IsNullOrEmpty(clientId) &&
!string.IsNullOrEmpty(clientSecret))
{
var sp = new ServicePrincipalLoginInformation
{
ClientId = clientId,
ClientSecret = clientSecret
};
credentials = new AzureCredentials(sp, tenantId, AzureEnvironment.AzureGlobalCloud);
}
else
{
credentials = SdkContext
.AzureCredentialsFactory
.FromMSI(new MSILoginInformation(MSIResourceType.AppService), AzureEnvironment.AzureGlobalCloud);
}
var authenticatedAzure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials);
var subscriptionId = Environment.GetEnvironmentVariable("DevLocalDbgSubscriptionId");
if (!string.IsNullOrEmpty(subscriptionId))
return authenticatedAzure.WithSubscription(subscriptionId);
return authenticatedAzure.WithDefaultSubscription();
}
public static IEnumerable<IContainerGroup> ListTaskContainers(string resourceGroupName, ILogger log)
{
log.LogInformation($"Getting a list of all container groups in Resource Group '{resourceGroupName}'");
return azure.ContainerGroups.ListByResourceGroup(resourceGroupName);
}
}

那么我的显示器功能很简单

public static class MonitorACIs
{
[FunctionName("MonitorACIs")]
public static void Run([TimerTrigger("%TimerSchedule%")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"MonitorACIs Timer trigger function executed at: {DateTime.Now}");
foreach(var containerGroup in ContainerTasks.ListTaskContainers(Environment.GetEnvironmentVariable("ResourceGroupName"), log))
{
if(String.Equals(containerGroup.State, "Failed"))
{
log.LogInformation($"Container Group {containerGroup.Name} has failed please investigate");
Notifications.FailedTaskACI(containerGroup.Name, log);
}
}
}
}

Notifications.FailedTaskACI 只是一个类方法,它向我们的一个团队频道发送电子邮件

它并不完美,但它现在可以完成工作!

最新更新