Skip to content

Commit

Permalink
Dispose HealthCheck, Hercules and Log metrics as well
Browse files Browse the repository at this point in the history
  • Loading branch information
HolyPrapor committed Oct 7, 2021
1 parent 144c4b2 commit 8f45baa
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Vostok.Hosting/Components/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public void SubstituteTracer((ITracer tracer, TracerSettings tracerSettings) tra

public T RegisterDisposable<T>(T disposable)
{
disposables.Add(disposable);
if (disposable != null)
disposables.Add(disposable);
return disposable;
}

Expand Down
6 changes: 3 additions & 3 deletions Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context)
if (settings.SendAnnotations)
AnnotationsHelper.ReportLaunching(context.ApplicationIdentity, context.Metrics.Instance);

HerculesSinkMetrics.Measure(context.HerculesSink, context.Metrics, context.Log);
context.RegisterDisposable(HerculesSinkMetrics.Measure(context.HerculesSink, context.Metrics, context.Log));

if (settings.ConfigureStaticProviders)
FlowingContext.Configuration.ErrorCallback = (errorMessage, error) => context.Log.ForContext(typeof(FlowingContext)).Error(error, errorMessage);
Expand Down Expand Up @@ -270,14 +270,14 @@ private VostokHostingEnvironment BuildInner(BuildContext context)
context.Log = context.Logs.BuildCompositeLog(out _);
}

LogLevelMetrics.Measure(context.Logs.LogEventLevelCounterFactory.CreateCounter(), context.Metrics);
context.RegisterDisposable(LogLevelMetrics.Measure(context.Logs.LogEventLevelCounterFactory.CreateCounter(), context.Metrics));

if (settings.ConfigureStaticProviders)
StaticProvidersHelper.Configure(vostokHostingEnvironment);

context.DiagnosticsHub.HealthTracker.LaunchPeriodicalChecks(vostokHostingEnvironment.ShutdownToken);

HealthCheckMetrics.Measure(context.DiagnosticsHub.HealthTracker, context.Metrics);
context.RegisterDisposable(HealthCheckMetrics.Measure(context.DiagnosticsHub.HealthTracker, context.Metrics));

return vostokHostingEnvironment;
}
Expand Down
10 changes: 4 additions & 6 deletions Vostok.Hosting/Components/Hercules/HerculesSinkMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ private HerculesSinkMetrics(HerculesSink herculesSink, IMetricContext context, I
this.log = log.ForContext<HerculesSink>();
this.herculesSink = herculesSink;
tags = context.Tags;

context.Register(this, ScrapePeriod);
}

public static void Measure(IHerculesSink herculesSink, IVostokApplicationMetrics context, ILog log)
public static IDisposable Measure(IHerculesSink herculesSink, IVostokApplicationMetrics context, ILog log)
{
if (!(herculesSink is HerculesSink sink))
return;
return null;

// ReSharper disable once ObjectCreationAsStatement
new HerculesSinkMetrics(sink, context.Application.WithTag(WellKnownTagKeys.Component, "HerculesSink"), log);
var builtContext = context.Application.WithTag(WellKnownTagKeys.Component, "HerculesSink");
return builtContext.Register(new HerculesSinkMetrics(sink, builtContext, log), ScrapePeriod);
}

public IEnumerable<MetricEvent> Scrape(DateTimeOffset timestamp)
Expand Down
15 changes: 6 additions & 9 deletions Vostok.Hosting/Components/Log/LogLevelMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ internal class LogLevelMetrics
{
private readonly LogEventLevelCounter counter;

public LogLevelMetrics(LogEventLevelCounter counter, IMetricContext context)
private LogLevelMetrics(LogEventLevelCounter counter)
{
this.counter = counter;

context.CreateMultiFuncGauge(ProvideMetrics);
}

public static void Measure(LogEventLevelCounter counter, IVostokApplicationMetrics context)
public static IDisposable Measure(LogEventLevelCounter counter, IVostokApplicationMetrics context)
{
// ReSharper disable once ObjectCreationAsStatement
new LogLevelMetrics(
counter,
context.Instance
return context.Instance
.WithTag(WellKnownTagKeys.Component, "VostokLog")
.WithTag(WellKnownTagKeys.Name, "EventsByLevel"));
.WithTag(WellKnownTagKeys.Name, "EventsByLevel")
.CreateMultiFuncGauge(new LogLevelMetrics(counter).ProvideMetrics)
as IDisposable;
}

private IEnumerable<MetricDataPoint> ProvideMetrics()
Expand Down
6 changes: 3 additions & 3 deletions Vostok.Hosting/Components/Metrics/HealthCheckMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ private HealthCheckMetrics(IHealthTracker healthTracker, IMetricContext context)
healthTracker.ObserveReports().Subscribe(this);
}

public static void Measure(IHealthTracker healthTracker, IVostokApplicationMetrics context)
public static IDisposable Measure(IHealthTracker healthTracker, IVostokApplicationMetrics context)
{
// ReSharper disable once ObjectCreationAsStatement
new HealthCheckMetrics(healthTracker, context.Instance.WithTag(WellKnownTagKeys.Component, "VostokHealthChecks"));
var metrics = new HealthCheckMetrics(healthTracker, context.Instance.WithTag(WellKnownTagKeys.Component, "VostokHealthChecks"));
return healthTracker.ObserveReports().Subscribe(metrics);
}

public void OnCompleted()
Expand Down

0 comments on commit 8f45baa

Please sign in to comment.