Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit ee37da5

Browse files
stishkinstas
authored andcommitted
instance config (#1791)
* instance config * address PR comments * make logs scoped * make logs scoped Co-authored-by: stas <statis@microsoft.com>
1 parent b2cb82c commit ee37da5

23 files changed

+539
-136
lines changed

src/ApiService/ApiService/ApiService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
66
<OutputType>Exe</OutputType>
77
<Nullable>enable</Nullable>
8+
<WarningLevel>5</WarningLevel>
89
</PropertyGroup>
910
<ItemGroup>
1011
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.0.0" />

src/ApiService/ApiService/EnvironmentVariables.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ static EnvironmentVariables()
2222
//TODO: Add environment variable to control where to write logs to
2323
public static LogDestination[] LogDestinations { get; set; }
2424

25+
//TODO: Get this from Environment variable
26+
public static ApplicationInsights.DataContracts.SeverityLevel LogSeverityLevel() { return ApplicationInsights.DataContracts.SeverityLevel.Verbose; }
27+
2528
public static class AppInsights
2629
{
2730
public static string? AppId { get => Environment.GetEnvironmentVariable("APPINSIGHTS_APPID"); }

src/ApiService/ApiService/Info.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/ApiService/ApiService/Log.cs

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,34 @@ public interface ILogTracer
132132
void ForceFlush();
133133
void Info(string message);
134134
void Warning(string message);
135+
void Verbose(string message);
135136

136-
ILogTracer AddTags((string, string)[]? tags);
137+
ILogTracer WithTag(string k, string v);
138+
ILogTracer WithTags((string, string)[]? tags);
137139
}
138140

139-
public class LogTracer : ILogTracer
141+
internal interface ILogTracerInternal : ILogTracer
142+
{
143+
void ReplaceCorrelationId(Guid newCorrelationId);
144+
void AddTags((string, string)[] tags);
145+
}
146+
147+
148+
149+
public class LogTracer : ILogTracerInternal
140150
{
141151
private string? GetCaller()
142152
{
143153
return new StackTrace()?.GetFrame(2)?.GetMethod()?.DeclaringType?.FullName;
144154
}
145155

156+
private Guid _correlationId;
146157
private List<ILog> _loggers;
158+
private Dictionary<string, string> _tags;
159+
private SeverityLevel _logSeverityLevel;
147160

148-
public Guid CorrelationId { get; }
149-
public IReadOnlyDictionary<string, string> Tags { get; }
150-
161+
public Guid CorrelationId => _correlationId;
162+
public IReadOnlyDictionary<string, string> Tags => _tags;
151163

152164
private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]? tags)
153165
{
@@ -166,17 +178,43 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
166178
}
167179
}
168180

169-
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers) : this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers) { }
181+
public LogTracer(Guid correlationId, (string, string)[]? tags, List<ILog> loggers, SeverityLevel logSeverityLevel) :
182+
this(correlationId, new Dictionary<string, string>(ConvertTags(tags)), loggers, logSeverityLevel)
183+
{ }
170184

171185

172-
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers)
186+
public LogTracer(Guid correlationId, IReadOnlyDictionary<string, string> tags, List<ILog> loggers, SeverityLevel logSeverityLevel)
173187
{
174-
CorrelationId = correlationId;
175-
Tags = tags;
188+
_correlationId = correlationId;
189+
_tags = new(tags);
176190
_loggers = loggers;
191+
_logSeverityLevel = logSeverityLevel;
192+
}
193+
194+
//Single threaded only
195+
public void ReplaceCorrelationId(Guid newCorrelationId)
196+
{
197+
_correlationId = newCorrelationId;
198+
}
199+
200+
//single threaded only
201+
public void AddTags((string, string)[] tags)
202+
{
203+
if (tags is not null)
204+
{
205+
foreach (var (k, v) in tags)
206+
{
207+
_tags[k] = v;
208+
}
209+
}
177210
}
178211

