Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit a32e580

Browse files
Very first implementation of incoming http tracking (#47)
* very first implementation of http in monitoring without any tests yet * WIP test project. It will break build * http in tests are now running OK. Time to start implementing an actual test * implemented http in test case * sharing files between collectors * update build to use 2.1.4 instead * removed copy/paste error * do not test testapp as a unit test * revert back 2.1 change * 2.1 yet again
1 parent c29d5f4 commit a32e580

30 files changed

+567
-103
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: csharp
22
mono: none
3-
dotnet: 2.0.0
3+
dotnet: 2.1.402
44
script:
55
- dotnet restore
66
- dotnet build

.vsts/ci-build.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# CI build. No publioshing of artifacts
22

3+
variables:
4+
DotNetVersion: "2.1.402"
5+
36
phases:
47

58
- phase: Windows
69
queue: Hosted VS2017
710
steps:
11+
812
- task: DotNetCoreInstaller@0
913
displayName: force use of desired dotnet version
1014
inputs:
11-
version: "2.0.3"
15+
version: $(DotNetVersion)
1216

1317
- task: DotNetCoreCLI@2
1418
displayName: restore packages
@@ -28,7 +32,7 @@ phases:
2832
displayName: test
2933
inputs:
3034
command: "test"
31-
projects: "test/**/*.csproj"
35+
projects: "test/**/*.Tests.csproj"
3236
arguments: "--configuration Release"
3337

3438
- task: PublishTestResults@2
@@ -39,7 +43,7 @@ phases:
3943
- task: DotNetCoreInstaller@0
4044
displayName: force use of desired dotnet version
4145
inputs:
42-
version: "2.0.3"
46+
version: $(DotNetVersion)
4347

4448
- task: DotNetCoreCLI@2
4549
displayName: restore packages
@@ -58,7 +62,7 @@ phases:
5862
displayName: test
5963
inputs:
6064
command: "test"
61-
projects: "test/**/*.csproj"
65+
projects: "test/**/*.Tests.csproj"
6266
arguments: "--configuration Release"
6367

6468
- task: PublishTestResults@2

.vsts/ci-myget-update.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CI build with the upload to MyGet
22

3+
variables:
4+
DotNetVersion: "2.1.402"
5+
36
trigger:
47
branches:
58
include:
@@ -11,7 +14,7 @@ steps:
1114
- task: DotNetCoreInstaller@0
1215
displayName: force use of desired dotnet version
1316
inputs:
14-
version: "2.0.3"
17+
version: $(DotNetVersion)
1518

1619
- task: DotNetCoreCLI@2
1720
displayName: restore packages

OpenCensus.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.Depend
4646
EndProject
4747
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.Dependencies.Tests", "test\OpenCensus.Collector.Dependencies.Tests\OpenCensus.Collector.Dependencies.Tests.csproj", "{56E2647A-B814-4BAC-B854-BEA0051B5F2E}"
4848
EndProject
49+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.AspNetCore", "src\OpenCensus.Collector.AspNetCore\OpenCensus.Collector.AspNetCore.csproj", "{752D2182-A351-41D8-99EE-DD363D7D5B43}"
50+
EndProject
51+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.AspNetCore.Tests", "test\OpenCensus.Collector.AspNetCore.Tests\OpenCensus.Collector.AspNetCore.Tests.csproj", "{2A47F6A8-63E5-4237-8046-94CAF321E797}"
52+
EndProject
53+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testdata", "testdata", "{77C7929A-2EED-4AA6-8705-B5C443C8AA0F}"
54+
EndProject
55+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp.AspNetCore.2.0", "test\TestApp.AspNetCore.2.0\TestApp.AspNetCore.2.0.csproj", "{F2F81E76-6A0E-466B-B673-EBBF1A9ED075}"
56+
EndProject
4957
Global
5058
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5159
Debug|Any CPU = Debug|Any CPU
@@ -88,10 +96,25 @@ Global
8896
{56E2647A-B814-4BAC-B854-BEA0051B5F2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
8997
{56E2647A-B814-4BAC-B854-BEA0051B5F2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
9098
{56E2647A-B814-4BAC-B854-BEA0051B5F2E}.Release|Any CPU.Build.0 = Release|Any CPU
99+
{752D2182-A351-41D8-99EE-DD363D7D5B43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
100+
{752D2182-A351-41D8-99EE-DD363D7D5B43}.Debug|Any CPU.Build.0 = Debug|Any CPU
101+
{752D2182-A351-41D8-99EE-DD363D7D5B43}.Release|Any CPU.ActiveCfg = Release|Any CPU
102+
{752D2182-A351-41D8-99EE-DD363D7D5B43}.Release|Any CPU.Build.0 = Release|Any CPU
103+
{2A47F6A8-63E5-4237-8046-94CAF321E797}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
104+
{2A47F6A8-63E5-4237-8046-94CAF321E797}.Debug|Any CPU.Build.0 = Debug|Any CPU
105+
{2A47F6A8-63E5-4237-8046-94CAF321E797}.Release|Any CPU.ActiveCfg = Release|Any CPU
106+
{2A47F6A8-63E5-4237-8046-94CAF321E797}.Release|Any CPU.Build.0 = Release|Any CPU
107+
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
108+
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075}.Debug|Any CPU.Build.0 = Debug|Any CPU
109+
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075}.Release|Any CPU.ActiveCfg = Release|Any CPU
110+
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075}.Release|Any CPU.Build.0 = Release|Any CPU
91111
EndGlobalSection
92112
GlobalSection(SolutionProperties) = preSolution
93113
HideSolutionNode = FALSE
94114
EndGlobalSection
115+
GlobalSection(NestedProjects) = preSolution
116+
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075} = {77C7929A-2EED-4AA6-8705-B5C443C8AA0F}
117+
EndGlobalSection
95118
GlobalSection(ExtensibilityGlobals) = postSolution
96119
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
97120
EndGlobalSection

