Finishing implementation of SendEvent (#1765)

This commit is contained in:
Cheick Keita
2022-04-11 09:32:09 -07:00
committed by GitHub
parent 9323d78c19
commit 5aceb25843
7 changed files with 63 additions and 28 deletions

View File

@ -60,7 +60,7 @@ public record NodeTasks
public enum NodeState
{
Init,
free,
Free,
SettingUp,
Rebooting,
Ready,
@ -80,7 +80,7 @@ public partial record Node
NodeState State,
Guid? ScalesetId,
DateTimeOffset Heartbeat,
Version Version,
string Version,
bool ReimageRequested,
bool DeleteRequested,
bool DebugKeepNode
@ -103,7 +103,7 @@ public record EventMessage(
//record AnyHttpUrl(AnyUrl):
// allowed_schemes = {'http', 'https
//
//

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using ApiService.OneFuzzLib;
namespace Microsoft.OneFuzz.Service;
@ -33,8 +34,12 @@ public class Program
.ConfigureServices((context, services) =>
services
.AddSingleton<ILogTracerFactory>(_ => new LogTracerFactory(GetLoggers()))
.AddScoped<ILogTracer>(s => s.GetService<LogTracerFactory>()?.MakeLogTracer(Guid.NewGuid()) ?? throw new InvalidOperationException("Unable to create a logger"))
.AddSingleton<IStorageProvider>(_ => new StorageProvider(EnvironmentVariables.OneFuzz.FuncStorage ?? throw new InvalidOperationException("Missing account id")))
.AddSingleton<INodeOperations, NodeOperations>()
.AddSingleton<IEvents, Events>()
.AddSingleton<IWebhookOperations, WebhookOperations>()
.AddSingleton<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
.AddSingleton<IQueue, Queue>()
.AddSingleton<ICreds>(_ => new Creds())
.AddSingleton<IStorage, Storage>()
)

View File

@ -1,7 +1,11 @@
using ApiService.OneFuzzLib;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Microsoft.OneFuzz.Service
@ -25,20 +29,20 @@ namespace Microsoft.OneFuzz.Service
public class Events : IEvents
{
private readonly IQueue _queue;
private readonly ILogTracer _logger;
private readonly ILogger _logger;
private readonly IWebhookOperations _webhook;
public Events(IQueue queue, ILogTracer logger, IWebhookOperations webhook)
public Events(IQueue queue, ILoggerFactory loggerFactory, IWebhookOperations webhook)
{
_queue = queue;
_logger = logger;
_logger = loggerFactory.CreateLogger<Events>();
_webhook = webhook;
}
public async Task QueueSignalrEvent(EventMessage eventMessage)
{
var message = new SignalREvent("events", new List<EventMessage>() { eventMessage });
var encodedMessage = Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(message));
var encodedMessage = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message));
await _queue.SendMessage("signalr-events", encodedMessage, StorageType.Config);
}
@ -60,15 +64,42 @@ namespace Microsoft.OneFuzz.Service
public void LogEvent(BaseEvent anEvent, EventType eventType)
{
//todo
//var scrubedEvent = FilterEvent(anEvent);
//throw new NotImplementedException();
var options = EntityConverter.GetJsonSerializerOptions();
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.Converters.Add(new RemoveUserInfo());
var serializedEvent = JsonSerializer.Serialize(anEvent, options);
_logger.LogInformation($"sending event: {eventType} - {serializedEvent}");
}
}
internal class RemoveUserInfo : JsonConverter<UserInfo>
{
public override UserInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var newOptions = new JsonSerializerOptions(options);
RemoveUserInfo? self = null;
foreach (var converter in newOptions.Converters)
{
if (converter is RemoveUserInfo)
{
self = (RemoveUserInfo)converter;
break;
}
}
if (self != null)
{
newOptions.Converters.Remove(self);
}
return JsonSerializer.Deserialize<UserInfo>(ref reader, newOptions);
}
private object FilterEvent(BaseEvent anEvent)
public override void Write(Utf8JsonWriter writer, UserInfo value, JsonSerializerOptions options)
{
throw new NotImplementedException();
writer.WriteStringValue("{}");
}
}
}

