Skip to content

Commit

Permalink
Throttle events over file system (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci committed May 4, 2020
1 parent a7648aa commit a79a3b8
Show file tree
Hide file tree
Showing 48 changed files with 693 additions and 639 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -337,3 +337,4 @@ tools/**
!tools/packages.config

__mismatch__/
*.Development.json
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>Thor.Hosting.AspNetCore.FunctionalTest</AssemblyName>
<RootNamespace>Thor.Hosting.AspNetCore.FunctionalTest</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
@@ -1,44 +1,23 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using static Thor.Hosting.AspNetCore.FunctionalTest.EventSources.ValuesEventSources;

namespace Thor.Hosting.AspNetCore.FunctionalTest.Controllers
{
[Route("api/[controller]")]
[Route("[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
[HttpGet("{count}")]
public string Get(int count)
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
throw new InvalidOperationException();

//return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
var timer = Stopwatch.StartNew();
var processed = 0;
for (processed = 0; processed < count; processed++)
{
Log.RetrieveObject(processed);
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
return $"{processed} {timer.Elapsed}";
}
}
}
@@ -0,0 +1,15 @@
using System.Diagnostics.Tracing;
using Thor.Core.Abstractions;

namespace Thor.Hosting.AspNetCore.FunctionalTest.EventSources
{
[EventSourceDefinition(Name = "AspNetCore-FunctionalTest")]
public interface IValuesEventSources
{
[Event(1,
Level = EventLevel.Informational,
Message = "Retrieve value for object {id}",
Version = 1)]
void RetrieveObject(int id);
}
}
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics.Tracing;
using Thor.Core;
using Thor.Core.Abstractions;
using Thor.Core.Transmission.Abstractions;

namespace Thor.Hosting.AspNetCore.FunctionalTest.EventSources
{

[EventSource(Name = "AspNetCore-FunctionalTest")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("thor-generator", "1.0.0")]
public sealed class ValuesEventSources
: EventSourceBase
, IValuesEventSources
{
private ValuesEventSources() { }

public static IValuesEventSources Log { get; } = new ValuesEventSources();


[NonEvent]
public void RetrieveObject(int id)
{
if (IsEnabled())
{

RetrieveObject(Application.Id, ActivityStack.Id, id);

}
}

[Event(1, Level = EventLevel.Informational, Message = "Retrieve value for object {2}", Version = 1)]
private void RetrieveObject(int applicationId, Guid activityId, int id)
{
WriteCore(1, applicationId, activityId, id);
}

[NonEvent]
private unsafe void WriteCore(int eventId, int applicationId, Guid activityId, int a)
{
if (IsEnabled())
{

const short dataCount = 3;
EventData* data = stackalloc EventData[dataCount];
data[0].DataPointer = (IntPtr)(&applicationId);
data[0].Size = 4;
data[1].DataPointer = (IntPtr)(&activityId);
data[1].Size = 16;
data[2].DataPointer = (IntPtr)(&a);
data[2].Size = 4;

WriteEventCore(eventId, dataCount, data);
}
}

}
}
9 changes: 5 additions & 4 deletions src/Clients/AspNetCore.FunctionalTest/Startup.cs
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Thor.Hosting.AspNetCore.FunctionalTest
{
Expand All @@ -18,10 +19,10 @@ public void ConfigureServices(IServiceCollection services)
{
services
.AddTracing(Configuration)
.AddMvc();
.AddMvc(o => o.EnableEndpointRouting = false);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand All @@ -31,4 +32,4 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseMvc();
}
}
}
}
21 changes: 0 additions & 21 deletions src/Clients/AspNetCore.FunctionalTest/appsettings.Development.json

This file was deleted.

44 changes: 21 additions & 23 deletions src/Clients/AspNetCore.FunctionalTest/appsettings.json
@@ -1,26 +1,24 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
{
"Tracing": {
"ApplicationId": 1000,
"Level": "Verbose",
"BlobStorage": {
//"AttachmentContainerName": "attach",
"ConnectionString": "xxx"
},
"ApplicationId": "432",
"Level": "Informational",
"EventHub": {
"ConnectionString": "xxx"
}
"TransportType": "AmqpWebSockets"
},
"ApplicationRootPath": null,
"InProcess": "true",
"Enabled": "true",

"Forwarding": [
{
"Regex": "Microsoft.AspNetCore.Hosting.Internal.WebHost",
"Level": "Warning"
},
{
"Regex": "Microsoft.*",
"Level": "Info"
}
],
"SkipRequestFilter": "_health"
}
}
}
5 changes: 3 additions & 2 deletions src/Core/Core.Abstractions/AssemblyAttributes.cs
@@ -1,11 +1,12 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Thor.Core")]
[assembly: InternalsVisibleTo("Thor.Core.Abstractions.Tests")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.Abstractions")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.BlobStorage")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.EventHub")]
[assembly: InternalsVisibleTo("Thor.Extensions.Http")]
[assembly: InternalsVisibleTo("Thor.Extensions.HotChocolate")]
[assembly: InternalsVisibleTo("Thor.Extensions.Http.Tests")]
[assembly: InternalsVisibleTo("Thor.Extensions.HotChocolate.Tests")]
[assembly: InternalsVisibleTo("Thor.Core.Session")]
[assembly: InternalsVisibleTo("Thor.Core.Session")]
4 changes: 2 additions & 2 deletions src/Core/Core.Abstractions/ExceptionMessages.cs
@@ -1,4 +1,4 @@
namespace Thor.Core.Abstractions
namespace Thor.Core.Abstractions
{
internal static class ExceptionMessages
{
Expand All @@ -7,4 +7,4 @@ internal static class ExceptionMessages
public const string ImmutableStackIsEmpty = "This operation does not apply to an empty instance.";
public const string NoActivityIdFound = "No activity id found.";
}
}
}
4 changes: 2 additions & 2 deletions src/Core/Core/TracingConfiguration.cs
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.IO;

namespace Thor.Core
Expand Down Expand Up @@ -54,4 +54,4 @@ public TracingConfiguration()
/// </summary>
public bool Enabled { get; set; }
}
}
}
37 changes: 28 additions & 9 deletions src/Core/Core/TracingConfigurationExtensions.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;

namespace Thor.Core
Expand All @@ -15,22 +15,40 @@ public static class TracingConfigurationExtensions
/// <returns>A temp path for attachments.</returns>
public static string GetAttachmentsStoragePath(
this TracingConfiguration configuration)
{
return configuration.GetStoragePath("Attachments");
}

/// <summary>
/// Gets the path for storing attachments temporarily.
/// </summary>
/// <param name="configuration">A configuration instance.</param>
/// <returns>A temp path for attachments.</returns>
public static string GetEventsStoragePath(
this TracingConfiguration configuration)
{
return configuration.GetStoragePath("Events");
}

private static string GetStoragePath(
this TracingConfiguration configuration,
string directoryName)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

return (configuration.Debug || configuration.InProcess)
? configuration.GetInProcessStoragePath()
: configuration.GetOutOfProcessStoragePath();
? configuration.GetInProcessStoragePath(directoryName)
: configuration.GetOutOfProcessStoragePath(directoryName);
}

private static string GetInProcessStoragePath(
this TracingConfiguration configuration)
this TracingConfiguration configuration,
string directoryName)
{
string path = Path.Combine(configuration.ApplicationRootPath,
"Attachments");
var path = Path.Combine(configuration.ApplicationRootPath, directoryName);

if (!Directory.Exists(path))
{
Expand All @@ -41,14 +59,15 @@ public static class TracingConfigurationExtensions
}

private static string GetOutOfProcessStoragePath(
this TracingConfiguration configuration)
this TracingConfiguration configuration,
string directoryName)
{
// note: Considering to have one global path for out-of-process
// attachments so that just the out-of-process collector is
// uploading attachments instead of every application is uploading
// its own attachments.

return configuration.GetInProcessStoragePath();
return configuration.GetInProcessStoragePath(directoryName);
}
}
}
}
6 changes: 4 additions & 2 deletions src/Core/Transmission.Abstractions/AssemblyAttributes.cs
@@ -1,5 +1,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Thor.Extensions.Http.Tests")]
[assembly: InternalsVisibleTo("Thor.Extensions.HotChocolate.Tests")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.Abstractions.Tests")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.Abstractions.Tests")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.BlobStorage")]
[assembly: InternalsVisibleTo("Thor.Core.Transmission.EventHub")]

0 comments on commit a79a3b8

Please sign in to comment.