Skip to content

Commit

Permalink
Libs upgrade and monitoring enabled
Browse files Browse the repository at this point in the history
# Conflicts:
#	ChocolateGateway/ChocolateGateway.csproj
#	ChocolateOrder.Front/ChocolateOrder.Front.csproj
#	ChocolateOrder/ChocolateOrder.csproj
#	ChocolateShipping/ChocolateShipping.csproj
#	Shared/Shared.csproj
  • Loading branch information
danielmarbach committed Jan 31, 2018
1 parent 75cbc04 commit fe89dd5
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 11 deletions.
5 changes: 3 additions & 2 deletions ChocolateOrder.Front/ChocolateOrder.Front.csproj
Expand Up @@ -22,8 +22,9 @@
<PackageReference Include="Microsoft.ServiceFabric.Data" Version="2.8.211" />
<PackageReference Include="Microsoft.ServiceFabric.Services" Version="2.8.211" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.6" />
<PackageReference Include="NServiceBus" Version="6.4.1" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.11" />
<PackageReference Include="NServiceBus" Version="6.4.3" />
<PackageReference Include="NServiceBus.Heartbeat" Version="2.0.0" />
<PackageReference Include="NServiceBus.Persistence.ServiceFabric" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
Expand Up @@ -12,6 +12,8 @@ public static void AddNServiceBus(this IServiceCollection services)
{
var endpointConfiguration = new EndpointConfiguration("chocolateorder.front");

#region NotImportant

var assemblyScanner = endpointConfiguration.AssemblyScanner();
assemblyScanner.ExcludeAssemblies("netstandard");

Expand All @@ -25,6 +27,10 @@ public static void AddNServiceBus(this IServiceCollection services)
var context = provider.GetService<StatelessServiceContext>();
var configurationPackage = context.CodePackageActivationContext.GetConfigurationPackageObject("Config");

endpointConfiguration.SendHeartbeatTo(serviceControlQueue: "Particular.ServiceControl.RabbitMQ", frequency: TimeSpan.FromSeconds(5), timeToLive: TimeSpan.FromSeconds(15));
var hostInfo = endpointConfiguration.UniquelyIdentifyRunningInstance();
hostInfo.UsingCustomDisplayName("chocolateorder.front");

var connectionString = configurationPackage.Settings.Sections["NServiceBus"].Parameters["ConnectionString"];

var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
Expand All @@ -35,6 +41,8 @@ public static void AddNServiceBus(this IServiceCollection services)
transport.ConnectionString(connectionString.Value);
transport.UseForwardingTopology();

#endregion

// let's query the remote service
// TODO 9
var chocolateOrderPartitionInformation = ServicePartitionQueryHelper
Expand Down
6 changes: 4 additions & 2 deletions ChocolateOrder/ChocolateOrder.csproj
Expand Up @@ -15,8 +15,10 @@
<PackageReference Include="Microsoft.ServiceFabric.Data" Version="2.8.211" />
<PackageReference Include="Microsoft.ServiceFabric" Version="6.0.211" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NServiceBus" Version="6.4.1" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.6" />
<PackageReference Include="NServiceBus" Version="6.4.3" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.11" />
<PackageReference Include="NServiceBus.Heartbeat" Version="2.0.0" />
<PackageReference Include="NServiceBus.Metrics.ServiceControl" Version="2.0.0" />
<PackageReference Include="NServiceBus.Persistence.ServiceFabric" Version="1.*" />
</ItemGroup>
<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions ChocolateOrder/EndpointCommunicationListener.cs
Expand Up @@ -8,6 +8,7 @@
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using NServiceBus;
using NServiceBus.Persistence.ServiceFabric;
using Shared;

namespace ChocolateOrder
{
Expand Down Expand Up @@ -37,6 +38,19 @@ public async Task<string> OpenAsync(CancellationToken cancellationToken)

endpointConfiguration = new EndpointConfiguration("chocolateorder");

#region Monitoring

endpointConfiguration.SendHeartbeatTo(serviceControlQueue: "Particular.ServiceControl.RabbitMQ", frequency: TimeSpan.FromSeconds(5), timeToLive: TimeSpan.FromSeconds(15));
var hostInfo = endpointConfiguration.UniquelyIdentifyRunningInstance();
var instanceId = partitionInfo.LocalPartitionKey != null ? $"chocolateorder-{partitionInfo.LocalPartitionKey}" : "chocolateorder";
hostInfo.UsingCustomDisplayName(instanceId);
hostInfo.UsingCustomIdentifier(DeterministicIdBuilder.ToGuid(instanceId));

var metrics = endpointConfiguration.EnableMetrics();
metrics.SendMetricDataToServiceControl(serviceControlMetricsAddress: "Particular.Monitoring.RabbitMQ", interval: TimeSpan.FromSeconds(5));

#endregion

var transport = endpointConfiguration.ApplyCommonConfiguration(stateManager, servicePartitionInformation, context);

var routing = transport.Routing();
Expand Down
6 changes: 4 additions & 2 deletions ChocolateShipping/ChocolateShipping.csproj
Expand Up @@ -15,8 +15,10 @@
<PackageReference Include="Microsoft.ServiceFabric.Data" Version="2.8.211" />
<PackageReference Include="Microsoft.ServiceFabric" Version="6.0.211" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NServiceBus" Version="6.4.1" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.6" />
<PackageReference Include="NServiceBus" Version="6.4.3" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.11" />
<PackageReference Include="NServiceBus.Heartbeat" Version="2.0.0" />
<PackageReference Include="NServiceBus.Metrics.ServiceControl" Version="2.0.0" />
<PackageReference Include="NServiceBus.Persistence.ServiceFabric" Version="1.*" />
</ItemGroup>
<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions ChocolateShipping/EndpointCommunicationListener.cs
Expand Up @@ -7,6 +7,7 @@
using Microsoft.ServiceFabric.Data;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using NServiceBus;
using Shared;

namespace ChocolateShipping
{
Expand Down Expand Up @@ -36,6 +37,19 @@ public async Task<string> OpenAsync(CancellationToken cancellationToken)

endpointConfiguration = new EndpointConfiguration("chocolateshipping");

#region Monitoring

endpointConfiguration.SendHeartbeatTo(serviceControlQueue: "Particular.ServiceControl.RabbitMQ", frequency: TimeSpan.FromSeconds(5), timeToLive: TimeSpan.FromSeconds(15));
var hostInfo = endpointConfiguration.UniquelyIdentifyRunningInstance();
var instanceId = partitionInfo.LocalPartitionKey != null ? $"chocolateshipping-{partitionInfo.LocalPartitionKey}" : "chocolateshipping";
hostInfo.UsingCustomDisplayName(instanceId);
hostInfo.UsingCustomIdentifier(DeterministicIdBuilder.ToGuid(instanceId));

var metrics = endpointConfiguration.EnableMetrics();
metrics.SendMetricDataToServiceControl(serviceControlMetricsAddress: "Particular.Monitoring.RabbitMQ", interval: TimeSpan.FromSeconds(5));

#endregion

var transport = endpointConfiguration.ApplyCommonConfiguration(stateManager, servicePartitionInformation, context);

ConfigureLocalPartitionsChocolateShipping(endpointConfiguration, partitionInfo);
Expand Down
2 changes: 1 addition & 1 deletion Contracts/Contracts.csproj
Expand Up @@ -8,6 +8,6 @@
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus" Version="6.4.1" />
<PackageReference Include="NServiceBus" Version="6.4.3" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion OrderChocolate/OrderChocolate.cs
Expand Up @@ -42,7 +42,7 @@ public Task<Result> SayHello()
return Task.FromResult(new Result
{
InstanceId = Context.InstanceId,
Message = $"Hello Warsaw! {this.Context.CodePackageActivationContext.CodePackageVersion}"
Message = $"Hello Lucerne! {this.Context.CodePackageActivationContext.CodePackageVersion}"
});
}
}
Expand Down
18 changes: 18 additions & 0 deletions Shared/DeterministicIdBuilder.cs
@@ -0,0 +1,18 @@
namespace Shared
{
using System;
using System.Text;

public static class DeterministicIdBuilder
{
public static Guid ToGuid(string src)
{
byte[] stringbytes = Encoding.UTF8.GetBytes(src);
byte[] hashedBytes = new System.Security.Cryptography
.SHA1CryptoServiceProvider()
.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
return new Guid(hashedBytes);
}
}
}
4 changes: 4 additions & 0 deletions Shared/EndpointPartitioning/EndpointPartitioningExtensions.cs
Expand Up @@ -23,5 +23,9 @@ public static void RegisterPartitionsForThisEndpoint(this EndpointConfiguration
endpointInstances.AddOrReplaceInstances(endpointName, destinationEndpointInstances);

endpointConfiguration.EnableFeature<HardcodeReplyToAddressToLogicalAddressFeature>();

var augmentedEndpointName = $"{endpointName}-{localPartitionKey}";
endpointConfiguration.Pipeline.Register(new HackEndpointNameBehavior.Registration(augmentedEndpointName));
endpointConfiguration.Pipeline.Register(new HackHostInfoHeadersBehavior.Registration(augmentedEndpointName));
}
}
30 changes: 30 additions & 0 deletions Shared/EndpointPartitioning/HackEndpointNameBehavior.cs
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;

