Exemplo n.º 1
0
        private static void EmitTelemetry()
        {
            var now = DateTime.UtcNow;

            if (_nextTelemetryDateTime < now)
            {
                var interval = ScmHostingConfigurations.TelemetryIntervalMinutes;
                if (interval <= 0)
                {
                    return;
                }

                _nextTelemetryDateTime = now.AddMinutes(interval);

                var telemetry = new Dictionary <string, object>();
                AddProcessInfo(telemetry);
                AddSiteInfo(telemetry);
                AddDiskSpaceInfo(telemetry);

                KuduEventSource.Log.GenericEvent(
                    ServerConfiguration.GetRuntimeSiteName(),
                    $"Telemetry: {JsonConvert.SerializeObject(telemetry)}",
                    string.Empty,
                    Environment.GetEnvironmentVariable(SettingsKeys.ScmType),
                    Environment.GetEnvironmentVariable(SettingsKeys.WebSiteSku),
                    KuduVersion.Value);
            }
        }
Exemplo n.º 2
0
 public void JobEvent(string jobName, string message, string jobType, string error)
 {
     KuduEventSource.Log.WebJobEvent(
         ServerConfiguration.GetRuntimeSiteName(),
         NullToEmptyString(jobName),
         NullToEmptyString(message),
         NullToEmptyString(jobType),
         NullToEmptyString(error));
 }
Exemplo n.º 3
0
 public void SiteExtensionEvent(string method, string path, string result, string deploymentDurationInMilliseconds, string message)
 {
     KuduEventSource.Log.KuduSiteExtensionEvent(
         ServerConfiguration.GetRuntimeSiteName(),
         NullToEmptyString(method),
         NullToEmptyString(path),
         NullToEmptyString(result),
         NullToEmptyString(deploymentDurationInMilliseconds),
         NullToEmptyString(message));
 }
Exemplo n.º 4
0
 public void JobStarted(string jobName, string scriptExtension, string jobType, string siteMode, string error, string trigger)
 {
     KuduEventSource.Log.WebJobStarted(
         ServerConfiguration.GetRuntimeSiteName(),
         NullToEmptyString(jobName),
         NullToEmptyString(scriptExtension),
         NullToEmptyString(jobType),
         NullToEmptyString(siteMode),
         NullToEmptyString(error),
         NullToEmptyString(trigger));
 }
Exemplo n.º 5
0
 public void ProjectDeployed(string projectType, string result, string error, long deploymentDurationInMilliseconds, string siteMode, string vsProjectId = "")
 {
     KuduEventSource.Log.ProjectDeployed(
         ServerConfiguration.GetRuntimeSiteName(),
         NullToEmptyString(projectType),
         NullToEmptyString(result),
         NullToEmptyString(error),
         deploymentDurationInMilliseconds,
         NullToEmptyString(siteMode),
         NullToEmptyString(_settings.GetValue(SettingsKeys.ScmType)),
         NullToEmptyString(vsProjectId));
 }
Exemplo n.º 6
0
        public void Trace(string message, IDictionary <string, string> attributes)
        {
            // these are traced by TraceModule
            if (message == XmlTracer.IncomingRequestTrace ||
                message == XmlTracer.OutgoingResponseTrace)
            {
                return;
            }

            if (_requestMethod == HttpMethod.Get.Method)
            {
                // ignore normal GET request body
                string type = null;
                if (!attributes.TryGetValue("type", out type))
                {
                    return;
                }

                if (type != "error" && type != "warning")
                {
                    return;
                }
            }


            var strb = new StringBuilder();

            strb.AppendFormat("{0} ", message);

            // took from XMLtracer
            foreach (var attrib in attributes)
            {
                if (TraceExtensions.IsNonDisplayableAttribute(attrib.Key))
                {
                    continue;
                }

                strb.AppendFormat("{0}=\"{1}\" ", attrib.Key, attrib.Value);
            }

            KuduEventSource.Log.GenericEvent(ServerConfiguration.GetRuntimeSiteName(),
                                             strb.ToString(),
                                             _requestId,
                                             string.Empty,
                                             string.Empty,
                                             string.Empty);
        }
