Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable telemetry metrics when running in serverless environment #4625

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tracer/src/Datadog.Trace/Telemetry/TelemetryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ internal static IConfigurationTelemetry SetConfigForTesting(IConfigurationTeleme
public static TelemetryFactory CreateFactory() => new();

public ITelemetryController CreateTelemetryController(ImmutableTracerSettings tracerSettings, IDiscoveryService discoveryService)
=> CreateTelemetryController(tracerSettings, TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, Config), discoveryService, useCiVisibilityTelemetry: false);
=> CreateTelemetryController(tracerSettings, TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, Config, tracerSettings), discoveryService, useCiVisibilityTelemetry: false);

public ITelemetryController CreateCiVisibilityTelemetryController(ImmutableTracerSettings tracerSettings, IDiscoveryService discoveryService)
=> CreateTelemetryController(tracerSettings, TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, Config), discoveryService, useCiVisibilityTelemetry: true);
=> CreateTelemetryController(tracerSettings, TelemetrySettings.FromSource(GlobalConfigurationSource.Instance, Config, tracerSettings), discoveryService, useCiVisibilityTelemetry: true);

public ITelemetryController CreateTelemetryController(ImmutableTracerSettings tracerSettings, TelemetrySettings settings, IDiscoveryService discoveryService, bool useCiVisibilityTelemetry)
{
Expand Down
18 changes: 14 additions & 4 deletions tracer/src/Datadog.Trace/Telemetry/TelemetrySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public TelemetrySettings(

public bool MetricsEnabled { get; }

public static TelemetrySettings FromSource(IConfigurationSource source, IConfigurationTelemetry telemetry)
=> FromSource(source, telemetry, IsAgentAvailable);
public static TelemetrySettings FromSource(IConfigurationSource source, IConfigurationTelemetry telemetry, ImmutableTracerSettings tracerSettings)
=> FromSource(source, telemetry, IsAgentAvailable, isServerless: tracerSettings.LambdaMetadata.IsRunningInLambda || tracerSettings.IsRunningInAzureFunctionsConsumptionPlan || tracerSettings.IsRunningInGCPFunctions);

public static TelemetrySettings FromSource(IConfigurationSource source, IConfigurationTelemetry telemetry, Func<bool?> isAgentAvailable)
public static TelemetrySettings FromSource(IConfigurationSource source, IConfigurationTelemetry telemetry, Func<bool?> isAgentAvailable, bool isServerless)
{
string? configurationError = null;
var config = new ConfigurationBuilder(source, telemetry);
Expand Down Expand Up @@ -156,7 +156,16 @@ public static TelemetrySettings FromSource(IConfigurationSource source, IConfigu

// Currently disabled, will be flipped to true in later versions as part of the rollout
// Also, will require v2 enabled
var metricsEnabled = config
bool metricsEnabled;
if (isServerless)
{
// disable metrics by default in serverless, because we can't guarantee the correctness
metricsEnabled = false;
telemetry.Record(ConfigurationKeys.Telemetry.MetricsEnabled, false, ConfigurationOrigins.Default);
}
else
{
metricsEnabled = config
.WithKeys(ConfigurationKeys.Telemetry.MetricsEnabled)
.AsBool(
defaultValue: v2Enabled,
Expand All @@ -172,6 +181,7 @@ public static TelemetrySettings FromSource(IConfigurationSource source, IConfigu
: configurationError + ", Cannot enable telemetry metrics unless telemetry V2 is enabled";
return false;
});
}

return new TelemetrySettings(
telemetryEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void WhenValidUrlIsProvided_AndAgentless_AppendsSlashToPath(string url, s
{ ConfigurationKeys.ApiKey, "some_key" },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);
settings.Agentless.Should().NotBeNull();
settings.Agentless.AgentlessUri.Should().Be(expected);
settings.ConfigurationError.Should().BeNullOrEmpty();
Expand All @@ -46,7 +46,7 @@ public void WhenNoUrlOrApiKeyIsProvided_AgentlessIsNotEnabled()
{ ConfigurationKeys.Telemetry.Enabled, "1" }
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);
settings.Agentless.Should().BeNull();
settings.ConfigurationError.Should().BeNullOrEmpty();
}
Expand All @@ -60,7 +60,7 @@ public void WhenOnlyApiKeyIsProvided_UsesIntakeUrl()
{ ConfigurationKeys.ApiKey, "some_key" },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);
settings.Agentless.Should().NotBeNull();
settings.Agentless.AgentlessUri.Should().Be(DefaultIntakeUrl);
settings.ConfigurationError.Should().BeNullOrEmpty();
Expand All @@ -77,7 +77,7 @@ public void WhenApiKeyAndDdSiteIsProvided_UsesDdSiteDomain()
{ ConfigurationKeys.Site, domain },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);

settings.Agentless.Should().NotBeNull();
settings.Agentless.AgentlessUri.Should().Be($"https://instrumentation-telemetry-intake.{domain}/");
Expand All @@ -94,7 +94,7 @@ public void WhenInvalidUrlIsProvided_AndNoApiKey_AgentlessIsNotEnabled()
{ ConfigurationKeys.Telemetry.Uri, url },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);

settings.Agentless.Should().BeNull();
settings.ConfigurationError.Should().BeNullOrEmpty();
Expand All @@ -114,7 +114,7 @@ public void WhenInvalidUrlIsProvided_AndHasApiKey_UsesDefaultIntakeUrl(string ur
{ ConfigurationKeys.ApiKey, "some_key" },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);

