Skip to content

Commit

Permalink
Tracking for graphql.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jan 24, 2023
1 parent 9e79b9d commit 6ea95a6
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 53 deletions.
Expand Up @@ -68,41 +68,39 @@ public async Task On(Envelope<IEvent> @event)

private async Task OnAppContributorRemoved(AppContributorRemoved appContributorRemoved)
{
using (Telemetry.Activities.StartActivity("RemoveContributorFromSystem"))
{
var appId = appContributorRemoved.AppId.Id;
using var activity = Telemetry.Activities.StartActivity("RemoveContributorFromSystem");

var appId = appContributorRemoved.AppId.Id;

foreach (var deleter in deleters)
foreach (var deleter in deleters)
{
using (Telemetry.Activities.StartActivity(deleter.GetType().Name))
{
using (Telemetry.Activities.StartActivity(deleter.GetType().Name))
{
await deleter.DeleteContributorAsync(appId, appContributorRemoved.ContributorId, default);
}
await deleter.DeleteContributorAsync(appId, appContributorRemoved.ContributorId, default);
}
}
}

private async Task OnArchiveAsync(AppDeleted appArchived)
{
using (Telemetry.Activities.StartActivity("RemoveAppFromSystem"))
{
// Bypass our normal app resolve process, so that we can also retrieve the deleted app.
var app = factory.Create<AppDomainObject>(appArchived.AppId.Id);
using var activity = Telemetry.Activities.StartActivity("RemoveAppFromSystem");

await app.EnsureLoadedAsync();
// Bypass our normal app resolve process, so that we can also retrieve the deleted app.
var app = factory.Create<AppDomainObject>(appArchived.AppId.Id);

// If the app does not exist, the version is lower than zero.
if (app.Version < 0)
{
return;
}
await app.EnsureLoadedAsync();

foreach (var deleter in deleters)
// If the app does not exist, the version is lower than zero.
if (app.Version < 0)
{
return;
}

foreach (var deleter in deleters)
{
using (Telemetry.Activities.StartActivity(deleter.GetType().Name))
{
using (Telemetry.Activities.StartActivity(deleter.GetType().Name))
{
await deleter.DeleteAppAsync(app.Snapshot, default);
}
await deleter.DeleteAppAsync(app.Snapshot, default);
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs
Expand Up @@ -61,8 +61,10 @@ public sealed class AppsIndex : IAppsIndex, ICommandMiddleware, IInitializable
public async Task<List<IAppEntity>> GetAppsForUserAsync(string userId, PermissionSet permissions,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AppsIndex/GetAppsForUserAsync"))
using (var activity = Telemetry.Activities.StartActivity("AppsIndex/GetAppsForUserAsync"))
{
activity?.SetTag("userId", userId);

var apps = await appRepository.QueryAllAsync(userId, permissions.ToAppNames(), ct);

foreach (var app in apps.Where(IsValid))
Expand All @@ -77,8 +79,10 @@ public sealed class AppsIndex : IAppsIndex, ICommandMiddleware, IInitializable
public async Task<List<IAppEntity>> GetAppsForTeamAsync(DomainId teamId,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AppsIndex/GetAppsForTeamAsync"))
using (var activity = Telemetry.Activities.StartActivity("AppsIndex/GetAppsForTeamAsync"))
{
activity?.SetTag("teamId", teamId);

var apps = await appRepository.QueryAllAsync(teamId, ct);

foreach (var app in apps.Where(IsValid))
Expand All @@ -93,8 +97,10 @@ public sealed class AppsIndex : IAppsIndex, ICommandMiddleware, IInitializable
public async Task<IAppEntity?> GetAppAsync(string name, bool canCache = false,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AppsIndex/GetAppByNameAsync"))
using (var activity = Telemetry.Activities.StartActivity("AppsIndex/GetAppByNameAsync"))
{
activity?.SetTag("appName", name);

if (canCache)
{
if (appCache.TryGetValue(GetCacheKey(name), out var v) && v is IAppEntity cacheApp)
Expand Down Expand Up @@ -122,8 +128,10 @@ public sealed class AppsIndex : IAppsIndex, ICommandMiddleware, IInitializable
public async Task<IAppEntity?> GetAppAsync(DomainId appId, bool canCache = false,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AppsIndex/GetAppAsync"))
using (var activity = Telemetry.Activities.StartActivity("AppsIndex/GetAppAsync"))
{
activity?.SetTag("appId", appId);

if (canCache)
{
if (appCache.TryGetValue(GetCacheKey(appId), out var cached) && cached is IAppEntity cachedApp)
Expand Down
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Options;
using Squidex.Domain.Apps.Entities.Assets.Repositories;
using Squidex.Infrastructure;
using System.Diagnostics;

namespace Squidex.Domain.Apps.Entities.Assets.Queries;

Expand Down Expand Up @@ -39,8 +40,10 @@ public sealed class AssetQueryService : IAssetQueryService
public async Task<IReadOnlyList<IAssetFolderEntity>> FindAssetFolderAsync(DomainId appId, DomainId id,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AssetQueryService/FindAssetFolderAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/FindAssetFolderAsync"))
{
activity?.SetTag("assetId", id);

var result = new List<IAssetFolderEntity>();

while (id != DomainId.Empty)
Expand All @@ -65,8 +68,10 @@ public sealed class AssetQueryService : IAssetQueryService
public async Task<IResultList<IAssetFolderEntity>> QueryAssetFoldersAsync(DomainId appId, DomainId parentId,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AssetQueryService/QueryAssetFoldersAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/QueryAssetFoldersAsync"))
{
activity?.SetTag("folderId", parentId);

var assetFolders = await QueryFoldersCoreAsync(appId, parentId, ct);

return assetFolders;
Expand All @@ -76,8 +81,10 @@ public sealed class AssetQueryService : IAssetQueryService
public async Task<IResultList<IAssetFolderEntity>> QueryAssetFoldersAsync(Context context, DomainId parentId,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("AssetQueryService/QueryAssetFoldersAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/QueryAssetFoldersAsync"))
{
activity?.SetTag("folderId", parentId);

var assetFolders = await QueryFoldersCoreAsync(context, parentId, ct);

return assetFolders;
Expand All @@ -89,8 +96,12 @@ public sealed class AssetQueryService : IAssetQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("AssetQueryService/FindByHashAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/FindByHashAsync"))
{
activity?.SetTag("fileHash", hash);
activity?.SetTag("fileName", fileName);
activity?.SetTag("fileSize", fileSize);

var asset = await FindByHashCoreAsync(context, hash, fileName, fileSize, ct);

if (asset == null)
Expand All @@ -107,8 +118,10 @@ public sealed class AssetQueryService : IAssetQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("AssetQueryService/FindBySlugAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/FindBySlugAsync"))
{
activity?.SetTag("slug", slug);

var asset = await FindBySlugCoreAsync(context, slug, ct);

if (asset == null)
Expand All @@ -125,8 +138,10 @@ public sealed class AssetQueryService : IAssetQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("AssetQueryService/FindGlobalAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/FindGlobalAsync"))
{
activity?.SetTag("assetId", id);

var asset = await FindCoreAsync(id, ct);

if (asset == null)
Expand All @@ -143,8 +158,10 @@ public sealed class AssetQueryService : IAssetQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("AssetQueryService/FindAsync"))
using (var activity = Telemetry.Activities.StartActivity("AssetQueryService/FindAsync"))
{
activity?.SetTag("assetId", id);

IAssetEntity? asset;

if (version > EtagVersion.Empty)
Expand Down
Expand Up @@ -6,6 +6,7 @@
// ==========================================================================

using GraphQL;
using GraphQL.Execution;
using GraphQL.Resolvers;
using Squidex.Domain.Apps.Core.Subscriptions;
using Squidex.Infrastructure;
Expand All @@ -21,22 +22,42 @@ public static class Resolvers
{
public static IFieldResolver Sync<TSource, T>(Func<TSource, T> resolver)
{
return new FuncFieldResolver<TSource, T>(x => resolver(x.Source));
return new FuncFieldResolver<TSource, T>(c => resolver(c.Source));
}

public static IFieldResolver Sync<TSource, T>(Func<TSource, IResolveFieldContext, GraphQLExecutionContext, T> resolver)
{
return new FuncFieldResolver<TSource, T>(x => resolver(x.Source, x, (GraphQLExecutionContext)x.UserContext));
return new FuncFieldResolver<TSource, T>(c => resolver(c.Source, c, (GraphQLExecutionContext)c.UserContext));
}

public static IFieldResolver Async<TSource, T>(Func<TSource, ValueTask<T?>> resolver)
{
return new FuncFieldResolver<TSource, T>(x => resolver(x.Source));
return new FuncFieldResolver<TSource, T>(c => resolver(c.Source));
}

public static IFieldResolver Async<TSource, T>(Func<TSource, IResolveFieldContext, GraphQLExecutionContext, ValueTask<T?>> resolver)
{
return new FuncFieldResolver<TSource, T>(x => resolver(x.Source, x, (GraphQLExecutionContext)x.UserContext));
return new FuncFieldResolver<TSource, T>(async c =>
{
using (var activity = Telemetry.Activities.StartActivity($"gql/{c.FieldDefinition.Name}"))
{
activity?.SetTag("/gql/fieldName", c.FieldDefinition.Name);
activity?.SetTag("/gql/fieldType", c.FieldDefinition.ResolvedType?.ToString());
if (activity != null && c.Arguments != null)
{
foreach (var (key, value) in c.Arguments)
{
if (value.Source == ArgumentSource.Literal)
{
activity.SetTag($"arg/{key}", value);
}
}
}
return await resolver(c.Source, c, (GraphQLExecutionContext)c.UserContext);
}
});
}

public static IFieldResolver Command(string permissionId, Func<IResolveFieldContext, ICommand> action)
Expand Down
Expand Up @@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Diagnostics;
using Microsoft.Extensions.Options;
using Squidex.Domain.Apps.Entities.Contents.Repositories;
using Squidex.Domain.Apps.Entities.Schemas;
Expand Down Expand Up @@ -46,8 +47,11 @@ public sealed class ContentQueryService : IContentQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("ContentQueryService/FindAsync"))
using (var activity = Telemetry.Activities.StartActivity("ContentQueryService/FindAsync"))
{
activity?.SetTag("schemaName", schemaIdOrName);
activity?.SetTag("contentId", id);

var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName, ct);

IContentEntity? content;
Expand Down Expand Up @@ -80,8 +84,10 @@ public sealed class ContentQueryService : IContentQueryService
{
Guard.NotNull(context);

using (Telemetry.Activities.StartActivity("ContentQueryService/QueryAsync"))
using (var activity = Telemetry.Activities.StartActivity("ContentQueryService/QueryAsync"))
{
activity?.SetTag("schemaName", schemaIdOrName);

if (q == null)
{
return ResultList.Empty<IEnrichedContentEntity>();
Expand Down
Expand Up @@ -13,6 +13,8 @@
using Squidex.Infrastructure.States;
using Squidex.Infrastructure.Translations;
using Squidex.Infrastructure.Validation;
using System.Diagnostics;
using System.Xml.Linq;

namespace Squidex.Domain.Apps.Entities.Schemas.Indexes;

Expand All @@ -34,8 +36,10 @@ public sealed class SchemasIndex : ICommandMiddleware, ISchemasIndex
public async Task<List<ISchemaEntity>> GetSchemasAsync(DomainId appId,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("SchemasIndex/GetSchemasAsync"))
using (var activity = Telemetry.Activities.StartActivity("SchemasIndex/GetSchemasAsync"))
{
activity?.SetTag("appId", appId);

var schemas = await schemaRepository.QueryAllAsync(appId, ct);

foreach (var schema in schemas.Where(IsValid))
Expand All @@ -50,8 +54,11 @@ public sealed class SchemasIndex : ICommandMiddleware, ISchemasIndex
public async Task<ISchemaEntity?> GetSchemaAsync(DomainId appId, string name, bool canCache,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("SchemasIndex/GetSchemaByNameAsync"))
using (var activity = Telemetry.Activities.StartActivity("SchemasIndex/GetSchemaByNameAsync"))
{
activity?.SetTag("appId", appId);
activity?.SetTag("schemaName", name);

var cacheKey = GetCacheKey(appId, name);

if (canCache)
Expand Down Expand Up @@ -81,8 +88,11 @@ public sealed class SchemasIndex : ICommandMiddleware, ISchemasIndex
public async Task<ISchemaEntity?> GetSchemaAsync(DomainId appId, DomainId id, bool canCache,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("SchemasIndex/GetSchemaAsync"))
using (var activity = Telemetry.Activities.StartActivity("SchemasIndex/GetSchemaAsync"))
{
activity?.SetTag("appId", appId);
activity?.SetTag("schemaId", id);

var cacheKey = GetCacheKey(appId, id);

if (canCache)
Expand Down
Expand Up @@ -7,6 +7,7 @@

using Squidex.Domain.Apps.Entities.Teams.Repositories;
using Squidex.Infrastructure;
using System.Diagnostics;

namespace Squidex.Domain.Apps.Entities.Teams.Indexes;

Expand All @@ -22,8 +23,10 @@ public TeamsIndex(ITeamRepository teamRepository)
public async Task<ITeamEntity?> GetTeamAsync(DomainId id,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("TeamsIndex/GetTeamAsync"))
using (var activity = Telemetry.Activities.StartActivity("TeamsIndex/GetTeamAsync"))
{
activity?.SetTag("teamId", id);

var team = await teamRepository.FindAsync(id, ct);

return IsValid(team) ? team : null;
Expand All @@ -33,8 +36,10 @@ public TeamsIndex(ITeamRepository teamRepository)
public async Task<List<ITeamEntity>> GetTeamsAsync(string userId,
CancellationToken ct = default)
{
using (Telemetry.Activities.StartActivity("TeamsIndex/GetTeamsAsync"))
using (var activity = Telemetry.Activities.StartActivity("TeamsIndex/GetTeamsAsync"))
{
activity?.SetTag("userId", userId);

var teams = await teamRepository.QueryAllAsync(userId, ct);

return teams.Where(IsValid).ToList();
Expand Down
Expand Up @@ -85,6 +85,8 @@ public IActionResult GetImage(string app)
#pragma warning disable MA0040 // Flow the cancellation token
using var activity = Telemetry.Activities.StartActivity("Resize");

activity?.SetTag("fileType", mimeType);

await using var assetOriginal = new TempAssetFile(resizedAsset, mimeType, 0);
await using var assetResized = new TempAssetFile(resizedAsset, mimeType, 0);

Expand All @@ -102,7 +104,7 @@ await using (var originalStream = assetOriginal.OpenWrite())
}
}

using (Telemetry.Activities.StartActivity("Resize"))
using (Telemetry.Activities.StartActivity("Compute"))
{
try
{
Expand Down

0 comments on commit 6ea95a6

Please sign in to comment.