未验证 提交 d5e6053f 编写于 作者: L Lemon 提交者: GitHub

Add `IAdditionalConfigurationSource` to load the agent-config from the config center. (#151)

上级 6793d430
......@@ -47,7 +47,8 @@ namespace SkyApm.Agent.AspNet.Extensions
services.AddSingleton<IConfigAccessor, ConfigAccessor>();
services.AddSingleton<IEnvironmentProvider, HostingEnvironmentProvider>();
services.AddSingleton<InstrumentRequestCallback>();
services.AddSingleton<IConfigurationFactory, ConfigurationFactory>();
services.AddSingleton<ITracingContext, Tracing.TracingContext>();
services.AddSingleton<ICarrierPropagator, CarrierPropagator>();
services.AddSingleton<ICarrierFormatter, Sw3CarrierFormatter>();
......@@ -61,19 +62,19 @@ namespace SkyApm.Agent.AspNet.Extensions
services.AddSingleton<IUniqueIdParser, UniqueIdParser>();
services.AddSingleton<ISegmentContextMapper, SegmentContextMapper>();
services.AddSingleton<IBase64Formatter, Base64Formatter>();
services.AddSingleton<SimpleCountSamplingInterceptor>();
services.AddSingleton<ISamplingInterceptor>(p => p.GetService<SimpleCountSamplingInterceptor>());
services.AddSingleton<IExecutionService>(p => p.GetService<SimpleCountSamplingInterceptor>());
services.AddSingleton<ISamplingInterceptor, RandomSamplingInterceptor>();
services.AddSingleton<ISkyApmClientV5, SkyApmClientV5>();
services.AddSingleton<ISegmentReporter, SegmentReporter>();
services.AddSingleton<ConnectionManager>();
services.AddSingleton<IPingCaller, PingCaller>();
services.AddSingleton<IServiceRegister, ServiceRegister>();
services.AddSingleton<IExecutionService, ConnectService>();
services.AddSingleton<ILoggerFactory, DefaultLoggerFactory>();
return services;
}
......
......@@ -57,6 +57,7 @@ namespace SkyApm.Agent.AspNetCore
services.AddSingleton<IRuntimeEnvironment>(RuntimeEnvironment.Instance);
services.AddSingleton<TracingDiagnosticProcessorObserver>();
services.AddSingleton<IConfigAccessor, ConfigAccessor>();
services.AddSingleton<IConfigurationFactory, ConfigurationFactory>();
services.AddSingleton<IHostedService, InstrumentationHostedService>();
services.AddSingleton<IEnvironmentProvider, HostingEnvironmentProvider>();
services.AddTracing().AddSampling().AddGrpcTransport().AddLogging();
......
......@@ -22,40 +22,15 @@ using System.Reflection;
using Microsoft.Extensions.Configuration;
using SkyApm.Config;
// ReSharper disable StringLiteralTypo
namespace SkyApm.Utilities.Configuration
{
public class ConfigAccessor : IConfigAccessor
{
private const string CONFIG_FILE_PATH_COMPATIBLE = "SKYWALKING__CONFIG__PATH";
private const string CONFIG_FILE_PATH = "SKYAPM__CONFIG__PATH";
private readonly IConfiguration _configuration;
public ConfigAccessor(IEnvironmentProvider environmentProvider)
public ConfigAccessor(IConfigurationFactory factory)
{
var builder = new ConfigurationBuilder();
builder.AddSkyWalkingDefaultConfig();
builder.AddJsonFile("appsettings.json", true).AddJsonFile($"appsettings.{environmentProvider.EnvironmentName}.json", true);
builder.AddJsonFile("skywalking.json", true).AddJsonFile($"skywalking.{environmentProvider.EnvironmentName}.json", true);
builder.AddJsonFile("skyapm.json", true).AddJsonFile($"skyapm.{environmentProvider.EnvironmentName}.json", true);
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH_COMPATIBLE)))
{
builder.AddJsonFile(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH_COMPATIBLE), false);
}
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH)))
{
builder.AddJsonFile(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH), false);
}
builder.AddEnvironmentVariables();
_configuration = builder.Build();
_configuration = factory.Create();
}
public T Get<T>() where T : class, new()
......@@ -72,7 +47,9 @@ namespace SkyApm.Utilities.Configuration
return _configuration.GetSection(config.GetSections()).GetValue<T>(key);
}
//hign performance
/// <summary>
/// high performance
/// </summary>
private static class New<T> where T : new()
{
public static readonly Func<T> Instance = Expression.Lambda<Func<T>>
......
/*
* 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;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
// ReSharper disable StringLiteralTypo
namespace SkyApm.Utilities.Configuration
{
public class ConfigurationFactory : IConfigurationFactory
{
private const string CONFIG_FILE_PATH_COMPATIBLE = "SKYWALKING__CONFIG__PATH";
private const string CONFIG_FILE_PATH = "SKYAPM__CONFIG__PATH";
private readonly IEnvironmentProvider _environmentProvider;
private readonly IEnumerable<IAdditionalConfigurationSource> _additionalConfigurations;
public ConfigurationFactory(IEnvironmentProvider environmentProvider,
IEnumerable<IAdditionalConfigurationSource> additionalConfigurations)
{
_environmentProvider = environmentProvider;
_additionalConfigurations = additionalConfigurations;
}
public IConfiguration Create()
{
var builder = new ConfigurationBuilder();
builder.AddSkyWalkingDefaultConfig();
builder.AddJsonFile("appsettings.json", true)
.AddJsonFile($"appsettings.{_environmentProvider.EnvironmentName}.json", true);
builder.AddJsonFile("skywalking.json", true)
.AddJsonFile($"skywalking.{_environmentProvider.EnvironmentName}.json", true);
builder.AddJsonFile("skyapm.json", true)
.AddJsonFile($"skyapm.{_environmentProvider.EnvironmentName}.json", true);
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH_COMPATIBLE)))
{
builder.AddJsonFile(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH_COMPATIBLE), false);
}
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH)))
{
builder.AddJsonFile(Environment.GetEnvironmentVariable(CONFIG_FILE_PATH), false);
}
builder.AddEnvironmentVariables();
foreach (var additionalConfiguration in _additionalConfigurations)
{
additionalConfiguration.Load(builder);
}
return builder.Build();
}
}
}
\ No newline at end of file
/*
* 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.Configuration;
namespace SkyApm.Utilities.Configuration
{
public interface IAdditionalConfigurationSource
{
void Load(ConfigurationBuilder builder);
}
}
\ No newline at end of file
/*
* 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.Configuration;
namespace SkyApm.Utilities.Configuration
{
public interface IConfigurationFactory
{
IConfiguration Create();
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册