Skip to content

Commit

Permalink
Enhance logging, update configs, and improve performance
Browse files Browse the repository at this point in the history
- Added logging to console in `Services.Actor.fs` for Cosmos DB details.
- Updated ASP.NET Core doc link in `Common.SDK.fs` to version 9.0.
- Modified comment in `Common.SDK.fs` for custom `SocketsHttpHandler`.
- Changed HTTP client timeout in `Common.SDK.fs` to 60s for non-debug builds.
- Removed commented-out logging line in `Common.SDK.fs`.
- Replaced fixed delay with conditional loop in `General.Server.Tests.fs` for Dapr init.
- Updated base image in `Grace.Server.fsproj` to .NET preview.7.
- Fixed namespace formatting in `LogRequestHeaders.Middleware.fs`.
- Added logging for request paths in `LogRequestHeaders.Middleware.fs`, excluding `/healthz`.
- Increased SAS token expiration in `Constants.Shared.fs` to 15 minutes.
- Reduced `MaxDegreeOfParallelism` in `Constants.Shared.fs` to 2x processor count.
- Enhanced `getObjectFileName` in `Utilities.Shared.fs` for files without extensions.
- Added Dapr environment variables to `kubernetes-deployment.yaml` for Grace server.
  • Loading branch information
ScottArbeit committed Aug 22, 2024
1 parent 1479c61 commit f46f24e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Grace.Actors/Services.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ module Services =
let mutable totalRecordsDeleted = 0
let overallStartTime = getCurrentInstant ()
queryRequestOptions.MaxItemCount <- 1000
logToConsole $"cosmosContainer.Id: {cosmosContainer.Id}; cosmosContainer.Database.Id: {cosmosContainer.Database.Id}; cosmosContainer.Database.Client.Endpoint: {cosmosContainer.Database.Client.Endpoint}."
let iterator = cosmosContainer.GetItemQueryIterator<DocumentIdentifier>(queryDefinition, requestOptions = queryRequestOptions)

while iterator.HasMoreResults do
Expand Down
7 changes: 3 additions & 4 deletions src/Grace.SDK/Common.SDK.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Common =
#endif

// This construct is equivalent to using IHttpClientFactory in the ASP.NET Dependency Injection container, for code (like this) that isn't using GenericHost.
// See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-7.0#alternatives-to-ihttpclientfactory for more information.
// See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-9.0#alternatives-to-ihttpclientfactory for more information.
let private socketsHttpHandler =
new SocketsHttpHandler(
AllowAutoRedirect = true, // We expect to use Traffic Manager or equivalents, so there will be redirects.
Expand All @@ -53,7 +53,7 @@ module Common =
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2.0) // Default is 2m
)

/// Gets an HttpClient instance from a custom HttpClientFactory.
/// Gets an HttpClient instance using our custom SocketsHttpHandler, with OpenTelemetry headers.
let getHttpClient (correlationId: string) =
let traceIdBytes = stackalloc<byte> 16
let parentIdBytes = stackalloc<byte> 8
Expand All @@ -72,7 +72,7 @@ module Common =
//httpClient.DefaultVersionPolicy <- HttpVersionPolicy.RequestVersionOrHigher
#if DEBUG
//httpClient.Timeout <- TimeSpan.FromSeconds(1800.0) // Keeps client commands open while debugging.
httpClient.Timeout <- TimeSpan.FromSeconds(10.0) // Fast fail for testing network connectivity.
httpClient.Timeout <- TimeSpan.FromSeconds(60.0)
#endif
httpClient

Expand Down Expand Up @@ -130,7 +130,6 @@ module Common =
try
use httpClient = getHttpClient parameters.CorrelationId
let serverUriWithRoute = Uri($"{Current().ServerUri}/{route}")
//logToConsole $"serverUriWithRoute: {serverUriWithRoute}"
let startTime = getCurrentInstant ()
let! response = httpClient.PostAsync(serverUriWithRoute, createJsonContent parameters)

Expand Down
6 changes: 3 additions & 3 deletions src/Grace.Server.Tests/General.Server.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ type Setup() =
sbOutput.Clear() |> ignore
sbError.Clear() |> ignore

do! Task.Delay(2000) // Give time for the containers to start.

let applicationInsightsConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")
let azureStorageKey = Environment.GetEnvironmentVariable("azurestoragekey")
let azureStorageConnectionString = Environment.GetEnvironmentVariable("azurestorageconnectionstring")
Expand Down Expand Up @@ -351,7 +349,9 @@ type Setup() =
// Give time for Dapr to warm up.
logToTestConsole "Waiting for Dapr to warm up..."
do! Task.Delay(8000)
//do! Task.Delay(8000)
while not <| (sbOutput.ToString().Contains("dapr initialized") && (sbOutput.ToString().Contains("Placement order received: unlock"))) do
do! Task.Delay(250)
logToTestConsole "Warm up complete."
// Create the owner we'll use for this test run.
Expand Down
2 changes: 1 addition & 1 deletion src/Grace.Server/Grace.Server.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Version>0.1</Version>
<Description>The server module for Grace Version Control System.</Description>
<UserSecretsId>f1167a88-7f15-49c3-8ea1-30c2608081c9</UserSecretsId>
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:9.0.0-preview.6</ContainerBaseImage>
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:9.0.0-preview.7</ContainerBaseImage>
<ContainerImageTags>0.1;latest</ContainerImageTags>
<!--<ContainerRegistry>registry.hub.docker.com</ContainerRegistry>-->
<ContainerRepository>scottarbeit/grace-server</ContainerRepository>
Expand Down
14 changes: 9 additions & 5 deletions src/Grace.Server/Middleware/LogRequestHeaders.Middleware.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Grace.Server.Middleware
namespace Grace.Server.Middleware

open Grace.Server
open Grace.Shared
Expand All @@ -23,13 +23,17 @@ type LogRequestHeadersMiddleware(next: RequestDelegate) =

context.Request.Headers["X-MiddlewareTraceIn"] <- $"{middlewareTraceHeader}{nameof (LogRequestHeadersMiddleware)} --> "
#endif
let path = context.Request.Path.ToString()
if path <> "/healthz" then
logToConsole $"In LogRequestHeadersMiddleware.Middleware.fs: Path: {path}."

let sb = StringBuilder()
if log.IsEnabled(LogLevel.Debug) then
let sb = StringBuilder()

context.Request.Headers
|> Seq.iter (fun kv -> sb.AppendLine($"{kv.Key} = {kv.Value}") |> ignore)
context.Request.Headers
|> Seq.iter (fun kv -> sb.AppendLine($"{kv.Key} = {kv.Value}") |> ignore)

log.LogDebug("Request headers: {headers}", sb.ToString())
log.LogDebug("Request headers: {headers}", sb.ToString())

// -----------------------------------------------------------------------------------------------------
// Pass control to next middleware instance...
Expand Down
4 changes: 2 additions & 2 deletions src/Grace.Shared/Constants.Shared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ module Constants =
let BlobCacheControl = "public,max-age=86400,no-transform"

/// The expiration time for a Shared Access Signature token, in minutes.
let SharedAccessSignatureExpiration = 2.0
let SharedAccessSignatureExpiration = 15.0

/// The path that indicates the root directory of the repository.
let RootDirectoryPath = "."
Expand Down Expand Up @@ -230,7 +230,7 @@ module Constants =
/// Grace's global settings for Parallel.ForEach/ForEachAsync expressions; sets MaxDegreeofParallelism to maximize performance.
// I'm choosing a higher-than-usual number here because these parallel loops are used in code where most of the time is spent on network
// and disk traffic - and therefore Task<'T> - and we can run lots of them simultaneously.
let ParallelOptions = ParallelOptions(MaxDegreeOfParallelism = Environment.ProcessorCount * 4)
let ParallelOptions = ParallelOptions(MaxDegreeOfParallelism = Environment.ProcessorCount * 2)

/// Default directory size magic value.
let InitialDirectorySize = int64 -1
Expand Down
5 changes: 4 additions & 1 deletion src/Grace.Shared/Utilities.Shared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ module Utilities =
/// Example: foo.txt with a SHA-256 hash of "8e798...980c" -> "foo_8e798...980c.txt".
let getObjectFileName (relativePath: string) (sha256Hash: string) =
let file = FileInfo(relativePath)
$"{file.Name.Replace(file.Extension, String.Empty)}_{sha256Hash}{file.Extension}"
if file.Extension = String.Empty then
$"{file.Name}_{sha256Hash}"
else
$"{file.Name.Replace(file.Extension, String.Empty)}_{sha256Hash}{file.Extension}"

/// Gets the name of the machine or node, without the prefix of the container name.
let getMachineName =
Expand Down
1 change: 1 addition & 0 deletions src/kubernetes-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data:
# ASPNETCORE_Kestrel__Certificates__Default__Password: CrypticPassword8=5
APPLICATIONINSIGHTS_CONNECTION_STRING: "InstrumentationKey=e0955eb4-1817-4a94-bf6e-d48f6ae54a8c;IngestionEndpoint=https://westus2-2.in.applicationinsights.azure.com/"
# Azure_CosmosDB_Connection_String: "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" # This is a well-known default key.
DAPR_APP_ID: "grace-server"
DAPR_APP_PORT: "5000"
DAPR_HTTP_PORT: "3500"
DAPR_GRPC_PORT: "50001"
Expand Down

0 comments on commit f46f24e

Please sign in to comment.