Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dicko2 committed Dec 12, 2024
1 parent 7946309 commit 6cf0f4b
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/Agoda.DevFeedback.AspNetStartup/TimedStartupPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Agoda.DevFeedback.AspNetStartup
Expand All @@ -10,27 +11,20 @@ internal static class TimedStartupPublisher
{
public static void Publish(string type, TimeSpan timeTaken)
{
var gitContext = GitContextReader.GetGitContext();

var tags = new Dictionary<string, string>();
const string TAG_PREFIX = "DEVFEEDBACK_TAG_";
const string TagPrefix = "DEVFEEDBACK_TAG_";

// Get all environment variables and filter for ones starting with DEVFEEDBACK_TAG_
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
string key = de.Key.ToString();
if (key.StartsWith(TAG_PREFIX, StringComparison.OrdinalIgnoreCase))
{
// Remove the prefix to get the clean tag name
string tagName = key.Substring(TAG_PREFIX.Length).ToLowerInvariant();
string tagValue = de.Value?.ToString() ?? string.Empty;
var gitContext = GitContextReader.GetGitContext();

if (!string.IsNullOrEmpty(tagValue))
{
tags.Add(tagName, tagValue);
}
}
}
// Use LINQ and dictionary comprehension for cleaner tag collection
var tags = Environment.GetEnvironmentVariables()
.Cast<DictionaryEntry>()
.Select(entry => (Key: entry.Key.ToString()!, Value: entry.Value?.ToString()))
.Where(entry => entry.Key.StartsWith(TagPrefix, StringComparison.OrdinalIgnoreCase)
&& !string.IsNullOrEmpty(entry.Value))
.ToDictionary(
entry => entry.Key[TagPrefix.Length..].ToLowerInvariant(),
entry => entry.Value!
);

var result = new DevFeedbackData(
metricsVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
Expand Down

0 comments on commit 6cf0f4b

Please sign in to comment.