View File

@ -1,5 +1,6 @@
using Azure.Storage;
using Azure.Storage.Queues;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
using System.Text.Json;
@ -16,12 +17,12 @@ public interface IQueue
public class Queue : IQueue
{
IStorage _storage;
ILog _logger;
ILogger _logger;
public Queue(IStorage storage, ILog logger)
public Queue(IStorage storage, ILoggerFactory loggerFactory)
{
_storage = storage;
_logger = logger;
_logger = loggerFactory.CreateLogger<Queue>();
}
@ -36,7 +37,6 @@ public class Queue : IQueue
}
catch (Exception)
{
}
}
}
@ -60,7 +60,7 @@ public class Queue : IQueue
var accountId = _storage.GetPrimaryAccount(storageType);
//_logger.LogDEbug("getting blob container (account_id: %s)", account_id)
(var name, var key) = _storage.GetStorageAccountNameAndKey(accountId);
var accountUrl = new Uri($"https://%s.queue.core.windows.net{name}");
var accountUrl = new Uri($"https://{name}.queue.core.windows.net");
var client = new QueueServiceClient(accountUrl, new StorageSharedKeyCredential(name, key));
return client;
}

View File

@ -3,9 +3,9 @@ using System;
using Azure.ResourceManager;
using Azure.ResourceManager.Storage;
using Azure.Core;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Linq;
using Microsoft.Extensions.Logging;
namespace Microsoft.OneFuzz.Service;
@ -33,7 +33,7 @@ public class Storage : IStorage
public Storage(ILoggerFactory loggerFactory, ICreds creds)
{
_creds = creds;
_logger = loggerFactory.CreateLogger<Storage>();
_logger = loggerFactory.CreateLogger<Storage>(); ;
}
// TODO: @cached

View File

@ -1,4 +1,5 @@
using ApiService.OneFuzzLib.Orm;
using Microsoft.Extensions.Logging;
using Microsoft.OneFuzz.Service;
using System;
using System.Collections.Generic;
@ -21,11 +22,11 @@ public class WebhookMessageLogOperations : Orm<WebhookMessageLog>, IWebhookMessa
);
private readonly IQueue _queue;
private readonly ILogTracer _log;
public WebhookMessageLogOperations(IStorage storage, IQueue queue, ILogTracer log) : base(storage)
private readonly ILogger _logger;
public WebhookMessageLogOperations(IStorage storage, IQueue queue, ILoggerFactory loggerFactory) : base(storage)
{
_queue = queue;
_log = log;
_logger = loggerFactory.CreateLogger<WebhookMessageLogOperations>(); ;
}
@ -43,7 +44,7 @@ public class WebhookMessageLogOperations : Orm<WebhookMessageLog>, IWebhookMessa
if (visibilityTimeout == null)
{
_log.Error($"invalid WebhookMessage queue state, not queuing. {webhookLog.WebhookId}:{webhookLog.EventId} - {webhookLog.State}");
_logger.LogError($"invalid WebhookMessage queue state, not queuing. {webhookLog.WebhookId}:{webhookLog.EventId} - {webhookLog.State}");
}
else

View File

@ -1,5 +1,4 @@
using Azure.Core;
using Azure.Data.Tables;
using Azure.Data.Tables;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
@ -50,8 +49,7 @@ namespace ApiService.OneFuzzLib.Orm
{
var account = accountId ?? EnvironmentVariables.OneFuzz.FuncStorage ?? throw new ArgumentNullException(nameof(accountId));
var (name, key) = _storage.GetStorageAccountNameAndKey(account);
var identifier = new ResourceIdentifier(account);
var tableClient = new TableServiceClient(new Uri($"https://{identifier.Name}.table.core.windows.net"), new TableSharedKeyCredential(name, key));
var tableClient = new TableServiceClient(new Uri($"https://{name}.table.core.windows.net"), new TableSharedKeyCredential(name, key));
await tableClient.CreateTableIfNotExistsAsync(table);
return tableClient.GetTableClient(table);
}