Exemplo n.º 7
0
        private static void AddSiteInfo(Dictionary <string, object> telemetry)
        {
            var ownerName = Environment.GetEnvironmentVariable(SettingsKeys.WebSiteOwnerName);

            if (!string.IsNullOrEmpty(ownerName))
            {
                var parts = ownerName.Split('+');
                telemetry["subscriptionId"] = parts.FirstOrDefault();
                telemetry["webspaceName"]   = parts.Length > 1 ? parts[1] : null;
            }

            telemetry["runtimeSiteName"]          = ServerConfiguration.GetRuntimeSiteName();
            telemetry["defaultHostName"]          = Environment.GetEnvironmentVariable(SettingsKeys.WebSiteHostName);
            telemetry["scmType"]                  = Environment.GetEnvironmentVariable(SettingsKeys.ScmType);
            telemetry["sku"]                      = Environment.GetEnvironmentVariable(SettingsKeys.WebSiteSku);
            telemetry["scmHostingConfigurations"] = FileSystemHelpers.FileExists(ScmHostingConfigurations.ConfigsFile) ? FileSystemHelpers.ReadAllText(ScmHostingConfigurations.ConfigsFile) : null;
        }
Exemplo n.º 8
0
        public static string GetValue(string key, string defaultValue = null)
        {
            var env = System.Environment.GetEnvironmentVariable($"SCM_{key}");

            if (!string.IsNullOrEmpty(env))
            {
                return(env);
            }

            var configs = _configs;

            if (configs == null || DateTime.UtcNow > _configsTTL)
            {
                _configsTTL = DateTime.UtcNow.AddMinutes(10);

                try
                {
                    var settings = FileSystemHelpers.FileExists(ConfigsFile)
                        ? FileSystemHelpers.ReadAllText(ConfigsFile)
                        : null;

                    KuduEventSource.Log.GenericEvent(
                        ServerConfiguration.GetRuntimeSiteName(),
                        $"ScmHostingConfigurations: Update value '{settings}'",
                        string.Empty,
                        string.Empty,
                        string.Empty,
                        string.Empty);

                    configs  = Parse(settings);
                    _configs = configs;
                }
                catch (Exception ex)
                {
                    KuduEventSource.Log.KuduException(
                        ServerConfiguration.GetRuntimeSiteName(),
                        "ScmHostingConfigurations.GetValue",
                        string.Empty,
                        string.Empty,
                        $"ScmHostingConfigurations: Fail to GetValue('{key}')",
                        ex.ToString());
                }
            }

            return((configs == null || !configs.TryGetValue(key, out string value)) ? defaultValue : value);
        }
Exemplo n.º 9
0
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            if (_logger != null && eventType <= TraceEventType.Information)
            {
                _logger.Log(format, args);

                KuduEventSource.Log.GenericEvent(
                    ServerConfiguration.GetRuntimeSiteName(),
                    string.Format(format, args),
                    System.Environment.GetEnvironmentVariable("x-ms-request-id") ?? string.Empty,
                    string.Empty,
                    string.Empty,
                    string.Empty);
            }

            _tracer.Trace(format, args);
        }