settings.Agentless.Should().NotBeNull();
settings.Agentless.AgentlessUri.Should().Be(DefaultIntakeUrl);
Expand All @@ -136,7 +136,7 @@ public void SetsTelemetryEnabledBasedOnApiKeyAndEnabledSettings(string apiKey, s
{ ConfigurationKeys.ApiKey, apiKey },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);
var expectAgentless = enabled && !string.IsNullOrEmpty(apiKey);

if (expectAgentless)
Expand Down Expand Up @@ -167,7 +167,7 @@ public void SetsAgentlessBasedOnApiKey(string apiKey, bool? agentless)
});
var hasApiKey = !string.IsNullOrEmpty(apiKey);

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);
using var s = new AssertionScope();

settings.TelemetryEnabled.Should().Be(true);
Expand Down Expand Up @@ -211,7 +211,7 @@ public void SetsAgentlessBasedOnEnabledAndAgentlessEnabled(bool? enabled, bool?
{ ConfigurationKeys.ApiKey, agentlessEnabled == true ? "SOME_KEY" : null },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);

var expectEnabled = enabled != false;
var expectAgentless = expectEnabled && agentlessEnabled == true;
Expand Down Expand Up @@ -244,7 +244,7 @@ public void SetsAgentProxyEnabledBasedOnConfigAndDelegate(bool? agentProxyEnable
{ ConfigurationKeys.Telemetry.AgentProxyEnabled, agentProxyEnabled?.ToString() }
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => agentAvailable, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => agentAvailable, isServerless: false);

settings.AgentProxyEnabled.Should().Be(expected);
}
Expand All @@ -263,7 +263,7 @@ public void SetsTelemetryEnabledBasedOnAgentlessEnabledAndAgentProxyEnabled(bool
{ ConfigurationKeys.ApiKey, "SOME_KEY" },
});

var settings = TelemetrySettings.FromSource(source, isAgentAvailable: () => true, telemetry: new NullConfigurationTelemetry());
var settings = TelemetrySettings.FromSource(source, telemetry: new NullConfigurationTelemetry(), isAgentAvailable: () => true, isServerless: false);

using var s = new AssertionScope();
settings.TelemetryEnabled.Should().Be(expected);
Expand All @@ -289,7 +289,7 @@ public void SetsTelemetryEnabledBasedOnAgentlessEnabledAndAgentProxyEnabled(bool
public void HeartbeatInterval(string value, double expected)
{
var source = CreateConfigurationSource((ConfigurationKeys.Telemetry.HeartbeatIntervalSeconds, value));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true);
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: false);

settings.HeartbeatInterval.Should().Be(TimeSpan.FromSeconds(expected));
}
Expand All @@ -302,7 +302,7 @@ public void HeartbeatInterval(string value, double expected)
public void V2Enabled_DisabledByDefault(string value, bool expected)
{
var source = CreateConfigurationSource((ConfigurationKeys.Telemetry.V2Enabled, value));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true);
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: false);

settings.V2Enabled.Should().Be(expected);
}
Expand All @@ -312,16 +312,34 @@ public void V2Enabled_DisabledByDefault(string value, bool expected)
[InlineData(null, true)]
[InlineData("", true)]
[InlineData("1", true)]
public void V2Enabled_DisabledByDefaultInAas(string value, bool expected)
public void V2Enabled_EnabledByDefaultInAas(string value, bool expected)
{
var source = CreateConfigurationSource(
(ConfigurationKeys.AzureAppService.AzureAppServicesContextKey, "1"),
(ConfigurationKeys.Telemetry.V2Enabled, value));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true);
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: false);

settings.V2Enabled.Should().Be(expected);
}

[Theory]
[InlineData("0", "1", false)]
[InlineData(null, "1", false)]
[InlineData("", "1", false)]
[InlineData("1", "0", false)]
[InlineData("1", null, false)]
[InlineData("1", "", false)]
[InlineData("1", "1", false)]
public void V2Metrics_DisabledInServerless(string v2Enabled, string metricsEnabled, bool expected)
{
var source = CreateConfigurationSource(
(ConfigurationKeys.Telemetry.V2Enabled, v2Enabled),
(ConfigurationKeys.Telemetry.MetricsEnabled, metricsEnabled));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: true);

settings.MetricsEnabled.Should().Be(expected);
}

[Theory]
[InlineData("0", false, false)]
[InlineData(null, false, false)]
Expand All @@ -335,7 +353,7 @@ public void MetricsEnabled_HasCorrectDefaultValue(string metricSettingValue, boo
var source = CreateConfigurationSource(
(ConfigurationKeys.Telemetry.MetricsEnabled, metricSettingValue),
(ConfigurationKeys.Telemetry.V2Enabled, v2Enabled ? "1" : "0"));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true);
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: false);

settings.MetricsEnabled.Should().Be(expected);
settings.ConfigurationError.Should().BeNullOrEmpty();
Expand All @@ -345,7 +363,7 @@ public void MetricsEnabled_HasCorrectDefaultValue(string metricSettingValue, boo
public void MetricsEnabled_TryingToEnableWhenV2DisabledIsConfigError()
{
var source = CreateConfigurationSource((ConfigurationKeys.Telemetry.MetricsEnabled, "1"), (ConfigurationKeys.Telemetry.V2Enabled, "0"));
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true);
var settings = TelemetrySettings.FromSource(source, NullConfigurationTelemetry.Instance, () => true, isServerless: false);

settings.MetricsEnabled.Should().Be(false);
settings.ConfigurationError.Should().NotBeNullOrEmpty();
Expand Down
Loading