opentracing and jaeger on a .netcore 2.1 application? - asp.net-core-2.0

Our application consist of angular 6 for the UI and .netcore 2.0 for the back end, looking to implement tracing to it and so far opentracing seems the most prominent but I can't seem to find any good help documentations for .netcore 2.0 apps.

There is several components which works together and can fully satisfy your requirement.
Common opentracing library, consisted of abstract layer for span, tracer, injectors and extractors, etc.
Official jaeger-client-csharp. Full list of clients can be found here, which implement opentracing abstraction layer mentioned earlier.
The final piece is the OpenTracing API for .NET, which is glue between opentracing library and DiagnosticSource concept in dotnet.
Actually, the final library has sample which uses jaeger csharp implementation of ITracer and configure it as default GlobalTracer.
At the rest in your Startup.cs, you will end up with something like from that sample (services is IServiceCollection):
services.AddSingleton<ITracer>(serviceProvider =>
{
string serviceName = Assembly.GetEntryAssembly().GetName().Name;
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
ISampler sampler = new ConstSampler(sample: true);
ITracer tracer = new Tracer.Builder(serviceName)
.WithLoggerFactory(loggerFactory)
.WithSampler(sampler)
.Build();
GlobalTracer.Register(tracer);
return tracer;
});
// Prevent endless loops when OpenTracing is tracking HTTP requests to Jaeger.
services.Configure<HttpHandlerDiagnosticOptions>(options =>
{
options.IgnorePatterns.Add(request => _jaegerUri.IsBaseOf(request.RequestUri));
});

Related

How best to mock S4 endpoints to do performance tests (load test)?