Exemplo n.º 10
0
        private static ITracer TraceStartup(HttpContext httpContext)
        {
            ITracer tracer = null;

            // 0 means this is the very first request starting up Kudu
            if (0 == Interlocked.Exchange(ref _traceStartup, 1))
            {
                tracer = TraceServices.CreateRequestTracer(httpContext);

                if (tracer != null && tracer.TraceLevel > TraceLevel.Off)
                {
                    var attribs = GetTraceAttributes(httpContext);

                    // force always trace
                    attribs[TraceExtensions.AlwaysTrace] = "1";

                    // Dump environment variables
                    foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
                    {
                        var key = (string)entry.Key;
                        if (key.StartsWith("SCM", StringComparison.OrdinalIgnoreCase))
                        {
                            attribs[key] = (string)entry.Value;
                        }
                    }

                    tracer.Trace(XmlTracer.StartupRequestTrace, attribs);
                }

                OperationManager.SafeExecute(() =>
                {
                    var requestId = (string)httpContext.Items[Constants.RequestIdHeader];
                    KuduEventSource.Log.GenericEvent(
                        ServerConfiguration.GetRuntimeSiteName(),
                        string.Format("StartupRequest pid:{0}, domain:{1}, UseSiteExtensionV1:{2}", Process.GetCurrentProcess().Id, AppDomain.CurrentDomain.Id, DeploymentSettingsExtension.UseSiteExtensionV1.Value),
                        requestId,
                        Environment.GetEnvironmentVariable(SettingsKeys.ScmType),
                        Environment.GetEnvironmentVariable(SettingsKeys.WebSiteSku),
                        BackgroundTask.KuduVersion.Value);
                });
            }

            return(tracer);
        }
Exemplo n.º 11
0
 private static void LogBeginRequest(HttpContext httpContext)
 {
     OperationManager.SafeExecute(() =>
     {
         var request   = httpContext.Request;
         var requestId = request.GetRequestId() ?? Guid.NewGuid().ToString();
         httpContext.Items[Constants.RequestIdHeader]    = requestId;
         httpContext.Items[Constants.RequestDateTimeUtc] = DateTime.UtcNow;
         KuduEventSource.Log.ApiEvent(
             ServerConfiguration.GetRuntimeSiteName(),
             "OnBeginRequest",
             request.RawUrl,
             request.HttpMethod,
             requestId,
             0,
             0,
             request.GetUserAgent());
     });
 }
Exemplo n.º 12
0
        public void DeprecatedApiUsed(string route, string userAgent, string method, string path)
        {
            path = NullToEmptyString(path);

            // Try not to send the same event (on the same path) more than once.
            if (DeprecatedApiPaths.ContainsKey(path))
            {
                return;
            }

            KuduEventSource.Log.DeprecatedApiUsed(
                ServerConfiguration.GetRuntimeSiteName(),
                NullToEmptyString(route),
                NullToEmptyString(userAgent),
                NullToEmptyString(method),
                path);

            DeprecatedApiPaths[path] = path;
        }
Exemplo n.º 13
0
        public void UnexpectedException(Exception ex, string method, string path, string result, string message, bool trace = true)
        {
            // this happen during unexpected situation and should not throw (masking the original exception)
            OperationManager.SafeExecute(() =>
            {
                if (ex.AbortedByKudu())
                {
                    return;
                }

                KuduEventSource.Log.KuduException(
                    ServerConfiguration.GetRuntimeSiteName(),
                    NullToEmptyString(method),
                    NullToEmptyString(path),
                    NullToEmptyString(result),
                    NullToEmptyString(message),
                    GetExceptionContent(ex, trace));
            });
        }
Exemplo n.º 14
0
        public void UnexpectedException(Exception exception, bool trace = true, string memberName = null, string sourceFilePath = null, int sourceLineNumber = 0)
        {
            // this happen during unexpected situation and should not throw (masking the original exception)
            OperationManager.SafeExecute(() =>
            {
                if (exception.AbortedByKudu())
                {
                    return;
                }

                KuduEventSource.Log.KuduException(
                    ServerConfiguration.GetRuntimeSiteName(),
                    string.Empty,
                    string.Empty,
                    string.Empty,
                    string.Empty,
                    GetExceptionContent(exception, trace, memberName, sourceFilePath, sourceLineNumber));
            });
        }