build/Common.test.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@
66
<DelaySign>false</DelaySign>
77
<DefineConstants>$(DefineConstants);SIGNED</DefineConstants>
88
</PropertyGroup>
9+
10+
<PropertyGroup>
11+
<CodeAnalysisRuleSet>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenCensus.sln'))\build\OpenCensus.test.ruleset</CodeAnalysisRuleSet>
12+
</PropertyGroup>
13+
14+
15+
<ItemGroup>
16+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)/stylecop.json" />
17+
</ItemGroup>
18+
919
</Project>

src/OpenCensus.Abstractions/Trace/SpanKind.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
namespace OpenCensus.Trace
1818
{
19-
2019
public enum SpanKind {
2120
Server,
2221

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// <copyright file="HttpInListener.cs" company="OpenCensus Authors">
2+
// Copyright 2018, OpenCensus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of theLicense at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenCensus.Collector.AspNetCore.Implementation
18+
{
19+
using System.Diagnostics;
20+
using Microsoft.AspNetCore.Http;
21+
using Microsoft.AspNetCore.Http.Features;
22+
using OpenCensus.Collector.Implementation.Common;
23+
using OpenCensus.Trace;
24+
25+
internal class HttpInListener : ListenerHandler
26+
{
27+
private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext");
28+
private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext");
29+
30+
public HttpInListener(ITracer tracer, ISampler sampler) : base("Microsoft.AspNetCore", tracer, sampler)
31+
{
32+
}
33+
34+
public override void OnStartActivity(Activity activity, object payload)
35+
{
36+
var context = this.startContextFetcher.Fetch(payload) as HttpContext;
37+
38+
if (context == null)
39+
{
40+
// Debug.WriteLine("context is null");
41+
return;
42+
}
43+
44+
var request = context.Request;
45+
46+
this.Tracer.SpanBuilder("HttpIn").SetSampler(this.Sampler).StartScopedSpan();
47+
48+
var span = this.Tracer.CurrentSpan;
49+
50+
if (span != null)
51+
{
52+
span.PutServerSpanKindAttribute();
53+
span.PutHttpMethodAttribute(request.Method);
54+
span.PutHttpHostAttribute(request.Host.Value, request.Host.Port ?? 80);
55+
span.PutHttpPathAttribute(request.Path);
56+
}
57+
}
58+
59+
public override void OnStopActivity(Activity activity, object payload)
60+
{
61+
var context = this.stopContextFetcher.Fetch(payload) as HttpContext;
62+
63+
if (context == null)
64+
{
65+
// Debug.WriteLine("context is null");
66+
return;
67+
}
68+
69+
var span = this.Tracer.CurrentSpan;
70+
71+
if (span == null)
72+
{
73+
// report lost span
74+
}
75+
76+
var response = context.Response;
77+
78+
span.PutHttpStatusCode(response.StatusCode, response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase);
79+
span.End();
80+
}
81+
}
82+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenCensus.sln'))\build\Common.prod.props" />
3+
4+
<PropertyGroup>
5+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
6+
<TargetFrameworks Condition="$(OS) != 'Windows_NT'">netstandard2.0</TargetFrameworks>
7+
<IncludeSymbols>True</IncludeSymbols>
8+
</PropertyGroup>
9+
10+
<PropertyGroup>
11+
<Description>OpenCensus collector for ASP.NET Core requests</Description>
12+
<PackageTags>Tracing;OpenCensus;Management;Monitoring;distributed-tracing;AspNetCore</PackageTags>
13+
<PackageIconUrl>https://opencensus.io/images/opencensus-logo.png</PackageIconUrl>
14+
<PackageProjectUrl>https://opencensus.io</PackageProjectUrl>
15+
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
16+
<Authors>OpenCensus authors</Authors>
17+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<Compile Include="..\OpenCensus.Collector.Implementation.Common\**" LinkBase="Implementation\Common\" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\OpenCensus.Abstractions\OpenCensus.Abstractions.csproj" />
26+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta008">
27+
<PrivateAssets>All</PrivateAssets>
28+
</PackageReference>
29+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.0" />
30+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
31+
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.0.0" />
32+
</ItemGroup>
33+
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// <copyright file="RequestsCollector.cs" company="OpenCensus Authors">
2+
// Copyright 2018, OpenCensus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of theLicense at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenCensus.Collector.AspNetCore
18+
{
19+
using System;
20+
using System.Collections.Generic;
21+
using OpenCensus.Collector.AspNetCore.Implementation;
22+
using OpenCensus.Collector.Implementation.Common;
23+
using OpenCensus.Trace;
24+
25+
/// <summary>
26+
/// Dependencies collector.
27+
/// </summary>
28+
public class RequestsCollector : IDisposable
29+
{
30+
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
31+
32+
/// <summary>
33+
/// Dependencies collector.
34+
/// </summary>
35+
/// <param name="options">Configuration options for dependencies collector.</param>
36+
/// <param name="tracer">Tracer to record traced with.</param>
37+
/// <param name="sampler">Sampler to use to sample dependnecy calls.</param>
38+
public RequestsCollector(RequestsCollectorOptions options, ITracer tracer, ISampler sampler)
39+
{
40+
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
41+
new Dictionary<string, Func<ITracer, ISampler, ListenerHandler>>()
42+
{ {"Microsoft.AspNetCore", (t, s) => new HttpInListener(t, s) } },
43+
tracer,
44+
sampler);
45+
this.diagnosticSourceSubscriber.Subscribe();
46+
}
47+
48+
public void Dispose()
49+
{
50+
this.diagnosticSourceSubscriber.Dispose();
51+
}
52+
}
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <copyright file="RequestsCollectorOptions.cs" company="OpenCensus Authors">
2+
// Copyright 2018, OpenCensus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of theLicense at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenCensus.Collector.AspNetCore
18+
{
19+
/// <summary>
20+
/// Options for dependencies collector.
21+
/// </summary>
22+
public class RequestsCollectorOptions
23+
{
24+
}
25+
}

0 commit comments

Comments
 (0)