This is not related to cloud sdk per se, but more regarding mocking the s4 endpoints which we usually use c sdk to query.
We want to do this for our load test, where we would not want the load test to go till s4 endpoint. We are considering using wiremock, to mock the endpoints, but the question is, whether the mocking logic in wiremock itself will contribute not in an insignificant manner to the metrics we are taking. If it would, then the metric becomes somewhwat unreliable since we want the apps performance metric not the mock framework's.
Other approach would be to use a mock server application dedicated for this, so that from app we would not have to do any mocking. Just route the call to the mock server app(using a mock destination perhaps)
My question to you guys is, have you ever encountered this use case? Perhaps yourself, or maybe from a consumer of yours. I would like to know how other teams in SAP solved this problem.
Thanks,
Sachin
In cases like yours, where the entire system (including responses from external services) should be tested, we usually recommend using Wiremock.
This is, because Wiremock is rather easy to setup and works well-enough for regular testing scenarios.
However, as you also pointed out, Wiremock introduces significant runtime overhead for the tested code and, thus, rending performance measurements of any kind more or less useless.
Hence, you could try mocking the HttpClient using Mockito instead:
BasicHttpResponse page = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
page.setEntity(new StringEntity("hello world!));
HttpClient httpClient = mock(HttpClient.class);
doReturn(page).when(httpClient).execute(any(HttpUriRequest.class));
This enables fine-grained control over what your applications retrieves from the mocked endpoint without introducing any kind of actual network actions.
Using the code shown above obviously requires your application under test to actually use the mocked httpClient.
Assuming you are using the SAP Cloud SDK in your application, this can be achieved by overriding the HttpClientCache used in the HttpClientAccessor with a custom implementation that returns your mocked client, like so:
class MockedHttpClientCache implements HttpClientCache
{
#Nonnull
#Override
public Try<HttpClient> tryGetHttpClient(#Nonnull final HttpDestinationProperties destination, #Nonnull final HttpClientFactory httpClientFactory) {
return Try.of(yourMockedClient);
}
#Nonnull
#Override
public Try<HttpClient> tryGetHttpClient(#Nonnull final HttpClientFactory httpClientFactory) {
return Try.of(yourMockedClient);
}
}
// in your test code:
HttpClientAccessor.setHttpClientCache(new MockedHttpClientCache());

Assign Application Insights cloud_RoleName to Windows Service running w/ OWIN

I have an application built from a series of web servers and microservices, perhaps 12 in all. I would like to monitor and, importantly, map this suite of services in Applications Insights. Some of the services are built with Dot Net framework 4.6 and deployed as Windows services using OWIN to receive and respond to requests.
In order to get the instrumentation working with OWIN I'm using the ApplicationInsights.OwinExtensions package. I'm using a single instrumentation key across all my services.
When I look at my Applications Insights Application Map, it appears that all the services that I've instrumented are grouped into a single "application", with a few "links" to outside dependencies. I do not seem to be able to produce the "Composite Application Map" the existence of which is suggested here: https://learn.microsoft.com/en-us/azure/application-insights/app-insights-app-map.
I'm assuming that this is because I have not set a different "RoleName" for each of my services. Unfortunately, I cannot find any documentation that describes how to do so. My map looks as follow, but the big circle in the middle is actually several different microservices:
I do see that the OwinExtensions package offers the ability to customize some aspects of the telemetry reported but, without a deep knowledge of the internal structure of App Insights telemetry, I can't figure out whether it allows the RoleName to be set and, if so, how to accomplish this. Here's what I've tried so far:
appBuilder.UseApplicationInsights(
new RequestTrackingConfiguration
{
GetAdditionalContextProperties =
ctx =>
Task.FromResult(
new [] { new KeyValuePair<string, string>("cloud_RoleName", ServiceConfiguration.SERVICE_NAME) }.AsEnumerable()
)
}
);
Can anyone tell me how, in this context, I can instruct App Insights to collect telemetry which will cause a Composite Application Map to be built?
The following is the overall doc about TelemetryInitializer which is exactly what you want to set additional properties to the collected telemetry - in this case set Cloud Rolename to enable application map.
https://learn.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer
Your telemetry initializer code would be something along the following lines...
public void Initialize(ITelemetry telemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
{
// set role name correctly here.
telemetry.Context.Cloud.RoleName = "RoleName";
}
}
Please try this and see if this helps.

Is there a .NET Standard equivalent for Trace.CorrelationManager?

In my .NET Framework 4.6.1 Web API applications, I'm using the System.Diagnostics.Trace class' CorrelationManager property, along with NLog, to group log messages per request. Unfortunately, it seems like the CorrelationManager property no longer exists on System.Diagnostics.Trace. I have two questions:
Is there a replacement concept somewhere in .NET Standard?
Does NLog natively support that replacement?
It's already supported from version 4.3.1 of NLog.Web. Use variable ${aspnet-TraceIdentifier}.
You can use also custom one with custom logic, for example:
app.Use(next => {
return async context => {
context.TraceIdentifier = Guid.NewGuid().ToString();
await next(context);
};
});
It appears that Microsoft.AspNetCore.Http.HttpContext.TraceIdentifier is what I am looking for. NLog does not currently support this.

Azure Service Bus - topics, messages - using .NET Core

I'm trying to use Azure Service Bus with .NET Core. Obviously at the moment, this kind of sucks. I have tried the following routes:
The official SDK: doesn't work with .NET Core
AMQP.Net Lite: no (decent) documentation, no management APIs around creating/listing topics, etc. Only Service Bus examples cover a small subset of functionality and need you to have a topic already, etc
The community wrapper around AMQP.Net Lite which mirrors the Azure SDK (https://github.com/ppatierno/azuresblite): doesn't work with .NET Core
Then, I moved on to REST.
https://azure.microsoft.com/en-gb/documentation/articles/service-bus-brokered-tutorial-rest/ is a good start (although no RestSharp support for .NET Core either, and for some reason the official SDK doesn't seem to cover a REST client - no Swagger def, no AutoRest client, etc). Although this crappy example concatenates strings into XML without encoding, and covers a small subset of functionality.
So I decided to look for REST documentation. There are two sections, "classic" REST and just REST. Plain-old new REST doesn't support actually sending and receiving messages it seems (...huh?). I'm loathed to use an older technology labelled "classic" unless I can understand what it is - of course, docs are no help here. It also uses XML and ATOM rather than JSON. I have no idea why.
Bonus: the sample linked to in the documentation for the REST API, e.g. from https://msdn.microsoft.com/en-US/library/azure/hh780786.aspx, no longer exists.
Are there any viable approaches anyone has managed to use to read/write messages to topics/from subscriptions with Azure Service Bus and .NET Core?
Still there's not sufficient support for OnMessage implementation which I think the-most important thing in ServiceBus, .Net Core version of ServiceBus was rolled out several days ago.
Receive message example for .Net Core > https://github.com/Azure/azure-service-bus-dotnet/tree/b6f0474429efdff5960cab7cf18031ba2cbbbf52/samples/ReceiveSample
Github project link >
https://github.com/Azure/azure-service-bus-dotnet
And, nuget information > https://www.nuget.org/packages/Microsoft.Azure.Management.ServiceBus/
The support for Azure Service Bus in .Net Core is getting better and better. There is a dedicated nuget package for it: Microsoft.Azure.ServiceBus. As for now (March 2018) it supports most of the scenarios that you might need, altough there are some gaps, like:
receiving messages in batches
checking if topic / queue / subscription exists
creating new topic / queue / subscription from code
As for OnMessage support for receiving messages, there is a new method: RegisterMessageHandler, that does the same thing.
Here is a code sample how it can be used:
public class MessageReceiver
{
private const string ServiceBusConnectionString = "Endpoint=sb://bialecki.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[privateKey]";
public void Receive()
{
var subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, "productRatingUpdates", "sampleSubscription");
try
{
subscriptionClient.RegisterMessageHandler(
async (message, token) =>
{
var messageJson = Encoding.UTF8.GetString(message.Body);
var updateMessage = JsonConvert.DeserializeObject<ProductRatingUpdateMessage>(messageJson);
Console.WriteLine($"Received message with productId: {updateMessage.ProductId}");
await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
},
new MessageHandlerOptions(async args => Console.WriteLine(args.Exception))
{ MaxConcurrentCalls = 1, AutoComplete = false });
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
For full information have a look at my blog posts:
Sending messages in .Net Core: http://www.michalbialecki.com/2017/12/21/sending-a-azure-service-bus-message-in-asp-net-core/
Receiving messages in .Net Core: http://www.michalbialecki.com/2018/02/28/receiving-messages-azure-service-bus-net-core/
Unfortunately, as of time of this writing, your only options for using service bus are either to roll your own if you want to use Azure Storage, or an alternative third party library, such as Hangfire, which has a sort-of-queue in form of Sql Server storage.

How to inject HttpRequestBase and HttpContextBase in Funq (while using ServiceStack)

I have been happily using AutoFaq for a couple of years and take advantage of its ability to easily inject HttpRequestBase and HttpContextBase in the MVC pipeline. This makes mocking and decoupling a lot easier.
I am in the process of changing my data layer to ServiceStack and as part of wiring the default Funq DI mechanism to my different layers I can't figure out how to inject HttpRequestBase and HttpContextBase.
Is there a way to do this? I am looking for the container.Register() analog inside of AppHost.Configure(Func.Container container).
Thanks
ServiceStack doesn't allow registering runtime dependencies with its IOC, although as ServiceStack Services and Request pipeline only binds to the IRequest interface which can just inject a mocked IRequest directly on the service when its required, e.g:
var service = new MyService {
Request = new MockHttpRequest()
};
var response = service.Get(new MyRequest { Id = 1 });
The Testing wiki shows other ways of testing ServiceStack services.
ServiceStack has it's own abstraction of the HttpContext and Request/Response. In v3.x, these are IRequestContext, IHttpRequest, IHttpResponse. This is to be implementation-independent of ASP.NET (console or Mono). It is recommended you use the abstractions instead of trying to use the underlying ASP.NET objects.
In your Service code, you may access them this way:
var httpReq = base.RequestContext.Get<IHttpRequest>();
var httpResp = base.RequestContext.Get<IHttpResponse>();
If you really need the real ASP.NET HttpContext, apparently you should be able to access it at IRequest.OriginalRequest. But if you are trying it the ServiceStack way, "don't do that".
More explanation of the Funq usage in v3 is here:
https://github.com/ServiceStackV3/ServiceStackV3/wiki/The-IoC-container

Resources