示例#1
0
        private void LoadTelemetryClientContext(PSQoSEvent qos, TelemetryContext clientContext)
        {
            if (clientContext != null && qos != null)
            {
                clientContext.Device.Type = "Browser";
                clientContext.User.Id     = qos.Uid;
                clientContext.User.AuthenticatedUserId = qos.AuthenticatedUserId;
                clientContext.User.AccountId           = qos.AccountId;
                // This is not used for some reason, will create issue with MS on Github
                //clientContext.User.UserAgent = qos.UserAgent ?? SSPowerShell.UserAgentValue;
                #region User-Agent work around
                try
                {
                    // Browser is only read from User-Agent header, will create issue with MS on Github. Should also be using Context.User.UserAgent
                    // https://github.com/microsoft/ApplicationInsights-Announcements/issues/3
                    Type typeTransmission = typeof(Microsoft.ApplicationInsights.Channel.Transmission);
                    System.Reflection.FieldInfo fClient = typeTransmission.GetField("client", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
                    System.Net.Http.HttpClient  value   = fClient.GetValue(null) as System.Net.Http.HttpClient;
                    if (!value.DefaultRequestHeaders.UserAgent.Contains(qos.UserAgent))
                    {
                        value.DefaultRequestHeaders.UserAgent.Clear();
                        value.DefaultRequestHeaders.UserAgent.Add(qos.UserAgent);
                    }
                }
                catch { }
                #endregion

                clientContext.Component.Version      = qos.AppVersion;
                clientContext.Session.Id             = qos.SessionId;
                clientContext.Device.OperatingSystem = Environment.OSVersion.ToString();
                clientContext.Cloud.RoleName         = qos.RoleName;
            }
        }
示例#2
0
        private void PopulatePropertiesFromQos(PSQoSEvent qos, IDictionary <string, string> eventProperties)
        {
            if (qos == null)
            {
                return;
            }

            eventProperties.Add("Title", qos.Title);
            eventProperties.Add("Selected", qos.Selected);
            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("CommandParameters", qos.Parameters);
            eventProperties.Add("x-client-request-id", qos.ClientRequestId);
            eventProperties.Add("HashMacAddress", HashMacAddress);
            eventProperties.Add("PowerShellVersion", PSVersion);
            eventProperties.Add("MenuVersion", SSPowerShell.AssemblyVersion);
            eventProperties.Add("CommandParameterSetName", qos.ParameterSetName);
            eventProperties.Add("CommandInvocationName", qos.InvocationName);

            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];
            }
        }
示例#3
0
        public bool LogPerfEvent(PSQoSEvent qos, bool isUsageMetricEnabled, bool isErrorMetricEnabled)
        {
            bool sent = false;

            if (qos == null || !IsMetricTermAccepted())
            {
                return(sent);
            }

            if (isUsageMetricEnabled)
            {
                if (LogPerfEvent(qos))
                {
                    sent = true;
                }
            }

            if (isErrorMetricEnabled && qos.Exception != null)
            {
                if (LogExceptionEvent(qos))
                {
                    sent = true;
                }
            }

            return(sent);
        }
示例#4
0
        private bool LogExceptionEvent(PSQoSEvent qos)
        {
            bool sent = false;

            if (qos == null || qos.Exception == null)
            {
                return(sent);
            }

            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 unless told to.
                if (LogPIIData)
                {
                    eventProperties.Add("Message", qos.Exception.Message);
                }
                else
                {
                    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 going 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);
                sent = true;
            }
            return(sent);
        }
示例#5
0
        private bool LogUsageEvent(PSQoSEvent qos)
        {
            bool sent = false;

            if (qos != null)
            {
                foreach (TelemetryClient client in TelemetryClients)
                {
                    var pageViewTelemetry = new PageViewTelemetry(qos.Name)
                    {
                        Id        = qos.Id,
                        Duration  = qos.Duration,
                        Timestamp = qos.StartTime
                    };
                    LoadTelemetryClientContext(qos, pageViewTelemetry.Context);
                    PopulatePropertiesFromQos(qos, pageViewTelemetry.Properties);

                    client.TrackPageView(pageViewTelemetry);
                    sent = true;
                }
            }
            return(sent);
        }
示例#6
0
        private bool LogPerfEvent(PSQoSEvent qos)
        {
            bool sent = false;

            if (qos != null)
            {
                foreach (TelemetryClient client in TelemetryClients)
                {
                    var perf = new PageViewPerformanceTelemetry(qos.Name)
                    {
                        Id            = qos.Id,
                        DomProcessing = qos.ProcessingTime,
                        PerfTotal     = qos.ProcessingTime,
                        Timestamp     = qos.StartTime
                    };
                    LoadTelemetryClientContext(qos, perf.Context);
                    PopulatePropertiesFromQos(qos, perf.Properties);

                    client.Track(perf);
                    sent = true;
                }
            }
            return(sent);
        }
        protected override void InitializeQosEvent()
        {
            if (MetricData == null)
            {
                MetricData = new MetricData();
            }
            this._metricHelper.LogPIIData = MetricData.LogPIIData;

            var commandAlias = this.GetType().Name;

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

            _qosEvent = new PSQoSEvent()
            {
                CommandName      = commandAlias,
                Id               = Menu?.Name,
                Name             = Menu?.Name,
                Title            = Menu?.DisplayName,
                ModuleName       = MetricData.ModuleName ?? $"{this.MyInvocation.MyCommand.ModuleName}",
                ModuleVersion    = MetricData.ModuleVersion ?? $"{this.MyInvocation.MyCommand.Version}",
                UserAgent        = MetricData.UserAgent,
                AppVersion       = MetricData.AppVersion,
                RoleName         = MetricData.RoleName,
                AccountId        = MetricData.AccountId,
                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)));
            }

            if (!string.IsNullOrWhiteSpace(MetricData.AuthenticatedUserId) &&
                this._metricHelper.LogPIIData)
            {
                _qosEvent.AuthenticatedUserId = MetricData.AuthenticatedUserId;
            }
            if (!string.IsNullOrWhiteSpace(MetricData.UserId) &&
                MetricData.UserId != "defaultid")
            {
                if (this._metricHelper.LogPIIData)
                {
                    _qosEvent.Uid = MetricData.UserId;
                }
                else
                {
                    _qosEvent.Uid = MetricHelper.GenerateSha256HashString(MetricData.UserId);
                }
            }
            else
            {
                _qosEvent.Uid = "defaultid";
            }
        }