Exemplo n.º 15
0
        public static void Persist(string siteName, string kind, string requestId, string status, string details)
        {
            var info = new DeploymentCompletedInfo
            {
                TimeStamp = $"{DateTime.UtcNow:s}Z",
                SiteName  = siteName,
                Kind      = kind,
                RequestId = requestId,
                Status    = status,
                Details   = details ?? string.Empty
            };

            try
            {
                var path       = Path.Combine(System.Environment.ExpandEnvironmentVariables(@"%HOME%"), "site", "deployments");
                var file       = Path.Combine(path, $"{Constants.LatestDeployment}.json");
                var serializer = new JavaScriptSerializer();
                var content    = serializer.Serialize(info);
                FileSystemHelpers.EnsureDirectory(path);

                // write deployment info to %home%\site\deployments\LatestDeployment.json
                OperationManager.Attempt(() => FileSystemHelpers.Instance.File.WriteAllText(file, content));

                // write to etw
                KuduEventSource.Log.DeploymentCompleted(
                    ServerConfiguration.GetRuntimeSiteName(),
                    info.Kind,
                    info.RequestId,
                    info.Status,
                    info.Details);
            }
            catch (Exception ex)
            {
                KuduEventSource.Log.KuduException(
                    ServerConfiguration.GetRuntimeSiteName(),
                    string.Empty,
                    info.RequestId ?? string.Empty,
                    string.Empty,
                    string.Empty,
                    $"{ex}");
            }
        }
Exemplo n.º 16
0
 private static void LogErrorRequest(HttpContext httpContext, Exception ex)
 {
     OperationManager.SafeExecute(() =>
     {
         var request               = httpContext.Request;
         var response              = httpContext.Response;
         var requestId             = (string)httpContext.Items[Constants.RequestIdHeader];
         var requestTime           = (DateTime)httpContext.Items[Constants.RequestDateTimeUtc];
         var latencyInMilliseconds = (long)(DateTime.UtcNow - requestTime).TotalMilliseconds;
         KuduEventSource.Log.ApiEvent(
             ServerConfiguration.GetRuntimeSiteName(),
             $"OnErrorRequest {ex}",
             request.RawUrl,
             request.HttpMethod,
             requestId,
             response.StatusCode,
             latencyInMilliseconds,
             request.GetUserAgent());
     });
 }
Exemplo n.º 17
0
        private static void AddDiskSpaceInfo(Dictionary <string, object> telemetry)
        {
            if (Kudu.Core.Helpers.EnvironmentHelper.IsWindowsContainers())
            {
                return;
            }

            ulong freeBytes;
            ulong totalBytes;
            var   homePath        = Environment.GetEnvironmentVariable("HOME");
            var   localPath       = Environment.ExpandEnvironmentVariables("%SystemDrive%\\local");
            var   userProfilePath = Environment.ExpandEnvironmentVariables($"%SystemDrive%\\users\\{ServerConfiguration.GetRuntimeSiteName()}");

            OperationManager.SafeExecute(() =>
            {
                telemetry["homePath"] = homePath;
                Kudu.Core.Environment.GetDiskFreeSpace(homePath, out freeBytes, out totalBytes);
                telemetry["homeFreeMB"]  = freeBytes / (1024 * 1024);
                telemetry["homeTotalMB"] = totalBytes / (1024 * 1024);
            });

            OperationManager.SafeExecute(() =>
            {
                telemetry["localPath"] = localPath;
                Kudu.Core.Environment.GetDiskFreeSpace(localPath, out freeBytes, out totalBytes);
                telemetry["localFreeMB"]  = freeBytes / (1024 * 1024);
                telemetry["localTotalMB"] = totalBytes / (1024 * 1024);
            });

            OperationManager.SafeExecute(() =>
            {
                if (FileSystemHelpers.DirectoryExists(userProfilePath))
                {
                    telemetry["userProfilePath"] = userProfilePath;
                    Kudu.Core.Environment.GetDiskFreeSpace(userProfilePath, out freeBytes, out totalBytes);
                    telemetry["userProfileFreeMB"]  = freeBytes / (1024 * 1024);
                    telemetry["userProfileTotalMB"] = totalBytes / (1024 * 1024);
                }
            });
        }
