Skip to content

Commit 149ef76

Browse files
committed
Move telemetry opt-out into OptionsModule
1 parent 583f092 commit 149ef76

File tree

6 files changed

+23
-55
lines changed

6 files changed

+23
-55
lines changed

src/Contrast.K8s.AgentOperator/Core/Telemetry/Cluster/ClusterIdHandler.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@ public class ClusterIdHandler : INotificationHandler<EntityReconciled<V1Secret>>
2121

2222
private readonly IClusterIdWriter _clusterIdWriter;
2323
private readonly IClusterIdState _state;
24-
private readonly ITelemetryOptOut _optOut;
25-
private readonly TelemetryOptions _options;
24+
private readonly TelemetryOptions _telemetryOptions;
2625

27-
public ClusterIdHandler(IClusterIdWriter clusterIdWriter, IClusterIdState state, ITelemetryOptOut optOut, TelemetryOptions options)
26+
public ClusterIdHandler(IClusterIdWriter clusterIdWriter, IClusterIdState state, TelemetryOptions options)
2827
{
2928
_clusterIdWriter = clusterIdWriter;
3029
_state = state;
31-
_optOut = optOut;
32-
_options = options;
30+
_telemetryOptions = options;
3331
}
3432

3533
public Task Handle(EntityReconciled<V1Secret> notification, CancellationToken cancellationToken)
3634
{
37-
if (!_optOut.IsOptOutActive()
38-
&& string.Equals(notification.Entity.Name(), _options.ClusterIdSecretName, StringComparison.OrdinalIgnoreCase)
39-
&& string.Equals(notification.Entity.Namespace(), _options.ClusterIdSecretNamespace, StringComparison.OrdinalIgnoreCase))
35+
if (!_telemetryOptions.OptOut
36+
&& string.Equals(notification.Entity.Name(), _telemetryOptions.ClusterIdSecretName, StringComparison.OrdinalIgnoreCase)
37+
&& string.Equals(notification.Entity.Namespace(), _telemetryOptions.ClusterIdSecretNamespace, StringComparison.OrdinalIgnoreCase))
4038
{
4139
var clusterId = _clusterIdWriter.ParseClusterId(notification.Entity);
4240
if (clusterId != null)
@@ -54,7 +52,7 @@ public Task Handle(EntityReconciled<V1Secret> notification, CancellationToken ca
5452

5553
public async Task Handle(LeaderStateChanged notification, CancellationToken cancellationToken)
5654
{
57-
if (!_optOut.IsOptOutActive()
55+
if (!_telemetryOptions.OptOut
5856
&& notification.IsLeader)
5957
{
6058
var stopwatch = Stopwatch.StartNew();

src/Contrast.K8s.AgentOperator/Core/Telemetry/Services/Exceptions/TelemetryExceptionWorker.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Contrast.K8s.AgentOperator.Core.Telemetry.Cluster;
88
using Contrast.K8s.AgentOperator.Core.Telemetry.Models;
9+
using Contrast.K8s.AgentOperator.Options;
910
using JetBrains.Annotations;
1011
using Microsoft.Extensions.Hosting;
1112
using NLog;
@@ -16,20 +17,20 @@ namespace Contrast.K8s.AgentOperator.Core.Telemetry.Services.Exceptions;
1617
public class TelemetryExceptionWorker : BackgroundService
1718
{
1819
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
19-
private readonly ITelemetryOptOut _optOut;
20+
private readonly TelemetryOptions _options;
2021
private readonly TelemetryService _telemetryService;
2122
private readonly IClusterIdState _clusterIdState;
2223

23-
public TelemetryExceptionWorker(ITelemetryOptOut optOut, TelemetryService telemetryService, IClusterIdState clusterIdState)
24+
public TelemetryExceptionWorker(TelemetryOptions options, TelemetryService telemetryService, IClusterIdState clusterIdState)
2425
{
25-
_optOut = optOut;
26+
_options = options;
2627
_telemetryService = telemetryService;
2728
_clusterIdState = clusterIdState;
2829
}
2930

3031
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3132
{
32-
if (_optOut.IsOptOutActive())
33+
if (_options.OptOut)
3334
{
3435
await Task.Delay(Timeout.Infinite, stoppingToken);
3536
return;

src/Contrast.K8s.AgentOperator/Core/Telemetry/Services/Metrics/TelemetryMetricsWorker.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Contrast.K8s.AgentOperator.Core.Telemetry.Cluster;
88
using Contrast.K8s.AgentOperator.Core.Telemetry.Getters;
99
using Contrast.K8s.AgentOperator.Core.Telemetry.Models;
10+
using Contrast.K8s.AgentOperator.Options;
1011
using JetBrains.Annotations;
1112
using Microsoft.Extensions.Hosting;
1213
using NLog;
@@ -17,19 +18,19 @@ namespace Contrast.K8s.AgentOperator.Core.Telemetry.Services.Metrics;
1718
public class TelemetryMetricsWorker : BackgroundService
1819
{
1920
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
20-
private readonly ITelemetryOptOut _optOut;
21+
private readonly TelemetryOptions _options;
2122
private readonly IClusterIdState _clusterIdState;
2223
private readonly TelemetryService _telemetryService;
2324
private readonly StatusReportGenerator _statusReportGenerator;
2425
private readonly IsPublicTelemetryBuildGetter _isPublicTelemetryBuildGetter;
2526

26-
public TelemetryMetricsWorker(ITelemetryOptOut optOut,
27+
public TelemetryMetricsWorker(TelemetryOptions options,
2728
IClusterIdState clusterIdState,
2829
TelemetryService telemetryService,
2930
StatusReportGenerator statusReportGenerator,
3031
IsPublicTelemetryBuildGetter isPublicTelemetryBuildGetter)
3132
{
32-
_optOut = optOut;
33+
_options = options;
3334
_clusterIdState = clusterIdState;
3435
_telemetryService = telemetryService;
3536
_statusReportGenerator = statusReportGenerator;
@@ -43,7 +44,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
4344
Logger.Warn("This instance is not running a public build.");
4445
}
4546

46-
if (_optOut.IsOptOutActive())
47+
if (_options.OptOut)
4748
{
4849
await Task.Delay(Timeout.Infinite, stoppingToken);
4950
return;

src/Contrast.K8s.AgentOperator/Core/Telemetry/TelemetryOptOut.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Contrast.K8s.AgentOperator/Modules/OptionsModule.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ protected override void Load(ContainerBuilder builder)
235235
logger.LogOptionValue("install-source", installSource, installSourceStr);
236236
installSource = installSourceStr;
237237
}
238+
var optOutOld = GetEnvironmentOptionFlag(logger, "CONTRAST_DOTNET_TELEMETRY_OPTOUT", "telemetry-opt-out", false);
239+
var optOutNew = GetEnvironmentOptionFlag(logger, "CONTRAST_AGENT_TELEMETRY_OPTOUT", "telemetry-opt-out", false);
240+
var telemetryOptOut = optOutOld || optOutNew;
238241

239-
return new TelemetryOptions("contrast-cluster-id", @namespace, installSource);
242+
return new TelemetryOptions(telemetryOptOut, "contrast-cluster-id", @namespace, installSource);
240243
}).SingleInstance();
241244

242245
builder.Register(context =>

src/Contrast.K8s.AgentOperator/Options/TelemetryOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Contrast.K8s.AgentOperator.Options;
55

6-
public record TelemetryOptions(string ClusterIdSecretName,
6+
public record TelemetryOptions(bool OptOut,
7+
string ClusterIdSecretName,
78
string ClusterIdSecretNamespace,
89
string InstallSource);

0 commit comments

Comments
 (0)