Skip to content

Commit

Permalink
one function for activity creation in Otel service
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Sep 24, 2024
1 parent de9fbc0 commit f26bc45
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 83 deletions.
37 changes: 12 additions & 25 deletions Backend/Controllers/WordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public WordController(IWordRepository repo, IWordService wordService, IProjectRe
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
public async Task<IActionResult> DeleteFrontierWord(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "deleting a word from Frontier");
OtelService.AddOtelTag(otelTagName, "deleting a word from Frontier");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -62,8 +61,7 @@ public async Task<IActionResult> DeleteFrontierWord(string projectId, string wor
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<Word>))]
public async Task<IActionResult> GetProjectWords(string projectId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting all words");
OtelService.AddOtelTag(otelTagName, "getting all words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -82,8 +80,7 @@ public async Task<IActionResult> GetProjectWords(string projectId)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Word))]
public async Task<IActionResult> GetWord(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting a word");
OtelService.AddOtelTag(otelTagName, "getting a word");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -107,8 +104,7 @@ public async Task<IActionResult> GetWord(string projectId, string wordId)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))]
public async Task<IActionResult> IsFrontierNonempty(string projectId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking if Frontier is nonempty");
OtelService.AddOtelTag(otelTagName, "checking if Frontier is nonempty");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -127,9 +123,7 @@ public async Task<IActionResult> IsFrontierNonempty(string projectId)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<Word>))]
public async Task<IActionResult> GetProjectFrontierWords(string projectId)
{

using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting all Frontier words");
OtelService.AddOtelTag(otelTagName, "getting all Frontier words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -148,8 +142,7 @@ public async Task<IActionResult> GetProjectFrontierWords(string projectId)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))]
public async Task<IActionResult> IsInFrontier(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking if Frontier contains a word");
OtelService.AddOtelTag(otelTagName, "checking if Frontier contains a word");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -168,8 +161,7 @@ public async Task<IActionResult> IsInFrontier(string projectId, string wordId)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<string>))]
public async Task<IActionResult> AreInFrontier(string projectId, [FromBody, BindRequired] List<string> wordIds)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking if Frontier contains given words");
OtelService.AddOtelTag(otelTagName, "checking if Frontier contains given words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand Down Expand Up @@ -200,8 +192,7 @@ public async Task<IActionResult> AreInFrontier(string projectId, [FromBody, Bind
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
public async Task<IActionResult> GetDuplicateId(string projectId, [FromBody, BindRequired] Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking for duplicates of a word");
OtelService.AddOtelTag(otelTagName, "checking for duplicates of a word");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -224,8 +215,7 @@ public async Task<IActionResult> GetDuplicateId(string projectId, [FromBody, Bin
public async Task<IActionResult> UpdateDuplicate(
string projectId, string dupId, [FromBody, BindRequired] Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "combining duplicate words");
OtelService.AddOtelTag(otelTagName, "combining duplicate words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand Down Expand Up @@ -261,8 +251,7 @@ public async Task<IActionResult> UpdateDuplicate(
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
public async Task<IActionResult> CreateWord(string projectId, [FromBody, BindRequired] Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "creating a word");
OtelService.AddOtelTag(otelTagName, "creating a word");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand All @@ -285,8 +274,7 @@ public async Task<IActionResult> CreateWord(string projectId, [FromBody, BindReq
public async Task<IActionResult> UpdateWord(
string projectId, string wordId, [FromBody, BindRequired] Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "updating words");
OtelService.AddOtelTag(otelTagName, "updating words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand Down Expand Up @@ -318,8 +306,7 @@ public async Task<IActionResult> RevertWords(
string projectId, [FromBody, BindRequired] Dictionary<string, string> wordIds)
{
// note: review tag description
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "reverting words");
OtelService.AddOtelTag(otelTagName, "reverting words");

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry, projectId))
{
Expand Down
20 changes: 10 additions & 10 deletions Backend/Otel/BackendActivitySource.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics;
// using System.Diagnostics;

namespace BackendFramework.Otel;
// namespace BackendFramework.Otel;

public class BackendActivitySource
{
public static ActivitySource Get()
{
// return new ActivitySource("service");
return new ActivitySource(OtelKernel.SourceName);
}
}
// public class BackendActivitySource
// {
// public static ActivitySource Get()
// {
// // return new ActivitySource("service");
// return new ActivitySource(OtelKernel.SourceName);
// }
// }
15 changes: 15 additions & 0 deletions Backend/Otel/OtelService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics;

namespace BackendFramework.Otel;

public class OtelService
{
public static void AddOtelTag(string key, object? value)
{
// return new ActivitySource("service");
// return new ActivitySource(OtelKernel.SourceName);

using var activity = new ActivitySource(OtelKernel.SourceName).StartActivity();
activity?.AddTag(key, value);
}
}
45 changes: 14 additions & 31 deletions Backend/Repositories/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,15 @@ private static FilterDefinition<Word> GetProjectWordsFilter(string projectId, Li
/// <summary> Finds all <see cref="Word"/>s with specified projectId </summary>
public async Task<List<Word>> GetAllWords(string projectId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting all words");
OtelService.AddOtelTag(otelTagName, "getting all words");

return await _wordDatabase.Words.Find(GetAllProjectWordsFilter(projectId)).ToListAsync();
}

/// <summary> Finds <see cref="Word"/> with specified wordId and projectId </summary>
public async Task<Word?> GetWord(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting a word");
OtelService.AddOtelTag(otelTagName, "getting a word");

var wordList = await _wordDatabase.Words.FindAsync(GetProjectWordFilter(projectId, wordId));
try
Expand All @@ -83,8 +81,7 @@ public async Task<List<Word>> GetAllWords(string projectId)
/// <returns> A bool: success of operation </returns>
public async Task<bool> DeleteAllWords(string projectId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "deleting all words from WordsCollection and Frontier");
OtelService.AddOtelTag(otelTagName, "deleting all words from WordsCollection and Frontier");

var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.Eq(x => x.ProjectId, projectId);
Expand Down Expand Up @@ -119,10 +116,7 @@ private static void PopulateBlankWordTimes(Word word)
/// <returns> The word created </returns>
public async Task<Word> Create(Word word)
{

using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "creating a word in WordsCollection and Frontier");

OtelService.AddOtelTag(otelTagName, "creating a word in WordsCollection and Frontier");

PopulateBlankWordTimes(word);
await _wordDatabase.Words.InsertOneAsync(word);
Expand All @@ -140,8 +134,7 @@ public async Task<Word> Create(Word word)
/// <returns> The words created </returns>
public async Task<List<Word>> Create(List<Word> words)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "creating words in WordsCollection and Frontier");
OtelService.AddOtelTag(otelTagName, "creating words in WordsCollection and Frontier");

if (words.Count == 0)
{
Expand All @@ -164,8 +157,7 @@ public async Task<List<Word>> Create(List<Word> words)
/// <returns> The word created </returns>
public async Task<Word> Add(Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "adding a word to WordsCollection");
OtelService.AddOtelTag(otelTagName, "adding a word to WordsCollection");

PopulateBlankWordTimes(word);
await _wordDatabase.Words.InsertOneAsync(word);
Expand All @@ -175,8 +167,7 @@ public async Task<Word> Add(Word word)
/// <summary> Checks if Frontier is nonempty for specified <see cref="Project"/> </summary>
public async Task<bool> IsFrontierNonempty(string projectId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking if Frontier is nonempty");
OtelService.AddOtelTag(otelTagName, "checking if Frontier is nonempty");

var word = await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId)).FirstOrDefaultAsync();
return word is not null;
Expand All @@ -185,27 +176,23 @@ public async Task<bool> IsFrontierNonempty(string projectId)
/// <summary> Checks if specified word is in Frontier for specified <see cref="Project"/> </summary>
public async Task<bool> IsInFrontier(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "checking if Frontier contains a word");
OtelService.AddOtelTag(otelTagName, "checking if Frontier contains a word");

return (await _wordDatabase.Frontier.CountDocumentsAsync(GetProjectWordFilter(projectId, wordId))) > 0;
}

/// <summary> Finds all <see cref="Word"/>s in the Frontier for specified <see cref="Project"/> </summary>
public async Task<List<Word>> GetFrontier(string projectId)
{

using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting all Frontier words");
OtelService.AddOtelTag(otelTagName, "getting all Frontier words");

return await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId)).ToListAsync();
}

/// <summary> Finds all <see cref="Word"/>s in Frontier of specified project with specified vern </summary>
public async Task<List<Word>> GetFrontierWithVernacular(string projectId, string vernacular)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "getting all words from Frontier with vern");
OtelService.AddOtelTag(otelTagName, "getting all words from Frontier with vern");

return await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId, vernacular)).ToListAsync();
}
Expand All @@ -215,8 +202,7 @@ public async Task<List<Word>> GetFrontierWithVernacular(string projectId, string
/// <returns> The word created </returns>
public async Task<Word> AddFrontier(Word word)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "adding a word to Frontier");
OtelService.AddOtelTag(otelTagName, "adding a word to Frontier");