Exemplo n.º 18
0
        public ISiteBuilder CreateBuilder(ITracer tracer, ILogger logger, IDeploymentSettingsManager settings, IRepository repository, DeploymentInfoBase deploymentInfo)
        {
            string repositoryRoot = repository.RepositoryPath;

            // Use the cached vs projects file finder for: a. better performance, b. ignoring solutions/projects under node_modules
            var fileFinder = new CachedVsProjectsFileFinder(repository);

            // If there's a custom deployment file then let that take over.
            var command = settings.GetValue(SettingsKeys.Command);

            if (!String.IsNullOrEmpty(command))
            {
                return(new CustomBuilder(_environment, settings, _propertyProvider, repositoryRoot, command));
            }

            // If the user provided specific generator arguments, that overrides any detection logic
            string scriptGeneratorArgs = settings.GetValue(SettingsKeys.ScriptGeneratorArgs);

            if (!String.IsNullOrEmpty(scriptGeneratorArgs))
            {
                return(new CustomGeneratorCommandSiteBuilder(_environment, settings, _propertyProvider, repositoryRoot, scriptGeneratorArgs));
            }

            // If the repository has an explicit pointer to a project path to be deployed
            // then use it.
            string targetProjectPath = settings.GetValue(SettingsKeys.Project);

            if (!String.IsNullOrEmpty(targetProjectPath))
            {
                tracer.Trace("Specific project was specified: " + targetProjectPath);
                targetProjectPath = Path.GetFullPath(Path.Combine(repositoryRoot, targetProjectPath.TrimStart('/', '\\')));
            }

            if (deploymentInfo != null && deploymentInfo.Deployer == Constants.OneDeploy)
            {
                var projectPath = !String.IsNullOrEmpty(targetProjectPath) ? targetProjectPath : repositoryRoot;
                return(new OneDeployBuilder(_environment, settings, _propertyProvider, repositoryRoot, projectPath, deploymentInfo));
            }

            if (settings.RunFromLocalZip())
            {
                return(new RunFromZipSiteBuilder());
            }

            if (!settings.DoBuildDuringDeployment())
            {
                var projectPath = !String.IsNullOrEmpty(targetProjectPath) ? targetProjectPath : repositoryRoot;
                return(new BasicBuilder(_environment, settings, _propertyProvider, repositoryRoot, projectPath));
            }

            string msbuild16Log = String.Format("UseMSBuild16: {0}", VsHelper.UseMSBuild16().ToString());

            tracer.Trace(msbuild16Log);
            KuduEventSource.Log.GenericEvent(
                ServerConfiguration.GetRuntimeSiteName(),
                msbuild16Log,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty
                );

            if (!String.IsNullOrEmpty(targetProjectPath))
            {
                // Try to resolve the project
                return(ResolveProject(repositoryRoot,
                                      targetProjectPath,
                                      settings,
                                      fileFinder,
                                      tryWebSiteProject: true,
                                      searchOption: SearchOption.TopDirectoryOnly));
            }

            // Get all solutions in the current repository path
            var solutions = VsHelper.GetSolutions(repositoryRoot, fileFinder).ToList();

            if (!solutions.Any())
            {
                return(ResolveProject(repositoryRoot,
                                      settings,
                                      fileFinder,
                                      searchOption: SearchOption.AllDirectories));
            }

            // More than one solution is ambiguous
            if (solutions.Count > 1)
            {
                // TODO: Show relative paths in error messages
                ThrowAmbiguousSolutionsError(solutions);
            }

            // We have a solution
            VsSolution solution = solutions[0];

            return(DetermineProjectFromSolution(logger, settings, repository, solution));
        }