-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cs
90 lines (81 loc) · 2.54 KB
/
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Diagnostics;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace PagoPA
{
public class CustomTest
{
private readonly Options _options;
private readonly ILogger<CustomTest> _log;
private readonly TelemetryClient _telemetryClient;
public CustomTest(
IOptions<Options> options,
ILogger<CustomTest> log,
TelemetryConfiguration telemetryConfiguration
)
{
log.LogDebug("[DEBUG] Init function..");
_options = options.Value;
_log = log;
_telemetryClient = new TelemetryClient(telemetryConfiguration);
}
[FunctionName("TLSCheck")]
public void Run([TimerTrigger("%TIME_TRIGGER%")]TimerInfo timer)
{
_log.LogDebug("[DEBUG] Init telemetry context..");
var availability = new AvailabilityTelemetry
{
Name = _options.FunctionName,
RunLocation = _options.Region,
Success = false,
};
availability.Context.Operation.ParentId = Activity.Current.SpanId.ToString();
availability.Context.Operation.Id = Activity.Current.RootId;
_log.LogDebug("[DEBUG] Start stopwatch..");
var stopwatch = new Stopwatch();
stopwatch.Start();
try
{
using (var activity = new Activity("AvailabilityContext"))
{
activity.Start();
availability.Id = Activity.Current.SpanId.ToString();
_log.LogDebug("[DEBUG] Run business logic..");
var test = new Test(_options, _log);
test.Run(_options.Host);
}
_log.LogInformation("[INFO] Success..");
availability.Success = true;
}
catch (SslExpirationException ex)
{
_log.LogInformation("[INFO] Fail..");
availability.Message = ex.Message;
throw;
}
catch (Exception ex)
{
_log.LogError($"[ERROR] {ex.Message}");
availability.Message = ex.Message;
throw;
}
finally
{
_log.LogDebug("[DEBUG] Stop stopwatch..");
stopwatch.Stop();
_log.LogDebug("[DEBUG] Clean environment..");
availability.Duration = stopwatch.Elapsed;
availability.Timestamp = DateTimeOffset.UtcNow;
_telemetryClient.TrackAvailability(availability);
_telemetryClient.Flush();
}
}
}
}