await _wordDatabase.Frontier.InsertOneAsync(word);
return word;
Expand All @@ -227,8 +213,7 @@ public async Task<Word> AddFrontier(Word word)
/// <returns> The words created </returns>
public async Task<List<Word>> AddFrontier(List<Word> words)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "adding words to Frontier");
OtelService.AddOtelTag(otelTagName, "adding words to Frontier");

await _wordDatabase.Frontier.InsertManyAsync(words);
return words;
Expand All @@ -238,8 +223,7 @@ public async Task<List<Word>> AddFrontier(List<Word> words)
/// <returns> A bool: success of operation </returns>
public async Task<bool> DeleteFrontier(string projectId, string wordId)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "deleting a word from Frontier");
OtelService.AddOtelTag(otelTagName, "deleting a word from Frontier");

var deleted = await _wordDatabase.Frontier.DeleteOneAsync(GetProjectWordFilter(projectId, wordId));
return deleted.DeletedCount > 0;
Expand All @@ -249,8 +233,7 @@ public async Task<bool> DeleteFrontier(string projectId, string wordId)
/// <returns> Number of words deleted </returns>
public async Task<long> DeleteFrontier(string projectId, List<string> wordIds)
{
using var activity = BackendActivitySource.Get().StartActivity();
activity?.AddTag(otelTagName, "deleting words from Frontier");
OtelService.AddOtelTag(otelTagName, "deleting words from Frontier");

var deleted = await _wordDatabase.Frontier.DeleteManyAsync(GetProjectWordsFilter(projectId, wordIds));
return deleted.DeletedCount;
Expand Down
Loading

0 comments on commit f26bc45

Please sign in to comment.