179-
public ILogTracer AddTags((string, string)[]? tags)
212+
public ILogTracer WithTag(string k, string v)
213+
{
214+
return WithTags(new[] { (k, v) });
215+
}
216+
217+
public ILogTracer WithTags((string, string)[]? tags)
180218
{
181219
var newTags = new Dictionary<string, string>(Tags);
182220
if (tags is not null)
@@ -186,42 +224,66 @@ public ILogTracer AddTags((string, string)[]? tags)
186224
newTags[k] = v;
187225
}
188226
}
189-
return new LogTracer(CorrelationId, newTags, _loggers);
227+
return new LogTracer(CorrelationId, newTags, _loggers, _logSeverityLevel);
228+
}
229+
230+
public void Verbose(string message)
231+
{
232+
if (_logSeverityLevel >= SeverityLevel.Verbose)
233+
{
234+
var caller = GetCaller();
235+
foreach (var logger in _loggers)
236+
{
237+
logger.Log(CorrelationId, message, SeverityLevel.Verbose, Tags, caller);
238+
}
239+
}
190240
}
191241

192242
public void Info(string message)
193243
{
194-
var caller = GetCaller();
195-
foreach (var logger in _loggers)
244+
if (_logSeverityLevel >= SeverityLevel.Information)
196245
{
197-
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
246+
var caller = GetCaller();
247+
foreach (var logger in _loggers)
248+
{
249+
logger.Log(CorrelationId, message, SeverityLevel.Information, Tags, caller);
250+
}
198251
}
199252
}
200253

201254
public void Warning(string message)
202255
{
203-
var caller = GetCaller();
204-
foreach (var logger in _loggers)
256+
if (_logSeverityLevel >= SeverityLevel.Warning)
205257
{
206-
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
258+
var caller = GetCaller();
259+
foreach (var logger in _loggers)
260+
{
261+
logger.Log(CorrelationId, message, SeverityLevel.Warning, Tags, caller);
262+
}
207263
}
208264
}
209265

210266
public void Error(string message)
211267
{
212-
var caller = GetCaller();
213-
foreach (var logger in _loggers)
268+
if (_logSeverityLevel >= SeverityLevel.Error)
214269
{
215-
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
270+
var caller = GetCaller();
271+
foreach (var logger in _loggers)
272+
{
273+
logger.Log(CorrelationId, message, SeverityLevel.Error, Tags, caller);
274+
}
216275
}
217276
}
218277

219278
public void Critical(string message)
220279
{
221-
var caller = GetCaller();
222-
foreach (var logger in _loggers)
280+
if (_logSeverityLevel >= SeverityLevel.Critical)
223281
{
224-
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
282+
var caller = GetCaller();
283+
foreach (var logger in _loggers)
284+
{
285+
logger.Log(CorrelationId, message, SeverityLevel.Critical, Tags, caller);
286+
}
225287
}
226288
}
227289

@@ -254,7 +316,7 @@ public void ForceFlush()
254316

255317
public interface ILogTracerFactory
256318
{
257-
LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null);
319+
LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose);
258320
}
259321

260322
public class LogTracerFactory : ILogTracerFactory
@@ -266,9 +328,9 @@ public LogTracerFactory(List<ILog> loggers)
266328
_loggers = loggers;
267329
}
268330

269-
public LogTracer MakeLogTracer(Guid correlationId, (string, string)[]? tags = null)
331+
public LogTracer CreateLogTracer(Guid correlationId, (string, string)[]? tags = null, SeverityLevel severityLevel = SeverityLevel.Verbose)
270332
{
271-
return new(correlationId, tags, _loggers);
333+
return new(correlationId, tags, _loggers, severityLevel);
272334
}
273335

274336
}

src/ApiService/ApiService/OneFuzzTypes/Enums.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
public enum ErrorCode
1+
namespace Microsoft.OneFuzz.Service;
2+
public enum ErrorCode
23
{
34
INVALID_REQUEST = 450,
45
INVALID_PERMISSION = 451,

src/ApiService/ApiService/OneFuzzTypes/Events.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public EventType GetEventType()
4343
this switch
4444
{
4545
EventNodeHeartbeat _ => EventType.NodeHeartbeat,
46+
EventInstanceConfigUpdated _ => EventType.InstanceConfigUpdated,
4647
_ => throw new NotImplementedException(),
4748
};
4849

@@ -243,7 +244,7 @@ PoolName PoolName
243244
// ) : BaseEvent();
244245

245246

246-
// record EventInstanceConfigUpdated(
247-
// InstanceConfig Config
248-
// ) : BaseEvent();
247+
record EventInstanceConfigUpdated(
248+
InstanceConfig Config
249+
) : BaseEvent();
249250
}

0 commit comments

Comments
 (0)