public async Task <IActionResult> MarkStart([Required] string service, [Required] string id) { _logger.LogInformation("Recording start of deployment of '{service}' with id '{id}'", service, id); NewGrafanaAnnotationRequest content = new NewGrafanaAnnotationRequest { Text = $"Deployment of {service}", Tags = new[] { "deploy", $"deploy-{service}", service }, Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), }; NewGrafanaAnnotationResponse annotation; using (var client = new HttpClient(new HttpClientHandler() { CheckCertificateRevocationList = true })) { annotation = await _retry.RetryAsync(async() => { GrafanaOptions grafanaOptions = _grafanaOptions.CurrentValue; _logger.LogInformation("Creating annotation to {url}", grafanaOptions.BaseUrl); using (var request = new HttpRequestMessage(HttpMethod.Post, $"{grafanaOptions.BaseUrl}/api/annotations")) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", grafanaOptions.ApiToken); request.Content = CreateObjectContent(content); using (HttpResponseMessage response = await client.SendAsync(request, CancellationToken.None)) { _logger.LogTrace("Response from grafana {responseCode} {reason}", response.StatusCode, response.ReasonPhrase); response.EnsureSuccessStatusCode(); return(await ReadObjectContent <NewGrafanaAnnotationResponse>(response.Content)); } } }, e => _logger.LogWarning(e, "Failed to send new annotation"), e => true ); } _logger.LogInformation("Created annotation {annotationId}, inserting into table", annotation.Id); CloudTable table = await GetCloudTable(); await table.ExecuteAsync( TableOperation.InsertOrReplace( new AnnotationEntity(service, id, annotation.Id) { ETag = "*" } ) ); return(NoContent()); }
public async Task <IActionResult> MarkEnd([Required] string service, [Required] string id) { _logger.LogInformation("Recording end of deployment of '{service}' with id '{id}'", service, id); CloudTable table = await GetCloudTable(); _logger.LogInformation("Looking for existing deployment"); var tableResult = await table.ExecuteAsync(TableOperation.Retrieve <AnnotationEntity>(service, id)); _logger.LogTrace("Table response code {responseCode}", tableResult.HttpStatusCode); if (!(tableResult.Result is AnnotationEntity annotation)) { return(NotFound()); } _logger.LogTrace("Updating end time of deployment..."); annotation.Ended = DateTimeOffset.UtcNow; tableResult = await table.ExecuteAsync(TableOperation.Replace(annotation)); _logger.LogInformation("Update response code {responseCode}", tableResult.HttpStatusCode); var content = new NewGrafanaAnnotationRequest { TimeEnd = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), }; using (var client = new HttpClient(new HttpClientHandler() { CheckCertificateRevocationList = true })) { await _retry.RetryAsync(async() => { GrafanaOptions grafanaOptions = _grafanaOptions.CurrentValue; _logger.LogInformation("Updating annotation {annotationId} to {url}", annotation.GrafanaAnnotationId, grafanaOptions.BaseUrl); using (var request = new HttpRequestMessage(HttpMethod.Patch, $"{grafanaOptions.BaseUrl}/api/annotations/{annotation.GrafanaAnnotationId}")) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", grafanaOptions.ApiToken); request.Content = CreateObjectContent(content); using (HttpResponseMessage response = await client.SendAsync(request, CancellationToken.None)) { _logger.LogTrace("Response from grafana {responseCode} {reason}", response.StatusCode, response.ReasonPhrase); response.EnsureSuccessStatusCode(); } } }, e => _logger.LogWarning(e, "Failed to send new annotation"), e => true ); } return(NoContent()); }
private async Task <CloudTable> GetCloudTable() { CloudTable table; if (_env.IsDevelopment()) { table = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient().GetTableReference("deployments"); await table.CreateIfNotExistsAsync(); } else { GrafanaOptions options = _grafanaOptions.CurrentValue; table = new CloudTable(new Uri(options.TableUri, UriKind.Absolute), new StorageCredentials(options.TableSasToken)); } return(table); }