private void SetRequestTracingOptions()
 {
     _requestTracingOptions = new RequestTracingOptions
     {
         HostType                    = TracingUtils.GetHostType(),
         IsDevEnvironment            = TracingUtils.IsDevEnvironment(),
         IsKeyVaultConfigured        = _options.IsKeyVaultConfigured,
         IsKeyVaultRefreshConfigured = _options.IsKeyVaultRefreshConfigured
     };
 }
        public static async Task CallWithRequestTracing(bool tracingEnabled, RequestType requestType, RequestTracingOptions requestTracingOptions, Func <Task> clientCall)
        {
            string correlationContextHeader = "";

            if (tracingEnabled && requestTracingOptions != null)
            {
                correlationContextHeader = CreateCorrelationContextHeader(requestType, requestTracingOptions);
            }

            var activity = new Activity(RequestTracingConstants.DiagnosticHeaderActivityName);

            activity.Start();

            try
            {
                if (!string.IsNullOrWhiteSpace(correlationContextHeader))
                {
                    activity.AddTag(RequestTracingConstants.CorrelationContextHeader, correlationContextHeader);
                }

                await clientCall().ConfigureAwait(false);
            }
            finally
            {
                activity.Stop();
            }
        }
        private static string CreateCorrelationContextHeader(RequestType requestType, RequestTracingOptions requestTracingOptions)
        {
            IList <KeyValuePair <string, string> > correlationContextKeyValues = new List <KeyValuePair <string, string> >();
            IList <string> correlationContextTags = new List <string>();

            correlationContextKeyValues.Add(new KeyValuePair <string, string>(RequestTracingConstants.RequestTypeKey, Enum.GetName(typeof(RequestType), requestType)));

            if (requestTracingOptions.HostType != HostType.Unidentified)
            {
                correlationContextKeyValues.Add(new KeyValuePair <string, string>(RequestTracingConstants.HostTypeKey, Enum.GetName(typeof(HostType), requestTracingOptions.HostType)));
            }

            if (requestTracingOptions.IsDevEnvironment)
            {
                correlationContextKeyValues.Add(new KeyValuePair <string, string>(RequestTracingConstants.EnvironmentKey, RequestTracingConstants.DevEnvironmentValue));
            }

            if (requestTracingOptions.IsKeyVaultConfigured)
            {
                correlationContextTags.Add(RequestTracingConstants.KeyVaultConfiguredTag);
            }

            if (requestTracingOptions.IsKeyVaultRefreshConfigured)
            {
                correlationContextTags.Add(RequestTracingConstants.KeyVaultRefreshConfiguredTag);
            }

            var sb = new StringBuilder();

            foreach (KeyValuePair <string, string> kvp in correlationContextKeyValues)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                }

                sb.Append($"{kvp.Key}={kvp.Value}");
            }

            foreach (string tag in correlationContextTags)
            {
                if (sb.Length > 0)
                {
                    sb.Append(",");
                }

                sb.Append($"{tag}");
            }

            return(sb.ToString());
        }