From f26bc453cee1aa028a289c2233af82b73219889c Mon Sep 17 00:00:00 2001 From: Andra Constantin Date: Tue, 24 Sep 2024 09:54:13 -0400 Subject: [PATCH] one function for activity creation in Otel service --- Backend/Controllers/WordController.cs | 37 +++++++-------------- Backend/Otel/BackendActivitySource.cs | 20 ++++++------ Backend/Otel/OtelService.cs | 15 +++++++++ Backend/Repositories/WordRepository.cs | 45 ++++++++------------------ Backend/Services/WordService.cs | 25 +++++--------- 5 files changed, 59 insertions(+), 83 deletions(-) create mode 100644 Backend/Otel/OtelService.cs diff --git a/Backend/Controllers/WordController.cs b/Backend/Controllers/WordController.cs index b1f8f847ad..0c454c6fd7 100644 --- a/Backend/Controllers/WordController.cs +++ b/Backend/Controllers/WordController.cs @@ -36,8 +36,7 @@ public WordController(IWordRepository repo, IWordService wordService, IProjectRe [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))] public async Task 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)) { @@ -62,8 +61,7 @@ public async Task DeleteFrontierWord(string projectId, string wor [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List))] public async Task 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)) { @@ -82,8 +80,7 @@ public async Task GetProjectWords(string projectId) [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Word))] public async Task 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)) { @@ -107,8 +104,7 @@ public async Task GetWord(string projectId, string wordId) [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))] public async Task 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)) { @@ -127,9 +123,7 @@ public async Task IsFrontierNonempty(string projectId) [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List))] public async Task 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)) { @@ -148,8 +142,7 @@ public async Task GetProjectFrontierWords(string projectId) [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(bool))] public async Task 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)) { @@ -168,8 +161,7 @@ public async Task IsInFrontier(string projectId, string wordId) [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List))] public async Task AreInFrontier(string projectId, [FromBody, BindRequired] List 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)) { @@ -200,8 +192,7 @@ public async Task AreInFrontier(string projectId, [FromBody, Bind [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))] public async Task 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)) { @@ -224,8 +215,7 @@ public async Task GetDuplicateId(string projectId, [FromBody, Bin public async Task 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)) { @@ -261,8 +251,7 @@ public async Task UpdateDuplicate( [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))] public async Task 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)) { @@ -285,8 +274,7 @@ public async Task CreateWord(string projectId, [FromBody, BindReq public async Task 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)) { @@ -318,8 +306,7 @@ public async Task RevertWords( string projectId, [FromBody, BindRequired] Dictionary 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)) { diff --git a/Backend/Otel/BackendActivitySource.cs b/Backend/Otel/BackendActivitySource.cs index eb194191d9..8848024af2 100644 --- a/Backend/Otel/BackendActivitySource.cs +++ b/Backend/Otel/BackendActivitySource.cs @@ -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); +// } +// } diff --git a/Backend/Otel/OtelService.cs b/Backend/Otel/OtelService.cs new file mode 100644 index 0000000000..87a396af42 --- /dev/null +++ b/Backend/Otel/OtelService.cs @@ -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); + } +} diff --git a/Backend/Repositories/WordRepository.cs b/Backend/Repositories/WordRepository.cs index a9771fa6f3..753585f20e 100644 --- a/Backend/Repositories/WordRepository.cs +++ b/Backend/Repositories/WordRepository.cs @@ -55,8 +55,7 @@ private static FilterDefinition GetProjectWordsFilter(string projectId, Li /// Finds all s with specified projectId public async Task> 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(); } @@ -64,8 +63,7 @@ public async Task> GetAllWords(string projectId) /// Finds with specified wordId and projectId public async Task 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 @@ -83,8 +81,7 @@ public async Task> GetAllWords(string projectId) /// A bool: success of operation public async Task 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(); var filter = filterDef.Eq(x => x.ProjectId, projectId); @@ -119,10 +116,7 @@ private static void PopulateBlankWordTimes(Word word) /// The word created public async Task 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); @@ -140,8 +134,7 @@ public async Task Create(Word word) /// The words created public async Task> Create(List 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) { @@ -164,8 +157,7 @@ public async Task> Create(List words) /// The word created public async Task 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); @@ -175,8 +167,7 @@ public async Task Add(Word word) /// Checks if Frontier is nonempty for specified public async Task 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; @@ -185,8 +176,7 @@ public async Task IsFrontierNonempty(string projectId) /// Checks if specified word is in Frontier for specified public async Task 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; } @@ -194,9 +184,7 @@ public async Task IsInFrontier(string projectId, string wordId) /// Finds all s in the Frontier for specified public async Task> 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(); } @@ -204,8 +192,7 @@ public async Task> GetFrontier(string projectId) /// Finds all s in Frontier of specified project with specified vern public async Task> 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(); } @@ -215,8 +202,7 @@ public async Task> GetFrontierWithVernacular(string projectId, string /// The word created public async Task 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; @@ -227,8 +213,7 @@ public async Task AddFrontier(Word word) /// The words created public async Task> AddFrontier(List 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; @@ -238,8 +223,7 @@ public async Task> AddFrontier(List words) /// A bool: success of operation public async Task 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; @@ -249,8 +233,7 @@ public async Task DeleteFrontier(string projectId, string wordId) /// Number of words deleted public async Task DeleteFrontier(string projectId, List 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; diff --git a/Backend/Services/WordService.cs b/Backend/Services/WordService.cs index 2449b80128..777cf197fd 100644 --- a/Backend/Services/WordService.cs +++ b/Backend/Services/WordService.cs @@ -38,8 +38,7 @@ private static Word PrepEditedData(string userId, Word word) /// The created word public async Task Create(string userId, Word word) { - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "creating a word"); + OtelService.AddOtelTag(otelTagName, "creating a word"); return await _wordRepo.Create(PrepEditedData(userId, word)); } @@ -48,8 +47,7 @@ public async Task Create(string userId, Word word) /// The created word public async Task> Create(string userId, List words) { - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "creating words"); + OtelService.AddOtelTag(otelTagName, "creating words"); return await _wordRepo.Create(words.Select(w => PrepEditedData(userId, w)).ToList()); } @@ -66,8 +64,7 @@ private async Task Add(string userId, Word word) public async Task Delete(string projectId, string userId, string wordId) { // note: review tag description - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "deleting a word"); + OtelService.AddOtelTag(otelTagName, "deleting a word"); var wordIsInFrontier = await _wordRepo.DeleteFrontier(projectId, wordId); @@ -101,8 +98,7 @@ public async Task Delete(string projectId, string userId, string wordId) /// New word public async Task Delete(string projectId, string userId, string wordId, string fileName) { - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "deleting an audio"); + OtelService.AddOtelTag(otelTagName, "deleting an audio"); var wordWithAudioToDelete = await _wordRepo.GetWord(projectId, wordId); if (wordWithAudioToDelete is null) @@ -132,9 +128,7 @@ public async Task Delete(string projectId, string userId, string wordId) /// A string: id of new word public async Task DeleteFrontierWord(string projectId, string userId, string wordId) { - // note: review tag description - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "deleting a word from Frontier"); + OtelService.AddOtelTag(otelTagName, "deleting a word from Frontier"); var wordIsInFrontier = await _wordRepo.DeleteFrontier(projectId, wordId); if (!wordIsInFrontier) @@ -159,8 +153,7 @@ public async Task Delete(string projectId, string userId, string wordId) /// A bool: true if successful, false if any don't exist or are already in the Frontier. public async Task RestoreFrontierWords(string projectId, List wordIds) { - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "restoring words to Frontier"); + OtelService.AddOtelTag(otelTagName, "restoring words to Frontier"); var words = new List(); foreach (var id in wordIds) @@ -180,8 +173,7 @@ public async Task RestoreFrontierWords(string projectId, List word /// A bool: success of operation public async Task Update(string projectId, string userId, string wordId, Word word) { - using var activity = BackendActivitySource.Get().StartActivity(); - activity?.AddTag(otelTagName, "updating a word in Frontier"); + OtelService.AddOtelTag(otelTagName, "updating a word in Frontier"); var wordIsInFrontier = await _wordRepo.DeleteFrontier(projectId, wordId); @@ -203,8 +195,7 @@ public async Task Update(string projectId, string userId, string wordId, W /// The id string of the existing word, or null if none. public async Task FindContainingWord(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"); var wordsWithVern = await _wordRepo.GetFrontierWithVernacular(word.ProjectId, word.Vernacular); var duplicatedWord = wordsWithVern.Find(w => w.Contains(word));