Skip to content

Commit

Permalink
Merge pull request #93 from gregkalapos/SampleAppImprovement
Browse files Browse the repository at this point in the history
Add HttpListener sample
  • Loading branch information
gregkalapos authored Feb 6, 2019
2 parents 01bb24a + 575c9b7 commit fcd627f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ElasticApmAgent.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiSamples", "sample\ApiSam
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Apm.All", "src\Elastic.Apm.All\Elastic.Apm.All.csproj", "{C0F1E546-4388-4EA5-8C7C-ACA8CFA4CE2F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpListenerSample", "sample\HttpListenerSample\HttpListenerSample.csproj", "{1AAB1A30-42C0-4C76-AE2E-9D7293945D80}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -61,6 +63,10 @@ Global
{C0F1E546-4388-4EA5-8C7C-ACA8CFA4CE2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0F1E546-4388-4EA5-8C7C-ACA8CFA4CE2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0F1E546-4388-4EA5-8C7C-ACA8CFA4CE2F}.Release|Any CPU.Build.0 = Release|Any CPU
{1AAB1A30-42C0-4C76-AE2E-9D7293945D80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AAB1A30-42C0-4C76-AE2E-9D7293945D80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AAB1A30-42C0-4C76-AE2E-9D7293945D80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AAB1A30-42C0-4C76-AE2E-9D7293945D80}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0ADF687D-4DB7-417E-80B4-27B3FD86875E} = {3C791D9C-6F19-4F46-B367-2EC0F818762D}
Expand All @@ -71,5 +77,6 @@ Global
{36D72723-2161-4322-A04B-E97D0CB48EF6} = {267A241E-571F-458F-B04C-B6C4DE79E735}
{19C9C1BA-4BCC-4708-92C3-8F5AE18B44F8} = {3C791D9C-6F19-4F46-B367-2EC0F818762D}
{C0F1E546-4388-4EA5-8C7C-ACA8CFA4CE2F} = {3734A52F-2222-454B-BF58-1BA5C1F29D77}
{1AAB1A30-42C0-4C76-AE2E-9D7293945D80} = {3C791D9C-6F19-4F46-B367-2EC0F818762D}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions sample/HttpListenerSample/HttpListenerSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Elastic.Apm\Elastic.Apm.csproj" />
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions sample/HttpListenerSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Net;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using Elastic.Apm;
using Elastic.Apm.Api;

namespace HttpListenerSample
{
class Program
{
static async Task Main(string[] args)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}

// Create a listener.
var listener = new HttpListener();
// Add the prefix
listener.Prefixes.Add("http://localhost:8080/");

listener.Start();
Console.WriteLine("Listening...");

while (true)
{
// Note: The GetContext method blocks while waiting for a request.
var context = listener.GetContext();

// Capture the incoming request as a transaction with the agent
await Agent.Tracer.CaptureTransaction("Request", ApiConstants.TypeRequest, async () =>
{
var request = context.Request;
// Obtain a response object.
var response = context.Response;
// Construct a response.
var responseString = $"<HTML><BODY> Hello world! Random number: {await GenerateRandomNumber()}</BODY></HTML>";
var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
var output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
});
}

listener.Stop();
}

static readonly Random Random = new Random();

private static async Task<int> GenerateRandomNumber()
{
// Get the current transaction and then capture this method as a span on the current transaction
return await Agent.Tracer.CurrentTransaction.CaptureSpan("RandomGenerator", "Random", async () =>
{
await Task.Delay(5); // Simulate some work
return Random.Next();
});
}
}
}

0 comments on commit fcd627f

Please sign in to comment.