英文:
How to read Elastic APM configuration from Environment Variables in dotnet 6+
问题
我有一个使用DotNet 6和最小API语法的Docker化WebAPI。我有以下的appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Elastic.Apm": "Trace",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ElasticApm": {
"ServerUrls": "http://host.docker.internal:8200",
"CaptureBody": "all",
"CaptureBodyContentTypes": "application/x-www-form-urlencoded*, text/*, application/json*, application/xml*",
"CloudProvider": "none",
"ServiceName": "MyApp"
}
}
我在Program.cs
中像这样使用它:
...
app.UseOpenApi();
app.UseSwaggerUi3();
app.UseReDoc();
app.UseAllElasticApm(app.Configuration);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
然而,我想将Elastic APM的配置传递为环境变量。我期望有一种方式来传递配置,而不仅仅是使用appsettings.json
。
英文:
Currently I have a dockerized WebAPI using DotNet 6 with the minimal API syntax. I have the following appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Elastic.Apm": "Trace",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ElasticApm": {
"ServerUrls": "http://host.docker.internal:8200",
"CaptureBody": "all",
"CaptureBodyContentTypes": "application/x-www-form-urlencoded*, text/*, application/json*, application/xml*",
"CloudProvider": "none",
"ServiceName": "MyApp"
}
}
and use it like this in my Program.cs
:
...
app.UseOpenApi();
app.UseSwaggerUi3();
app.UseReDoc();
app.UseAllElasticApm(app.Configuration);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
However, I want to pass the configuration for Elastic APM as Environment Variables.
I expect there to be some sort of way to pass configuration in any other way than appsettings.json
答案1
得分: 0
我找到一种实现这一目标的方法是创建一个新的 ConfigurationBuilder,就像这样:
...
// 为了提高可读性,在这里设置环境变量
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URLS", "http://host.docker.internal:8200");
Environment.SetEnvironmentVariable("ELASTIC_APM_ENVIRONMENT", "dev");
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", "MyApp");
var config = new ConfigurationBuilder()
.AddEnvironmentVariables() // 将环境变量添加到配置中
.Build();
app.UseAllElasticApm(config);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
所以我的收获是我没有完全理解最小 API 语法是如何工作的。这种方式还可以使用其他方式,比如从一个 .env
文件中读取配置。这些都是 APM 的环境变量:
https://www.elastic.co/guide/en/apm/agent/dotnet/current/config-all-options-summary.html
总之,所有的内容都在文档中,如果你对 dotnet 很熟悉,可能很明显,但对我来说不是...
英文:
I found one way to achieve this is to create a new ConfigurationBuilder like this:
...
// Setting environment variables here for readability
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URLS", "http://host.docker.internal:8200");
Environment.SetEnvironmentVariable("ELASTIC_APM_ENVIRONMENT", "dev");
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", "MyApp");
var config = new ConfigurationBuilder()
.AddEnvironmentVariables() // Add Environment Variables to configuration
.Build();
app.UseAllElasticApm(config);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
So my takeaway is that I did not understand well enough how exactly the minimal API syntax works.
This way you can also use other ways such as an .env
file to read the config from.
These are all the Environment variables for APM:
https://www.elastic.co/guide/en/apm/agent/dotnet/current/config-all-options-summary.html
In summary, everything was in the docs and if you are well versed in dotnet it may be obvious, but to me it wasn't...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论