|
| 1 | +// <copyright file="ManualInstrumentationConfigurationSource.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +#nullable enable |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Globalization; |
| 11 | +using System.Linq; |
| 12 | +using Datadog.Trace.ClrProfiler.AutoInstrumentation.ManualInstrumentation; |
| 13 | +using Datadog.Trace.Configuration.Telemetry; |
| 14 | +using Datadog.Trace.Telemetry; |
| 15 | +using Datadog.Trace.Telemetry.Metrics; |
| 16 | + |
| 17 | +namespace Datadog.Trace.Configuration.ConfigurationSources; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Wraps the settings passed in from the manual instrumentation API in a configuration source, to make it easier to integrate |
| 21 | +/// </summary> |
| 22 | +internal class ManualInstrumentationConfigurationSource : DictionaryObjectConfigurationSource |
| 23 | +{ |
| 24 | + public ManualInstrumentationConfigurationSource(IReadOnlyDictionary<string, object?> dictionary) |
| 25 | + : base(dictionary, ConfigurationOrigins.Code) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + protected override bool TryGetValue(string key, out object? value) |
| 30 | + { |
| 31 | + // Get the value for the given key, but also record telemetry |
| 32 | + // This is also where any "remapping" should be done, in cases |
| 33 | + // where either the manual-instrumentation key differs or the |
| 34 | + // type stored in the dictionary differs |
| 35 | + |
| 36 | + var result = base.TryGetValue(key, out value); |
| 37 | + if (result) |
| 38 | + { |
| 39 | + if (GetTelemetryKey(key) is { } telemetryKey) |
| 40 | + { |
| 41 | + TelemetryFactory.Metrics.Record(telemetryKey); |
| 42 | + } |
| 43 | + |
| 44 | + if (value is not null) |
| 45 | + { |
| 46 | + value = RemapResult(key, value); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + internal static PublicApiUsage? GetTelemetryKey(string key) => key switch |
| 54 | + { |
| 55 | + TracerSettingKeyConstants.AgentUriKey => PublicApiUsage.ExporterSettings_AgentUri_Set, |
| 56 | + TracerSettingKeyConstants.AnalyticsEnabledKey => PublicApiUsage.TracerSettings_AnalyticsEnabled_Set, |
| 57 | + TracerSettingKeyConstants.CustomSamplingRules => PublicApiUsage.TracerSettings_CustomSamplingRules_Set, |
| 58 | + TracerSettingKeyConstants.DiagnosticSourceEnabledKey => PublicApiUsage.TracerSettings_DiagnosticSourceEnabled_Set, |
| 59 | + TracerSettingKeyConstants.DisabledIntegrationNamesKey => PublicApiUsage.TracerSettings_DisabledIntegrationNames_Set, |
| 60 | + TracerSettingKeyConstants.EnvironmentKey => PublicApiUsage.TracerSettings_Environment_Set, |
| 61 | + TracerSettingKeyConstants.GlobalSamplingRateKey => PublicApiUsage.TracerSettings_GlobalSamplingRate_Set, |
| 62 | + TracerSettingKeyConstants.GrpcTags => PublicApiUsage.TracerSettings_GrpcTags_Set, |
| 63 | + TracerSettingKeyConstants.HeaderTags => PublicApiUsage.TracerSettings_HeaderTags_Set, |
| 64 | + TracerSettingKeyConstants.GlobalTagsKey => PublicApiUsage.TracerSettings_GlobalTags_Set, |
| 65 | + TracerSettingKeyConstants.HttpClientErrorCodesKey => PublicApiUsage.TracerSettings_SetHttpClientErrorStatusCodes, |
| 66 | + TracerSettingKeyConstants.HttpServerErrorCodesKey => PublicApiUsage.TracerSettings_SetHttpServerErrorStatusCodes, |
| 67 | + TracerSettingKeyConstants.KafkaCreateConsumerScopeEnabledKey => PublicApiUsage.TracerSettings_KafkaCreateConsumerScopeEnabled_Set, |
| 68 | + TracerSettingKeyConstants.LogsInjectionEnabledKey => PublicApiUsage.TracerSettings_LogsInjectionEnabled_Set, |
| 69 | + TracerSettingKeyConstants.ServiceNameKey => PublicApiUsage.TracerSettings_ServiceName_Set, |
| 70 | + TracerSettingKeyConstants.ServiceNameMappingsKey => PublicApiUsage.TracerSettings_SetServiceNameMappings, |
| 71 | + TracerSettingKeyConstants.MaxTracesSubmittedPerSecondKey => PublicApiUsage.TracerSettings_MaxTracesSubmittedPerSecond_Set, |
| 72 | + TracerSettingKeyConstants.ServiceVersionKey => PublicApiUsage.TracerSettings_ServiceVersion_Set, |
| 73 | + TracerSettingKeyConstants.StartupDiagnosticLogEnabledKey => PublicApiUsage.TracerSettings_StartupDiagnosticLogEnabled_Set, |
| 74 | + TracerSettingKeyConstants.StatsComputationEnabledKey => PublicApiUsage.TracerSettings_StatsComputationEnabled_Set, |
| 75 | + TracerSettingKeyConstants.TraceEnabledKey => PublicApiUsage.TracerSettings_TraceEnabled_Set, |
| 76 | + TracerSettingKeyConstants.TracerMetricsEnabledKey => PublicApiUsage.TracerSettings_TracerMetricsEnabled_Set, |
| 77 | + TracerSettingKeyConstants.IntegrationSettingsKey => PublicApiUsage.TracerSettings_TracerMetricsEnabled_Set, |
| 78 | + // These are pretty hacky but about the best we can do |
| 79 | + _ when key.EndsWith("_ANALYTICS_ENABLED", StringComparison.Ordinal) => PublicApiUsage.IntegrationSettings_AnalyticsEnabled_Set, |
| 80 | + _ when key.EndsWith("_ANALYTICS_SAMPLE_RATE", StringComparison.Ordinal) => PublicApiUsage.IntegrationSettings_AnalyticsSampleRate_Set, |
| 81 | + _ when key.StartsWith("DD_TRACE_", StringComparison.Ordinal) && key.EndsWith("_ANALYTICS_SAMPLE_RATE", StringComparison.Ordinal) |
| 82 | + => PublicApiUsage.IntegrationSettings_Enabled_Set, // this could definitely be too broad, but about the best we can reasonably do |
| 83 | + _ => null |
| 84 | + }; |
| 85 | + |
| 86 | + private static object RemapResult(string key, object value) => key switch |
| 87 | + { |
| 88 | + TracerSettingKeyConstants.AgentUriKey => value is Uri uri ? uri.ToString() : value, |
| 89 | + TracerSettingKeyConstants.HttpServerErrorCodesKey => value is List<int> list |
| 90 | + ? string.Join(",", list.Select(i => i.ToString(CultureInfo.InvariantCulture))) |
| 91 | + : value, |
| 92 | + TracerSettingKeyConstants.HttpClientErrorCodesKey => value is List<int> list |
| 93 | + ? string.Join(",", list.Select(i => i.ToString(CultureInfo.InvariantCulture))) |
| 94 | + : value, |
| 95 | + _ => value, |
| 96 | + }; |
| 97 | +} |
0 commit comments