Commit af15d97d authored by Liuhaoyang's avatar Liuhaoyang

Init SkyWalking.AspNetCore project

parent 5c794442
......@@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Components", "Components",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.AspNetCore", "src\SkyWalking.AspNetCore\SkyWalking.AspNetCore.csproj", "{D75B68C5-6788-4871-A63F-F6EB48FB0DC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -54,6 +56,10 @@ Global
{50BE8184-EC7A-4257-AF54-764C0E61276F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50BE8184-EC7A-4257-AF54-764C0E61276F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50BE8184-EC7A-4257-AF54-764C0E61276F}.Release|Any CPU.Build.0 = Release|Any CPU
{D75B68C5-6788-4871-A63F-F6EB48FB0DC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D75B68C5-6788-4871-A63F-F6EB48FB0DC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D75B68C5-6788-4871-A63F-F6EB48FB0DC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D75B68C5-6788-4871-A63F-F6EB48FB0DC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -66,6 +72,7 @@ Global
{FD84A10C-C962-4AEA-BA06-86F08EB048E6} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{54B9183A-928B-43E1-9851-10E2F1CCE61C} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{50BE8184-EC7A-4257-AF54-764C0E61276F} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{D75B68C5-6788-4871-A63F-F6EB48FB0DC5} = {79ED86A5-E9B9-49B2-9354-C911C079D03E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
......
......@@ -28,7 +28,7 @@ namespace SkyWalking.Context.Tag
Key = tagKey;
}
protected abstract void Set(ISpan span, T tagValue);
public abstract void Set(ISpan span, T tagValue);
/**
* @return the key of this tag.
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DiagnosticAdapter;
using SkyWalking.Context;
using SkyWalking.Context.Tag;
using SkyWalking.Context.Trace;
namespace SkyWalking.AspNetCore.Diagnostics
{
public class HostingDiagnosticListener : ITracingDiagnosticListener
{
public HostingDiagnosticListener()
{
}
public string ListenerName { get; } = "Microsoft.AspNetCore";
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn")]
public void HttpRequestIn()
{
// do nothing, just enable the diagnotic source
}
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")]
public void HttpRequestInStart(HttpContext httpContext)
{
var carrier = new ContextCarrier();
foreach (var item in carrier.Items)
item.HeadValue = httpContext.Request.Headers[item.HeadKey];
var httpRequestSpan = ContextManager.CreateEntrySpan(httpContext.Request.Path, carrier);
httpRequestSpan.AsHttp();
httpRequestSpan.SetComponent("Asp.Net Core");
Tags.Url.Set(httpRequestSpan, httpContext.Request.Path);
Tags.HTTP.Method.Set(httpRequestSpan, httpContext.Request.Method);
}
[DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop")]
public void HttpRequestInStop(HttpContext httpContext)
{
var httpRequestSpan = ContextManager.ActiveSpan;
var statusCode = httpContext.Response.StatusCode;
if (statusCode >= 400)
{
httpRequestSpan.ErrorOccurred();
}
Tags.StatusCode.Set(httpRequestSpan, statusCode.ToString());
ContextManager.StopSpan(httpRequestSpan);
}
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.HandledException")]
public void DiagnosticHandledException(HttpContext httpContext, Exception exception)
{
ContextManager.ActiveSpan.ErrorOccurred();
}
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")]
public void DiagnosticUnhandledException(HttpContext httpContext, Exception exception)
{
ContextManager.ActiveSpan.ErrorOccurred();
}
[DiagnosticName("Microsoft.AspNetCore.Hosting.UnhandledException")]
public void HostingUnhandledException(HttpContext httpContext, Exception exception)
{
ContextManager.ActiveSpan.ErrorOccurred();
}
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.AspNetCore.Diagnostics
{
public interface ITracingDiagnosticListener
{
string ListenerName { get; }
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SkyWalking.AspNetCore.Diagnostics;
namespace SkyWalking.AspNetCore
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSkyWalking(this IServiceCollection services,
Action<SkyWalkingOptions> options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
return services.AddSkyWalking().Configure(options);
}
public static IServiceCollection AddSkyWalking(this IServiceCollection services,
IConfiguration configuration)
{
return services.AddSkyWalking().Configure<SkyWalkingOptions>(configuration);
}
private static IServiceCollection AddSkyWalking(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddSingleton<IHostedService, SkyWalkingHostedService>();
services.AddSingleton<ITracingDiagnosticListener, HostingDiagnosticListener>();
return services;
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.0-preview2-final" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SkyWalking.Core\SkyWalking.Core.csproj" />
</ItemGroup>
</Project>
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using SkyWalking.AspNetCore.Diagnostics;
using SkyWalking.Boot;
using SkyWalking.Config;
using SkyWalking.Remote;
namespace SkyWalking.AspNetCore
{
public class SkyWalkingHostedService : IHostedService
{
public SkyWalkingHostedService(IOptions<SkyWalkingOptions> options, IHostingEnvironment hostingEnvironment,
IEnumerable<ITracingDiagnosticListener> tracingDiagnosticListeners, DiagnosticListener diagnosticListener)
{
if (string.IsNullOrEmpty(options.Value.DirectServers))
{
throw new ArgumentException("DirectServers cannot be empty or null.");
}
if (string.IsNullOrEmpty(options.Value.ApplicationCode))
{
options.Value.ApplicationCode = hostingEnvironment.ApplicationName;
}
AgentConfig.ApplicationCode = options.Value.ApplicationCode;
CollectorConfig.DirectServers = options.Value.DirectServers;
foreach (var tracingDiagnosticListener in tracingDiagnosticListeners)
diagnosticListener.SubscribeWithAdapter(tracingDiagnosticListener);
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await GrpcChannelManager.Instance.ConnectAsync();
await ServiceManager.Instance.Initialize();
}
public async Task StopAsync(CancellationToken cancellationToken)
{
GrpcChannelManager.Instance.ShutdownAsync();
ServiceManager.Instance.Dispose();
}
}
}
\ No newline at end of file
namespace SkyWalking.AspNetCore
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using Microsoft.Extensions.Options;
namespace SkyWalking.AspNetCore
{
public class SkyWalkingOptions
public class SkyWalkingOptions : IOptions<SkyWalkingOptions>
{
public SkyWalkingOptions Value => this;
/// <summary>
/// Application code is showed in sky-walking-ui
/// </summary>
public string ApplicationCode
{
get;
set;
}
/// <summary>
/// Collector agent_gRPC/grpc service addresses.
/// By using this, no discovery mechanism provided. The agent only uses these addresses to uplink data.
/// Recommend to use this only when collector cluster IPs are unreachable from agent side. Such as:
/// 1. Agent and collector cluster are in different VPC in Cloud.
/// 2. Agent uplinks data to collector cluster through Internet.
/// Single collector:DirectServers="127.0.0.1:11800"
/// Collector cluster:DirectServers="10.2.45.126:11800,10.2.45.127:11800"
/// </summary>
public string DirectServers
{
get;
set;
}
}
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ namespace SkyWalking.Context
public class SW3CarrierItem : CarrierItem
{
public const String HEADER_NAME = "sw3";
private IContextCarrier _carrier;
private readonly IContextCarrier _carrier;
public SW3CarrierItem(IContextCarrier carrier, CarrierItem next)
: base(HEADER_NAME, carrier.Serialize(), next)
......
......@@ -26,7 +26,7 @@ namespace SkyWalking.Context.Tag
{
}
protected override void Set(ISpan span, string tagValue)
public override void Set(ISpan span, string tagValue)
{
span.Tag(Key, tagValue);
}
......
......@@ -24,46 +24,46 @@ namespace SkyWalking.Context.Tag
/// </summary>
public static class Tags
{
public static StringTag URL = new StringTag("url");
public static readonly StringTag Url = new StringTag("url");
/**
* STATUS_CODE records the http status code of the response.
*/
public static StringTag STATUS_CODE = new StringTag("status_code");
public static readonly StringTag StatusCode = new StringTag("status_code");
/**
* DB_TYPE records database type, such as sql, redis, cassandra and so on.
*/
public static StringTag DB_TYPE = new StringTag("db.type");
public static readonly StringTag DbType = new StringTag("db.type");
/**
* DB_INSTANCE records database instance name.
*/
public static StringTag DB_INSTANCE = new StringTag("db.instance");
public static readonly StringTag DbInstance = new StringTag("db.instance");
/**
* DB_STATEMENT records the sql statement of the database access.
*/
public static StringTag DB_STATEMENT = new StringTag("db.statement");
public static readonly StringTag DbStatement = new StringTag("db.statement");
/**
* DB_BIND_VARIABLES records the bind variables of sql statement.
*/
public static StringTag DB_BIND_VARIABLES = new StringTag("db.bind_vars");
public static readonly StringTag DbBindVariables = new StringTag("db.bind_vars");
/**
* MQ_BROKER records the broker address of message-middleware
*/
public static StringTag MQ_BROKER = new StringTag("mq.broker");
public static readonly StringTag MqBorker = new StringTag("mq.broker");
/**
* MQ_TOPIC records the topic name of message-middleware
*/
public static StringTag MQ_TOPIC = new StringTag("mq.topic");
public static readonly StringTag MqTopic = new StringTag("mq.topic");
public static class HTTP
{
public static readonly StringTag METHOD = new StringTag("http.method");
public static readonly StringTag Method = new StringTag("http.method");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment