-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathAgentComponents.cs
114 lines (89 loc) · 3.9 KB
/
AgentComponents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using Elastic.Apm.Api;
using Elastic.Apm.BackendComm.CentralConfig;
using Elastic.Apm.Config;
using Elastic.Apm.DiagnosticListeners;
using Elastic.Apm.Helpers;
using Elastic.Apm.Logging;
using Elastic.Apm.Metrics;
using Elastic.Apm.Metrics.MetricsProvider;
using Elastic.Apm.Report;
using Elastic.Apm.ServerInfo;
namespace Elastic.Apm
{
public class AgentComponents : IApmAgent, IDisposable
{
public AgentComponents(
IApmLogger logger = null,
IConfigurationReader configurationReader = null,
IPayloadSender payloadSender = null
) : this(logger, configurationReader, payloadSender, null, null, null, null) { }
internal AgentComponents(
IApmLogger logger,
IConfigurationReader configurationReader,
IPayloadSender payloadSender,
IMetricsCollector metricsCollector,
ICurrentExecutionSegmentsContainer currentExecutionSegmentsContainer,
ICentralConfigFetcher centralConfigFetcher,
IApmServerInfo apmServerInfo,
BreakdownMetricsProvider breakdownMetricsProvider = null
)
{
try
{
var tempLogger = logger ?? ConsoleLogger.LoggerOrDefault(configurationReader?.LogLevel);
ConfigurationReader = configurationReader ?? new EnvironmentConfigurationReader(tempLogger);
Logger = logger ?? ConsoleLogger.LoggerOrDefault(ConfigurationReader.LogLevel);
Service = Service.GetDefaultService(ConfigurationReader, Logger);
var systemInfoHelper = new SystemInfoHelper(Logger);
var system = systemInfoHelper.ParseSystemInfo(ConfigurationReader.HostName);
ConfigStore = new ConfigStore(new ConfigSnapshotFromReader(ConfigurationReader, "local"), Logger);
ApmServerInfo = apmServerInfo ?? new ApmServerInfo();
PayloadSender = payloadSender
?? new PayloadSenderV2(Logger, ConfigStore.CurrentSnapshot, Service, system, ApmServerInfo,
isEnabled: ConfigurationReader.Enabled);
HttpTraceConfiguration = new HttpTraceConfiguration();
if (ConfigurationReader.Enabled)
{
breakdownMetricsProvider ??= new BreakdownMetricsProvider(Logger);
CentralConfigFetcher = centralConfigFetcher ?? new CentralConfigFetcher(Logger, ConfigStore, Service);
MetricsCollector = metricsCollector ?? new MetricsCollector(Logger, PayloadSender, ConfigStore, breakdownMetricsProvider);
MetricsCollector.StartCollecting();
}
else
Logger.Info()?.Log("The Elastic APM .NET Agent is disabled - the agent won't capture traces and metrics.");
TracerInternal = new Tracer(Logger, Service, PayloadSender, ConfigStore,
currentExecutionSegmentsContainer ?? new CurrentExecutionSegmentsContainer(), ApmServerInfo, breakdownMetricsProvider);
}
catch (Exception e)
{
Logger.Error()?.LogException(e, "Failed initializing agent.");
}
}
internal ICentralConfigFetcher CentralConfigFetcher { get; }
internal IConfigStore ConfigStore { get; }
public IConfigurationReader ConfigurationReader { get; }
public IApmLogger Logger { get; }
private IMetricsCollector MetricsCollector { get; }
public IPayloadSender PayloadSender { get; }
internal IApmServerInfo ApmServerInfo { get; }
internal HttpTraceConfiguration HttpTraceConfiguration { get; }
/// <summary>
/// Identifies the monitored service. If this remains unset the agent
/// automatically populates it based on the entry assembly.
/// </summary>
/// <value>The service.</value>
public Service Service { get; }
public ITracer Tracer => TracerInternal;
internal Tracer TracerInternal { get; }
public void Dispose()
{
if (MetricsCollector is IDisposable disposableMetricsCollector) disposableMetricsCollector.Dispose();
if (PayloadSender is IDisposable disposablePayloadSender) disposablePayloadSender.Dispose();
CentralConfigFetcher?.Dispose();
}
}
}