Beispiel #1
0
        private void LogExceptionEvent(AzurePSQoSEvent qos)
        {
            if (qos == null || qos.Exception == null)
            {
                return;
            }

            Dictionary <string, double> eventMetrics = new Dictionary <string, double>();

            eventMetrics.Add("Duration", qos.Duration.TotalMilliseconds);

            foreach (TelemetryClient client in TelemetryClients)
            {
                Dictionary <string, string> eventProperties = new Dictionary <string, string>();
                LoadTelemetryClientContext(qos, client.Context);
                PopulatePropertiesFromQos(qos, eventProperties);
                // qos.Exception contains exception message which may contain Users specific data.
                // We should not collect users specific data.
                eventProperties.Add("Message", "Message removed due to PII.");
                eventProperties.Add("StackTrace", qos.Exception.StackTrace);
                eventProperties.Add("ExceptionType", qos.Exception.GetType().ToString());
                Exception innerEx        = qos.Exception.InnerException;
                int       exceptionCount = 0;
                //keep goin till we get to the last inner exception
                while (innerEx != null)
                {
                    //Increment the inner exception count so that we can tell which is the outermost
                    //and which the innermost
                    eventProperties.Add("InnerExceptionType-" + exceptionCount++, innerEx.GetType().ToString());
                    innerEx = innerEx.InnerException;
                }
                client.TrackException(null, eventProperties, eventMetrics);
            }
        }
Beispiel #2
0
 private void LoadTelemetryClientContext(AzurePSQoSEvent qos, TelemetryContext clientContext)
 {
     clientContext.User.Id                = qos.Uid;
     clientContext.User.AccountId         = qos.Uid;
     clientContext.Session.Id             = qos.SessionId;
     clientContext.Device.OperatingSystem = Environment.OSVersion.ToString();
 }
Beispiel #3
0
        /// <summary>
        /// Log a telemtry event
        /// </summary>
        /// <param name="qosEvent">The event to log</param>
        public void LogEvent(AzurePSQoSEvent qosEvent)
        {
            var dataCollection = _dataCollectionProfile.EnableAzureDataCollection;
            var enabled        = dataCollection.HasValue ? dataCollection.Value : true;

            _helper.LogQoSEvent(qosEvent, enabled, enabled);
        }
 private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary <string, string> eventProperties)
 {
     eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
     eventProperties.Add("ModuleName", qos.ModuleName);
     eventProperties.Add("ModuleVersion", qos.ModuleVersion);
     eventProperties.Add("HostVersion", qos.HostVersion);
     eventProperties.Add("OS", Environment.OSVersion.ToString());
     eventProperties.Add("CommandParameters", qos.Parameters);
     eventProperties.Add("UserId", qos.Uid);
     eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
     eventProperties.Add("UserAgent", AzurePowerShell.UserAgentValue.ToString());
     eventProperties.Add("HashMacAddress", HashMacAddress);
     if (qos.InputFromPipeline != null)
     {
         eventProperties.Add("InputFromPipeline", qos.InputFromPipeline.Value.ToString());
     }
     if (qos.OutputToPipeline != null)
     {
         eventProperties.Add("OutputToPipeline", qos.OutputToPipeline.Value.ToString());
     }
     foreach (var key in qos.CustomProperties.Keys)
     {
         eventProperties[key] = qos.CustomProperties[key];
     }
 }
