diff --git a/sample/SkyApm.Sample.GeneralHost/Program.cs b/sample/SkyApm.Sample.GeneralHost/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..51844b9276265ce1d6fdf7c0c7fe88a3ac1775a0 --- /dev/null +++ b/sample/SkyApm.Sample.GeneralHost/Program.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SkyApm.Agent.GeneralHost; + +namespace SkyApm.Sample.GeneralHost +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + new HostBuilder() + .ConfigureServices(services => services.AddHostedService()) + .AddSkyAPM(); + } +} diff --git a/sample/SkyApm.Sample.GeneralHost/SkyApm.Sample.GeneralHost.csproj b/sample/SkyApm.Sample.GeneralHost/SkyApm.Sample.GeneralHost.csproj new file mode 100644 index 0000000000000000000000000000000000000000..fd74aef77f396374f77e426984c8ad575ad32acc --- /dev/null +++ b/sample/SkyApm.Sample.GeneralHost/SkyApm.Sample.GeneralHost.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp2.1 + Exe + + + + + Always + PreserveNewest + + + + + + + diff --git a/sample/SkyApm.Sample.GeneralHost/Worker.cs b/sample/SkyApm.Sample.GeneralHost/Worker.cs new file mode 100644 index 0000000000000000000000000000000000000000..1296afe678aa0da057ea67aadcbdfaebfb0983ac --- /dev/null +++ b/sample/SkyApm.Sample.GeneralHost/Worker.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.Hosting; +using SkyApm.Tracing; +using SkyApm.Tracing.Segments; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace SkyApm.Sample.GeneralHost +{ + public class Worker : BackgroundService + { + private readonly ITracingContext _tracingContext; + + public Worker(ITracingContext tracingContext) + { + _tracingContext = tracingContext; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + var context = _tracingContext.CreateEntrySegmentContext(nameof(ExecuteAsync), new TextCarrierHeaderCollection(new Dictionary())); + + await Task.Delay(1000, stoppingToken); + + context.Span.AddLog(LogEvent.Message($"Worker running at: {DateTime.Now}")); + + _tracingContext.Release(context); + } + } + } +} diff --git a/sample/SkyApm.Sample.GeneralHost/skyapm.json b/sample/SkyApm.Sample.GeneralHost/skyapm.json new file mode 100644 index 0000000000000000000000000000000000000000..29a19b8dafba457d38a581d16bb5a9baad7e94e8 --- /dev/null +++ b/sample/SkyApm.Sample.GeneralHost/skyapm.json @@ -0,0 +1,29 @@ +{ + "SkyWalking": { + "ServiceName": "general-host-sample", + "Namespace": "", + "HeaderVersions": [ + "sw6" + ], + "Sampling": { + "SamplePer3Secs": -1, + "Percentage": -1.0 + }, + "Logging": { + "Level": "Information", + "FilePath": "logs/skyapm-{Date}.log" + }, + "Transport": { + "Interval": 3000, + "ProtocolVersion": "v6", + "QueueSize": 30000, + "BatchSize": 3000, + "gRPC": { + "Servers": "localhost:11800", + "Timeout": 100000, + "ConnectTimeout": 100000, + "ReportTimeout": 600000 + } + } + } +} \ No newline at end of file diff --git a/skyapm-dotnet.sln b/skyapm-dotnet.sln index a0ed237635ee4d4a73fe4d60d5be044a1ec812c0..7d2cb5ab6c0f0fec6fce78405a0bf58831b6c742 100644 --- a/skyapm-dotnet.sln +++ b/skyapm-dotnet.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2024 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28729.10 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{05BF0D4E-C824-4EC8-8330-36C1FC49910E}" EndProject @@ -82,7 +82,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.DotNet.CLI", "src\Sk EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.SmartSql", "src\SkyApm.Diagnostics.SmartSql\SkyApm.Diagnostics.SmartSql.csproj", "{B43160C8-C9E3-43F6-83E9-A9A45B32F022}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Sample.SmartSql", "sample\SkyApm.Sample.SmartSql\SkyApm.Sample.SmartSql.csproj", "{8E2B336B-8138-4179-A754-A1D0C0471654}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Sample.SmartSql", "sample\SkyApm.Sample.SmartSql\SkyApm.Sample.SmartSql.csproj", "{8E2B336B-8138-4179-A754-A1D0C0471654}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Agent.GeneralHost", "src\SkyApm.Agent.GeneralHost\SkyApm.Agent.GeneralHost.csproj", "{4C1CF0E7-4CD3-4731-9A56-E033D5216D58}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Sample.GeneralHost", "sample\SkyApm.Sample.GeneralHost\SkyApm.Sample.GeneralHost.csproj", "{477D705E-576B-46C8-8F1E-9A86EDAE9D86}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -182,6 +186,14 @@ Global {8E2B336B-8138-4179-A754-A1D0C0471654}.Debug|Any CPU.Build.0 = Debug|Any CPU {8E2B336B-8138-4179-A754-A1D0C0471654}.Release|Any CPU.ActiveCfg = Release|Any CPU {8E2B336B-8138-4179-A754-A1D0C0471654}.Release|Any CPU.Build.0 = Release|Any CPU + {4C1CF0E7-4CD3-4731-9A56-E033D5216D58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C1CF0E7-4CD3-4731-9A56-E033D5216D58}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C1CF0E7-4CD3-4731-9A56-E033D5216D58}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C1CF0E7-4CD3-4731-9A56-E033D5216D58}.Release|Any CPU.Build.0 = Release|Any CPU + {477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Debug|Any CPU.Build.0 = Debug|Any CPU + {477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Release|Any CPU.ActiveCfg = Release|Any CPU + {477D705E-576B-46C8-8F1E-9A86EDAE9D86}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -218,6 +230,8 @@ Global {E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B} = {1B0865AF-369E-493C-AA6F-C56D3E520A22} {B43160C8-C9E3-43F6-83E9-A9A45B32F022} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC} {8E2B336B-8138-4179-A754-A1D0C0471654} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB} + {4C1CF0E7-4CD3-4731-9A56-E033D5216D58} = {EF6194B2-9ACB-49B9-8049-DD6AFAEB0399} + {477D705E-576B-46C8-8F1E-9A86EDAE9D86} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583} diff --git a/src/SkyApm.Agent.GeneralHost/Extensions/HostBuilderExtensions.cs b/src/SkyApm.Agent.GeneralHost/Extensions/HostBuilderExtensions.cs new file mode 100644 index 0000000000000000000000000000000000000000..b3ec44de7c721718c6e9956020dcde86d43e147b --- /dev/null +++ b/src/SkyApm.Agent.GeneralHost/Extensions/HostBuilderExtensions.cs @@ -0,0 +1,28 @@ +/* + * Licensed to the SkyAPM under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The SkyAPM 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.Hosting; + +namespace SkyApm.Agent.GeneralHost +{ + public static class HostBuilderExtensions + { + public static IHostBuilder AddSkyAPM(this IHostBuilder builder) => + builder.ConfigureServices((context, services) => services.AddSkyAPMCore()); + } +} \ No newline at end of file diff --git a/src/SkyApm.Agent.GeneralHost/Extensions/ServiceCollectionExtensions.cs b/src/SkyApm.Agent.GeneralHost/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000000000000000000000000000000000000..1f52b4d2ae262df81657d7f3d21fe156801e7e80 --- /dev/null +++ b/src/SkyApm.Agent.GeneralHost/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,112 @@ +/* + * Licensed to the SkyAPM under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The SkyAPM 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.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SkyApm.Config; +using SkyApm.Diagnostics; +using SkyApm.Diagnostics.EntityFrameworkCore; +using SkyApm.Diagnostics.HttpClient; +using SkyApm.Diagnostics.SqlClient; +using SkyApm.Logging; +using SkyApm.Sampling; +using SkyApm.Service; +using SkyApm.Tracing; +using SkyApm.Transport; +using SkyApm.Transport.Grpc; +using SkyApm.Transport.Grpc.V5; +using SkyApm.Transport.Grpc.V6; +using SkyApm.Utilities.Configuration; +using SkyApm.Utilities.DependencyInjection; +using SkyApm.Utilities.Logging; +using System; + +namespace SkyApm.Agent.GeneralHost +{ + internal static class ServiceCollectionExtensions + { + internal static IServiceCollection AddSkyAPMCore(this IServiceCollection services) + { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(RuntimeEnvironment.Instance); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddTracing().AddSampling().AddGrpcTransport().AddLogging(); + services.AddSkyApmExtensions().AddHttpClient().AddSqlClient() + .AddEntityFrameworkCore(c => c.AddPomeloMysql().AddNpgsql().AddSqlite()); + return services; + } + + private static IServiceCollection AddTracing(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + return services; + } + + private static IServiceCollection AddSampling(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(p => p.GetService()); + services.AddSingleton(p => p.GetService()); + services.AddSingleton(); + return services; + } + + private static IServiceCollection AddGrpcTransport(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + return services; + } + + private static IServiceCollection AddLogging(this IServiceCollection services) + { + services.AddSingleton(); + return services; + } + } +} \ No newline at end of file diff --git a/src/SkyApm.Agent.GeneralHost/HostingEnvironmentProvider.cs b/src/SkyApm.Agent.GeneralHost/HostingEnvironmentProvider.cs new file mode 100644 index 0000000000000000000000000000000000000000..379b659e152accc69b65df4a31603d39c809978f --- /dev/null +++ b/src/SkyApm.Agent.GeneralHost/HostingEnvironmentProvider.cs @@ -0,0 +1,32 @@ +/* + * Licensed to the SkyAPM under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The SkyAPM 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.Hosting; + +namespace SkyApm.Agent.GeneralHost +{ + internal class HostingEnvironmentProvider : IEnvironmentProvider + { + public string EnvironmentName { get; } + + public HostingEnvironmentProvider(IHostingEnvironment hostingEnvironment) + { + EnvironmentName = hostingEnvironment.EnvironmentName; + } + } +} \ No newline at end of file diff --git a/src/SkyApm.Agent.GeneralHost/InstrumentationHostedService.cs b/src/SkyApm.Agent.GeneralHost/InstrumentationHostedService.cs new file mode 100644 index 0000000000000000000000000000000000000000..57f31f979282cafdd05b032af23a11a6d8e64f4c --- /dev/null +++ b/src/SkyApm.Agent.GeneralHost/InstrumentationHostedService.cs @@ -0,0 +1,44 @@ +/* + * Licensed to the SkyAPM under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The SkyAPM 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.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; + +namespace SkyApm.Agent.GeneralHost +{ + internal class InstrumentationHostedService : IHostedService + { + private readonly IInstrumentStartup _startup; + + public InstrumentationHostedService(IInstrumentStartup startup) + { + _startup = startup; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + return _startup.StartAsync(cancellationToken); + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return _startup.StopAsync(cancellationToken); + } + } +} \ No newline at end of file diff --git a/src/SkyApm.Agent.GeneralHost/SkyApm.Agent.GeneralHost.csproj b/src/SkyApm.Agent.GeneralHost/SkyApm.Agent.GeneralHost.csproj new file mode 100644 index 0000000000000000000000000000000000000000..935cc209c2cbd85bd9df998469de0a271cbceb75 --- /dev/null +++ b/src/SkyApm.Agent.GeneralHost/SkyApm.Agent.GeneralHost.csproj @@ -0,0 +1,31 @@ + + + + $(Product) ASP.NET Core Agent. + $(PackagePrefix).Agent.GeneralHost + $(PackagePrefix).Agent.GeneralHost + $(PackagePrefix).Agent.GeneralHost + SkyWalking;APM + + + SkyApm.Agent.GeneralHost + netstandard2.0 + + + + + + + + + + + + + + + + + + + \ No newline at end of file