class HackEndpointNameBehavior : IBehavior<IAuditContext, IAuditContext>
{
public HackEndpointNameBehavior(string endpoint)
{
this.endpoint = endpoint;
}

public Task Invoke(IAuditContext context, Func<IAuditContext, Task> next)
{
context.AddAuditData(Headers.ProcessingEndpoint, endpoint);

return next(context);
}

readonly string endpoint;

internal class Registration : RegisterStep
{
public Registration(string endpointName) : base((string)"HackEndpointName", typeof(HackEndpointNameBehavior),
(string)"Hacks the endpoint name", b => new HackEndpointNameBehavior(endpointName))
{
InsertAfterIfExists("AuditHostInformation");
}
}
}
30 changes: 30 additions & 0 deletions Shared/EndpointPartitioning/HackHostInfoHeadersBehavior.cs
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;

class HackHostInfoHeadersBehavior : IBehavior<IOutgoingLogicalMessageContext, IOutgoingLogicalMessageContext>
{
public HackHostInfoHeadersBehavior(string endpoint)
{
this.endpoint = endpoint;
}

public Task Invoke(IOutgoingLogicalMessageContext context, Func<IOutgoingLogicalMessageContext, Task> next)
{
context.Headers[Headers.OriginatingEndpoint] = endpoint;

return next(context);
}

readonly string endpoint;

internal class Registration : RegisterStep
{
public Registration(string endpointName) : base((string)"HackHostInfoHeaders", typeof(HackHostInfoHeadersBehavior),
(string)"Hacks the endpoint name", b => new HackHostInfoHeadersBehavior(endpointName))
{
InsertAfterIfExists("AddHostInfoHeaders");
}
}
}
6 changes: 3 additions & 3 deletions Shared/Shared.csproj
Expand Up @@ -11,9 +11,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.ServiceFabric.Data" Version="2.8.211" />
<PackageReference Include="Microsoft.ServiceFabric" Version="6.0.211" />
<PackageReference Include="Newtonsoft.Json" Version="10.*" />
<PackageReference Include="NServiceBus" Version="6.4.1" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.6" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NServiceBus" Version="6.4.3" />
<PackageReference Include="NServiceBus.Azure.Transports.WindowsAzureServiceBus" Version="7.2.11" />
<PackageReference Include="NServiceBus.Persistence.ServiceFabric" Version="1.*" />
</ItemGroup>
</Project>

0 comments on commit fe89dd5

Please sign in to comment.