Beispiel #5
0
        protected override void InitializeQosEvent()
        {
            var commandAlias = this.GetType().Name;

            if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                commandAlias = this.MyInvocation.MyCommand.Name;
            }

            _qosEvent = new AzurePSQoSEvent()
            {
                CommandName      = commandAlias,
                ModuleName       = this.GetType().Assembly.GetName().Name,
                ModuleVersion    = this.GetType().Assembly.GetName().Version.ToString(),
                ClientRequestId  = this._clientRequestId,
                SessionId        = _sessionId,
                IsSuccess        = true,
                ParameterSetName = this.ParameterSetName
            };

            if (AzVersion == null)
            {
                AzVersion = this.LoadAzVersion();
                UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString();
                string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
                if (!String.IsNullOrWhiteSpace(hostEnv))
                {
                    UserAgent += string.Format(";{0}", hostEnv.Trim());
                }
            }
            _qosEvent.AzVersion = AzVersion;
            _qosEvent.UserAgent = UserAgent;

            if (this.MyInvocation != null && !string.IsNullOrWhiteSpace(this.MyInvocation.InvocationName))
            {
                _qosEvent.InvocationName = this.MyInvocation.InvocationName;
            }

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null &&
                this.MyInvocation.BoundParameters.Keys != null)
            {
                _qosEvent.Parameters = string.Join(" ",
                                                   this.MyInvocation.BoundParameters.Keys.Select(
                                                       s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            IAzureContext context;

            _qosEvent.Uid = "defaultid";
            if (TryGetDefaultContext(out context))
            {
                _qosEvent.SubscriptionId = context.Subscription?.Id;
                _qosEvent.TenantId       = context.Tenant?.Id;
                if (context.Account != null && !String.IsNullOrWhiteSpace(context.Account.Id))
                {
                    _qosEvent.Uid = MetricHelper.GenerateSha256HashString(context.Account.Id.ToString());
                }
            }
        }
        private static void LogUsageEvent(AzurePSQoSEvent qos)
        {
            var tcEvent = new RequestTelemetry(qos.CmdletType, qos.StartTime, qos.Duration, string.Empty, qos.IsSuccess);
            tcEvent.Context.User.Id = qos.Uid;
            tcEvent.Context.User.UserAgent = AzurePowerShell.UserAgentValue.ToString();
            tcEvent.Context.Device.OperatingSystem = Environment.OSVersion.VersionString;

            TelemetryClient.TrackRequest(tcEvent);
        }
Beispiel #7
0
        protected virtual void InitializeQosEvent()
        {
            _qosEvent = new AzurePSQoSEvent()
            {
                ClientRequestId = this._clientRequestId,
                // Use SessionId from MetricHelper so that generated cmdlet and handcrafted cmdlet could share the same Id
                SessionId        = MetricHelper.SessionId,
                IsSuccess        = true,
                ParameterSetName = this.ParameterSetName,
                PreviousEndTime  = _previousEndTime
            };

            if (AzVersion == null)
            {
                AzVersion = this.LoadAzVersion();
                UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString();
                string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
                if (!String.IsNullOrWhiteSpace(hostEnv))
                {
                    UserAgent += string.Format(" {0}", hostEnv.Trim());
                }
                PowerShellVersion = this.LoadPowerShellVersion();
                PSHostName        = this.Host?.Name;
                PSHostVersion     = this.Host?.Version?.ToString();
            }

            _qosEvent.AzVersion     = AzVersion;
            _qosEvent.UserAgent     = UserAgent;
            _qosEvent.PSVersion     = PowerShellVersion;
            _qosEvent.HostVersion   = PSHostVersion;
            _qosEvent.PSHostName    = PSHostName;
            _qosEvent.ModuleName    = this.ModuleName;
            _qosEvent.ModuleVersion = this.ModuleVersion;

            if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                _qosEvent.CommandName = this.MyInvocation.MyCommand.Name;
            }
            else
            {
                _qosEvent.CommandName = this.GetType().Name;
            }

            if (this.MyInvocation != null && !string.IsNullOrWhiteSpace(this.MyInvocation.InvocationName))
            {
                _qosEvent.InvocationName = this.MyInvocation.InvocationName;
            }

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null &&
                this.MyInvocation.BoundParameters.Keys != null)
            {
                _qosEvent.Parameters = string.Join(" ",
                                                   this.MyInvocation.BoundParameters.Keys.Select(
                                                       s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }
        }
        private static void LogUsageEvent(AzurePSQoSEvent qos)
        {
            var tcEvent = new RequestTelemetry(qos.CmdletType, qos.StartTime, qos.Duration, string.Empty, qos.IsSuccess);

            tcEvent.Context.User.Id                = qos.Uid;
            tcEvent.Context.User.UserAgent         = AzurePowerShell.UserAgentValue.ToString();
            tcEvent.Context.Device.OperatingSystem = Environment.OSVersion.VersionString;

            TelemetryClient.TrackRequest(tcEvent);
        }
Beispiel #9
0
        protected virtual void InitializeQosEvent()
        {
            _qosEvent = new AzurePSQoSEvent()
            {
                ClientRequestId  = this._clientRequestId,
                SessionId        = _sessionId,
                IsSuccess        = true,
                ParameterSetName = this.ParameterSetName,
                PreviousEndTime  = _previousEndTime
            };

            if (AzVersion == null)
            {
                AzVersion = this.LoadAzVersion();
                UserAgent = new ProductInfoHeaderValue("AzurePowershell", string.Format("Az{0}", AzVersion)).ToString();
                string hostEnv = Environment.GetEnvironmentVariable("AZUREPS_HOST_ENVIRONMENT");
                if (!String.IsNullOrWhiteSpace(hostEnv))
                {
                    UserAgent += string.Format(" {0}", hostEnv.Trim());
                }
            }
            _qosEvent.AzVersion = AzVersion;
            _qosEvent.UserAgent = UserAgent;

            if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                _qosEvent.CommandName = this.MyInvocation.MyCommand.Name;
                _qosEvent.ModuleName  = this.MyInvocation.MyCommand.ModuleName;
                if (this.MyInvocation.MyCommand.Version != null)
                {
                    _qosEvent.ModuleVersion = this.MyInvocation.MyCommand.Version.ToString();
                }
            }
            else
            {
                _qosEvent.CommandName   = this.GetType().Name;
                _qosEvent.ModuleName    = this.GetType().Assembly.GetName().Name;
                _qosEvent.ModuleVersion = this.GetType().Assembly.GetName().Version.ToString();
            }

            if (this.MyInvocation != null && !string.IsNullOrWhiteSpace(this.MyInvocation.InvocationName))
            {
                _qosEvent.InvocationName = this.MyInvocation.InvocationName;
            }

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null &&
                this.MyInvocation.BoundParameters.Keys != null)
            {
                _qosEvent.Parameters = string.Join(" ",
                                                   this.MyInvocation.BoundParameters.Keys.Select(
                                                       s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }
        }
Beispiel #10
0
 private void LogUsageEvent(AzurePSQoSEvent qos)
 {
     foreach (TelemetryClient client in TelemetryClients)
     {
         var pageViewTelemetry = new PageViewTelemetry
         {
             Name      = qos.CommandName ?? "empty",
             Duration  = qos.Duration,
             Timestamp = qos.StartTime
         };
         LoadTelemetryClientContext(qos, pageViewTelemetry.Context);
         PopulatePropertiesFromQos(qos, pageViewTelemetry.Properties);
         client.TrackPageView(pageViewTelemetry);
     }
 }
        public static void LogQoSEvent(AzurePSQoSEvent qos, bool isUsageMetricEnabled, bool isErrorMetricEnabled)
        {
            if (!IsMetricTermAccepted())
            {
                return;
            }

            if (isUsageMetricEnabled)
            {
                LogUsageEvent(qos);
            }

            if (isErrorMetricEnabled && qos.Exception != null)
            {
                LogExceptionEvent(qos);
            }
        }
        private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary <string, string> eventProperties)
        {
            if (qos == null)
            {
                return;
            }

            eventProperties.Add("Command", qos.CommandName);
            eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
            eventProperties.Add("ModuleName", qos.ModuleName);
            eventProperties.Add("ModuleVersion", qos.ModuleVersion);
            eventProperties.Add("HostVersion", qos.HostVersion);
            eventProperties.Add("OS", Environment.OSVersion.ToString());
            eventProperties.Add("CommandParameters", qos.Parameters);
            eventProperties.Add("UserId", qos.Uid);
            eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
            eventProperties.Add("UserAgent", qos.UserAgent);
            eventProperties.Add("HashMacAddress", HashMacAddress);
            eventProperties.Add("PowerShellVersion", PSVersion);
            eventProperties.Add("Version", qos.AzVersion);
            eventProperties.Add("CommandParameterSetName", qos.ParameterSetName);
            eventProperties.Add("CommandInvocationName", qos.InvocationName);
            eventProperties.Add("start-time", qos.StartTime.ToUniversalTime().ToString("o"));
            eventProperties.Add("end-time", qos.EndTime.ToUniversalTime().ToString("o"));
            eventProperties.Add("duration", qos.Duration.ToString("c"));
            eventProperties.Add("subscription-id", qos.SubscriptionId);
            eventProperties.Add("tenant-id", qos.TenantId);

            if (qos.Exception != null)
            {
                eventProperties.Add("exception-type", qos.Exception.GetType().ToString());
            }

            if (qos.InputFromPipeline != null)
            {
                eventProperties.Add("InputFromPipeline", qos.InputFromPipeline.Value.ToString());
            }
            if (qos.OutputToPipeline != null)
            {
                eventProperties.Add("OutputToPipeline", qos.OutputToPipeline.Value.ToString());
            }
            foreach (var key in qos.CustomProperties.Keys)
            {
                eventProperties[key] = qos.CustomProperties[key];
            }
        }
Beispiel #13
0
        protected override void InitializeQosEvent()
        {
            var commandAlias = this.GetType().Name;

            if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                commandAlias = this.MyInvocation.MyCommand.Name;
            }

            _qosEvent = new AzurePSQoSEvent()
            {
                CommandName      = commandAlias,
                ModuleName       = this.GetType().Assembly.GetName().Name,
                ModuleVersion    = this.GetType().Assembly.GetName().Version.ToString(),
                ClientRequestId  = this._clientRequestId,
                SessionId        = _sessionId,
                IsSuccess        = true,
                ParameterSetName = this.ParameterSetName
            };

            if (this.MyInvocation != null && !string.IsNullOrWhiteSpace(this.MyInvocation.InvocationName))
            {
                _qosEvent.InvocationName = this.MyInvocation.InvocationName;
            }

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null &&
                this.MyInvocation.BoundParameters.Keys != null)
            {
                _qosEvent.Parameters = string.Join(" ",
                                                   this.MyInvocation.BoundParameters.Keys.Select(
                                                       s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            IAzureContext context;

            if (TryGetDefaultContext(out context) &&
                context.Account != null &&
                !string.IsNullOrWhiteSpace(context.Account.Id))
            {
                _qosEvent.Uid = MetricHelper.GenerateSha256HashString(context.Account.Id.ToString());
            }
            else
            {
                _qosEvent.Uid = "defaultid";
            }
        }
        private static void LogExceptionEvent(AzurePSQoSEvent qos)
        {
            //Log as custome event to exclude actual exception message
            var tcEvent = new EventTelemetry("CmdletError");
            tcEvent.Properties.Add("ExceptionType", qos.Exception.GetType().FullName);
            tcEvent.Properties.Add("StackTrace", qos.Exception.StackTrace);
            if (qos.Exception.InnerException != null)
            {
                tcEvent.Properties.Add("InnerExceptionType", qos.Exception.InnerException.GetType().FullName);
                tcEvent.Properties.Add("InnerStackTrace", qos.Exception.InnerException.StackTrace);
            }

            tcEvent.Context.User.Id = qos.Uid;
            tcEvent.Properties.Add("CmdletType", qos.CmdletType);

            TelemetryClient.TrackEvent(tcEvent);
        }
Beispiel #15
0
        public void LogQoSEvent(AzurePSQoSEvent qos, bool isUsageMetricEnabled, bool isErrorMetricEnabled)
        {
            if (!IsMetricTermAccepted())
            {
                return;
            }

            if (isUsageMetricEnabled)
            {
                LogUsageEvent(qos);
            }

            if (isErrorMetricEnabled && qos.Exception != null)
            {
                LogExceptionEvent(qos);
            }
        }
Beispiel #16
0
        protected void InitializeQosEvent()
        {
            QosEvent = new AzurePSQoSEvent()
            {
                CmdletType = this.GetType().Name,
                IsSuccess  = true,
            };

            if (this.Profile != null && this.Profile.DefaultSubscription != null)
            {
                QosEvent.Uid = MetricHelper.GenerateSha256HashString(
                    this.Profile.DefaultSubscription.Id.ToString());
            }
            else
            {
                QosEvent.Uid = "defaultid";
            }
        }
        private static void LogExceptionEvent(AzurePSQoSEvent qos)
        {
            //Log as custome event to exclude actual exception message
            var tcEvent = new EventTelemetry("CmdletError");

            tcEvent.Properties.Add("ExceptionType", qos.Exception.GetType().FullName);
            tcEvent.Properties.Add("StackTrace", qos.Exception.StackTrace);
            if (qos.Exception.InnerException != null)
            {
                tcEvent.Properties.Add("InnerExceptionType", qos.Exception.InnerException.GetType().FullName);
                tcEvent.Properties.Add("InnerStackTrace", qos.Exception.InnerException.StackTrace);
            }

            tcEvent.Context.User.Id = qos.Uid;
            tcEvent.Properties.Add("CmdletType", qos.CmdletType);

            TelemetryClient.TrackEvent(tcEvent);
        }
Beispiel #18
0
 private void LogUsageEvent(AzurePSQoSEvent qos)
 {
     if (qos != null)
     {
         foreach (TelemetryClient client in TelemetryClients)
         {
             var pageViewTelemetry = new PageViewTelemetry
             {
                 Name      = EventName,
                 Duration  = qos.Duration,
                 Timestamp = qos.StartTime
             };
             LoadTelemetryClientContext(qos, pageViewTelemetry.Context);
             //we only need to populate exception details into pageview
             PopulatePropertiesFromQos(qos, pageViewTelemetry.Properties, true);
             client.TrackPageView(pageViewTelemetry);
         }
     }
 }
Beispiel #19
0
        protected override void InitializeQosEvent()
        {
            var commandAlias = this.GetType().Name;

            if (this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                commandAlias = this.MyInvocation.MyCommand.Name;
            }

            QosEvent = new AzurePSQoSEvent()
            {
                CommandName     = commandAlias,
                ModuleName      = this.GetType().Assembly.GetName().Name,
                ModuleVersion   = this.GetType().Assembly.GetName().Version.ToString(),
                ClientRequestId = this._clientRequestId,
                SessionId       = _sessionId,
                IsSuccess       = true,
            };

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null)
            {
                QosEvent.Parameters = string.Join(" ",
                                                  this.MyInvocation.BoundParameters.Keys.Select(
                                                      s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            if (this.DefaultProfile != null &&
                this.DefaultProfile.Context != null &&
                this.DefaultProfile.Context.Account != null &&
                this.DefaultProfile.Context.Account.Id != null)
            {
                QosEvent.Uid = MetricHelper.GenerateSha256HashString(
                    this.DefaultProfile.Context.Account.Id.ToString());
            }
            else
            {
                QosEvent.Uid = "defaultid";
            }
        }
Beispiel #20
0
        private void LogExceptionEvent(AzurePSQoSEvent qos)
        {
            if (qos == null || qos.Exception == null)
            {
                return;
            }

            Dictionary <string, double> eventMetrics = new Dictionary <string, double>();

            eventMetrics.Add("Duration", qos.Duration.TotalMilliseconds);

            foreach (TelemetryClient client in TelemetryClients)
            {
                Dictionary <string, string> eventProperties = new Dictionary <string, string>();
                LoadTelemetryClientContext(qos, client.Context);
                PopulatePropertiesFromQos(qos, eventProperties);
                // qos.Exception contains exception message which may contain Users specific data.
                // We should not collect users specific data.
                eventProperties.Add("StackTrace", qos.Exception.StackTrace);
                eventProperties.Add("ExceptionType", qos.Exception.GetType().ToString());
                client.TrackException(null, eventProperties, eventMetrics);
            }
        }
Beispiel #21
0
        private void LogExceptionEvent(AzurePSQoSEvent qos)
        {
            if (qos == null || qos.Exception == null)
            {
                return;
            }

            Dictionary<string, double> eventMetrics = new Dictionary<string, double>();
            eventMetrics.Add("Duration", qos.Duration.TotalMilliseconds);

            foreach (TelemetryClient client in TelemetryClients)
            {
                Dictionary<string, string> eventProperties = new Dictionary<string, string>();
                LoadTelemetryClientContext(qos, client.Context);
                PopulatePropertiesFromQos(qos, eventProperties);
                // qos.Exception contains exception message which may contain Users specific data. 
                // We should not collect users specific data. 
                eventProperties.Add("StackTrace", qos.Exception.StackTrace);
                eventProperties.Add("ExceptionType", qos.Exception.GetType().ToString());
                client.TrackException(null, eventProperties, eventMetrics);
            }
        }
        protected override void InitializeQosEvent()
        {
            QosEvent = new AzurePSQoSEvent()
            {
                CmdletType = this.GetType().Name,
                IsSuccess = true,
            };

            if (this.Profile != null && this.Profile.DefaultSubscription != null)
            {
                QosEvent.Uid = MetricHelper.GenerateSha256HashString(
                    this.Profile.DefaultSubscription.Id.ToString());
            }
            else
            {
                QosEvent.Uid = "defaultid";
            }
        }
Beispiel #23
0
 private void LogUsageEvent(AzurePSQoSEvent qos)
 {
     foreach (TelemetryClient client in TelemetryClients)
     {
         var pageViewTelemetry = new PageViewTelemetry
         {
             Name = qos.CommandName ?? "empty",
             Duration = qos.Duration,
             Timestamp = qos.StartTime
         };
         LoadTelemetryClientContext(qos, pageViewTelemetry.Context);
         PopulatePropertiesFromQos(qos, pageViewTelemetry.Properties);
         client.TrackPageView(pageViewTelemetry);
     }
 }
Beispiel #24
0
 private void LoadTelemetryClientContext(AzurePSQoSEvent qos, TelemetryContext clientContext)
 {
     clientContext.User.Id = qos.Uid;
     clientContext.User.AccountId = qos.Uid;
     clientContext.Session.Id = qos.SessionId;
     clientContext.Device.OperatingSystem = Environment.OSVersion.ToString();
 }
Beispiel #25
0
 private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string, string> eventProperties)
 {
     eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
     eventProperties.Add("ModuleName", qos.ModuleName);
     eventProperties.Add("ModuleVersion", qos.ModuleVersion);
     eventProperties.Add("HostVersion", qos.HostVersion);
     eventProperties.Add("OS", Environment.OSVersion.ToString());
     eventProperties.Add("CommandParameters", qos.Parameters);
     eventProperties.Add("UserId", qos.Uid);
     eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
     eventProperties.Add("UserAgent", AzurePowerShell.UserAgentValue.ToString());
     if (qos.InputFromPipeline != null)
     {
         eventProperties.Add("InputFromPipeline", qos.InputFromPipeline.Value.ToString());
     }
     if (qos.OutputToPipeline != null)
     {
         eventProperties.Add("OutputToPipeline", qos.OutputToPipeline.Value.ToString());
     }
     foreach (var key in qos.CustomProperties.Keys)
     {
         eventProperties[key] = qos.CustomProperties[key];
     }
 }
        protected override void InitializeQosEvent()
        {
            var commandAlias = this.GetType().Name; 
            if(this.MyInvocation != null && this.MyInvocation.MyCommand != null)
            {
                commandAlias = this.MyInvocation.MyCommand.Name;
            }

            _qosEvent = new AzurePSQoSEvent()
            {
                CommandName = commandAlias,
                ModuleName = this.GetType().Assembly.GetName().Name,
                ModuleVersion = this.GetType().Assembly.GetName().Version.ToString(),
                ClientRequestId = this._clientRequestId,
                SessionId = _sessionId,
                IsSuccess = true,
            };

            if (this.MyInvocation != null && this.MyInvocation.BoundParameters != null)
            {
                _qosEvent.Parameters = string.Join(" ", 
                    this.MyInvocation.BoundParameters.Keys.Select(
                        s => string.Format(CultureInfo.InvariantCulture, "-{0} ***", s)));
            }

            if (this.DefaultProfile != null && 
                this.DefaultProfile.Context != null &&
                this.DefaultProfile.Context.Account != null &&
                this.DefaultProfile.Context.Account.Id != null)
            {
                _qosEvent.Uid = MetricHelper.GenerateSha256HashString(
                    this.DefaultProfile.Context.Account.Id.ToString());
            }
            else
            {
                _qosEvent.Uid = "defaultid";
            }
        }
Beispiel #27
0
        private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary <string, string> eventProperties, bool populateException = false)
        {
            if (qos == null)
            {
                return;
            }

            eventProperties.Add("telemetry-version", "1");
            eventProperties.Add("Command", qos.CommandName);
            eventProperties.Add("IsSuccess", qos.IsSuccess.ToString());
            eventProperties.Add("ModuleName", qos.ModuleName);
            eventProperties.Add("ModuleVersion", qos.ModuleVersion);
            eventProperties.Add("HostVersion", qos.HostVersion);
            eventProperties.Add("OS", Environment.OSVersion.ToString());
            eventProperties.Add("CommandParameters", qos.Parameters);
            eventProperties.Add("x-ms-client-request-id", qos.ClientRequestId);
            eventProperties.Add("UserAgent", qos.UserAgent);
            eventProperties.Add("HashMacAddress", HashMacAddress);
            eventProperties.Add("PowerShellVersion", PSVersion);
            eventProperties.Add("Version", qos.AzVersion);
            eventProperties.Add("CommandParameterSetName", qos.ParameterSetName);
            eventProperties.Add("CommandInvocationName", qos.InvocationName);
            eventProperties.Add("start-time", qos.StartTime.ToUniversalTime().ToString("o"));
            eventProperties.Add("end-time", qos.EndTime.ToUniversalTime().ToString("o"));
            eventProperties.Add("duration", qos.Duration.ToString("c"));
            if (qos.Uid != null)
            {
                eventProperties.Add("UserId", qos.Uid);
            }
            if (qos.SubscriptionId != null)
            {
                eventProperties.Add("subscription-id", qos.SubscriptionId);
            }
            if (qos.TenantId != null)
            {
                eventProperties.Add("tenant-id", qos.TenantId);
            }

            if (qos.PreviousEndTime != null)
            {
                eventProperties.Add("interval", ((TimeSpan)(qos.StartTime - qos.PreviousEndTime)).ToString("c"));
            }

            if (!qos.IsSuccess && qos.Exception?.Data?.Contains(AzurePSErrorDataKeys.ErrorKindKey) == true)
            {
                eventProperties.Add("pebcak", (qos.Exception.Data[AzurePSErrorDataKeys.ErrorKindKey] == ErrorKind.UserError).ToString());
            }

            if (qos.Exception != null && populateException)
            {
                eventProperties["exception-type"] = qos.Exception.GetType().ToString();
                if (qos.Exception is CloudException cloudException)
                {
                    eventProperties["exception-httpcode"] = cloudException.Response?.StatusCode.ToString();
                }
                Exception        innerException    = qos.Exception.InnerException;
                List <Exception> innerExceptions   = new List <Exception>();
                string           innerExceptionStr = string.Empty;
                while (innerException != null)
                {
                    innerExceptions.Add(innerException);
                    if (innerException is CloudException)
                    {
                        eventProperties["exception-httpcode"] = ((CloudException)qos.Exception).Response?.StatusCode.ToString();
                    }
                    innerException = innerException.InnerException;
                }
                if (innerExceptions.Count > 0)
                {
                    eventProperties["exception-inner"] = string.Join(";", innerExceptions.Select(e => e.GetType().ToString()));
                }
                if (exceptionTrackAcceptModuleList.Contains(qos.ModuleName, StringComparer.InvariantCultureIgnoreCase) ||
                    exceptionTrackAcceptCmdletList.Contains(qos.CommandName, StringComparer.InvariantCultureIgnoreCase))
                {
                    StackTrace trace = new StackTrace(qos.Exception);
                    string     stack = string.Join(";", trace.GetFrames().Take(2).Select(f => ConvertFrameToString(f)));
                    eventProperties["exception-stack"] = stack;
                }

                if (qos.Exception.Data != null)
                {
                    if (qos.Exception.Data.Contains(AzurePSErrorDataKeys.HttpStatusCode))
                    {
                        eventProperties["exception-httpcode"] = qos.Exception.Data[AzurePSErrorDataKeys.HttpStatusCode].ToString();
                    }
                    StringBuilder sb = new StringBuilder();
                    foreach (var key in qos.Exception.Data?.Keys)
                    {
                        if (AzurePSErrorDataKeys.IsKeyPredefined(key.ToString()) &&
                            !AzurePSErrorDataKeys.HttpStatusCode.Equals(key))
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(";");
                            }
                            sb.Append($"{key.ToString().Substring(AzurePSErrorDataKeys.KeyPrefix.Length)}={qos.Exception.Data[key]}");
                        }
                    }
                    if (sb.Length > 0)
                    {
                        eventProperties["exception-data"] = sb.ToString();
                    }
                }
            }

            if (qos.InputFromPipeline != null)
            {
                eventProperties.Add("InputFromPipeline", qos.InputFromPipeline.Value.ToString());
            }
            if (qos.OutputToPipeline != null)
            {
                eventProperties.Add("OutputToPipeline", qos.OutputToPipeline.Value.ToString());
            }
            foreach (var key in qos.CustomProperties.Keys)
            {
                eventProperties[key] = qos.CustomProperties[key];
            }
        }