简体   繁体   中英

Email Alerts/Logs when Azure Container Instance Succeeds/Fails

Is there any way to setup email alerts for when individual Azure Container Instances succeed or fail (or basically change state)? I have some run-once containers I kick off periodically, and would like to be notified when they complete and the status of the completion. Bonus if the email can include logs from the container, as well. Is this possible using the built-in alerts?

So far I haven't been able to make it work with the provided signals.

Dont believe there is an automatic way so what I did was created a small timer based function that runs every 5 minutes and gets a list of all containers and checks the status. If any are in say a failed state it then uses SendGrid to sent an alert email.

UPDATE for Dan

I use Managed Service Identity in my function so I have a container tasks class like the below, can't remember exactly where I got the help to generate the GetAzure function as obviously when doing local debug you can't use the MSI credentials and the local account on Visual Studio that is meant to work doesn't appear to. However I think it might have been here - 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);
    }
}

Then my monitor function is simply

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 is just a class method that sends an email to one of our Teams channels

It's not perfect but it does the job for now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM