Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable style cop #111

Merged
merged 34 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
57a9297
Fix style in BufferBuilder.cs
ogaca-dd Apr 3, 2020
aea4f90
Fix style for AdvancedStatsConfig.cs
ogaca-dd Apr 3, 2020
26e5ec2
Fix style for StatsBufferize
ogaca-dd Apr 3, 2020
95f0b63
Fix style for StatsBufferizeFactory
ogaca-dd Apr 3, 2020
50f3296
Fix style for files in Bufferize folder.
ogaca-dd Apr 6, 2020
3d27092
Fix style for file in Worker folder.
ogaca-dd Apr 6, 2020
95803e1
Fix style for UnixEndPoint.cs including mix between tabs and spaces.
ogaca-dd Apr 6, 2020
f94e1ff
Fix style for IDogStatsd.cs
ogaca-dd Apr 6, 2020
8516f7f
Fix style for interfaces.
ogaca-dd Apr 6, 2020
258d4e3
Fix style for MetricsTimer
ogaca-dd Apr 6, 2020
56a0377
Fix style for SocketSender.
ogaca-dd Apr 6, 2020
1c936f6
Fix style for Statsd
ogaca-dd Apr 6, 2020
f954c3c
Fix style for StatsSender
ogaca-dd Apr 6, 2020
7540600
Fix style for StatsBuilder.
ogaca-dd Apr 6, 2020
105d68e
Fix style for StatsdUDP
ogaca-dd Apr 6, 2020
a0153e1
Fix style for Telemetry.
ogaca-dd Apr 6, 2020
79957d0
Fix style for Dogstatsd.cs
ogaca-dd Apr 6, 2020
b0da6e2
Minors style fixes.
ogaca-dd Apr 6, 2020
0b6bd11
Move properties before methods. Move public members before private.
ogaca-dd Apr 6, 2020
c9062ea
Readonly fields should appear before non-readonly fields
ogaca-dd Apr 6, 2020
e5abc08
Fix "A field should not follow a property"
ogaca-dd Apr 6, 2020
59187f5
Move public members before private ones.
ogaca-dd Apr 6, 2020
e213b57
Move inner classes at the end.
ogaca-dd Apr 6, 2020
721ca03
Fix "A constructor should not follow a method"
ogaca-dd Apr 6, 2020
da36d25
Fix: Static members should appear before non-static members
ogaca-dd Apr 7, 2020
80cf9d9
Move public methods before internal before private.
ogaca-dd Apr 7, 2020
0c3a87e
Fix Field 'XXXX' should not contain an underscore
ogaca-dd Apr 7, 2020
1788049
Minor style fixes.
ogaca-dd Apr 7, 2020
f0ab33a
Enable stylecop for StatsdClient.csproj
ogaca-dd Apr 7, 2020
9344fd9
Enforce StyleCop in appveyor.
ogaca-dd Apr 7, 2020
7bb77cb
Temporary disable SA0001: XML comment analysis is disabled due to pro…
ogaca-dd Apr 7, 2020
b5676ac
Merge branch 'olivierg/AddTelemetry' of github.com:DataDog/dogstatsd-…
ogaca-dd Apr 24, 2020
e946261
Fix formating.
ogaca-dd Apr 24, 2020
df35ece
Check StyleCop in the CI.
ogaca-dd Apr 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions BuildWarningAsError.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# When there are only warnings, the exit code is 0.
# See https://github.com/dotnet/installer/issues/1708.

dotnet build /warnaserror *> build.txt
Get-Content build.txt
$noError = Select-String -Path build.txt -pattern "0 Error\(s\)"
Exit $noError -eq $null
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ before_build:
- cmd: dotnet --info
- cmd: dotnet restore

build_script:
- cmd: Powershell -File BuildWarningAsError.ps1

test_script:
- cmd: dotnet test tests/StatsdClient.Tests/

Expand Down
18 changes: 9 additions & 9 deletions src/StatsdClient/AdvancedStatsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ namespace StatsdClient
{
public class AdvancedStatsConfig
{
public AdvancedStatsConfig()
{
DurationBeforeSendingNotFullBuffer = TimeSpan.FromMilliseconds(100);
}

/// <summary>
/// Metrics are sent asynchronously using a queue.
/// Metrics are sent asynchronously using a queue.
/// This value is the maximum number of metrics in the queue.
/// A small value reduces memory usage whereas an higher value reduces
/// latency (When `MaxBlockDuration` is null) or the number of messages
/// A small value reduces memory usage whereas an higher value reduces
/// latency (When `MaxBlockDuration` is null) or the number of messages
/// dropped (When `MaxBlockDuration` is not null).
/// </summary>
public int MaxMetricsInAsyncQueue { get; set; } = 100 * 1000;

/// <summary>
/// If there are more metrics than `MaxMetricsInAsyncQueue` waiting to be sent:
/// - if `MaxBlockDuration` is null, the metric send by a call to a
/// - if `MaxBlockDuration` is null, the metric send by a call to a
/// `DogStatsd` or `DogStatsdService` method will be dropped.
/// - If `MaxBlockDuration` is not null, the metric send by a call to a
/// `DogStatsd` or `DogStatsdService` method will block for at most
Expand All @@ -41,10 +46,5 @@ public class AdvancedStatsConfig
/// When this value is set at null, telemetry is disabled.
/// </summary>
public TimeSpan? TelemetryFlushInterval { get; set; } = TimeSpan.FromSeconds(10);

public AdvancedStatsConfig()
{
DurationBeforeSendingNotFullBuffer = TimeSpan.FromMilliseconds(100);
}
}
}
21 changes: 15 additions & 6 deletions src/StatsdClient/Bufferize/BufferBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace StatsdClient.Bufferize
/// <summary>
/// Append string values to a fixed size bytes buffer.
/// </summary>
class BufferBuilder
internal class BufferBuilder
{
readonly IBufferBuilderHandler _handler;
readonly byte[] _buffer;
readonly byte[] _separator;
readonly static Encoding _encoding = Encoding.UTF8;
private static readonly Encoding _encoding = Encoding.UTF8;
private readonly IBufferBuilderHandler _handler;
private readonly byte[] _buffer;
private readonly byte[] _separator;

public BufferBuilder(
IBufferBuilderHandler handler,
Expand All @@ -22,11 +22,14 @@ public BufferBuilder(
_handler = handler;
_separator = _encoding.GetBytes(separator);
if (_separator.Length >= _buffer.Length)
{
throw new ArgumentException("separator is greater or equal to the bufferCapacity");
}
}

public int Length { get; private set; }
public int Capacity { get { return _buffer.Length; } }

public int Capacity => _buffer.Length;

public static byte[] GetBytes(string message)
{
Expand All @@ -38,13 +41,19 @@ public bool Add(string value)
var byteCount = _encoding.GetByteCount(value);

if (byteCount > Capacity)
{
return false;
}

if (Length != 0)
{
byteCount += _separator.Length;
}

if (Length + byteCount > Capacity)
{
this.HandleBufferAndReset();
}

if (Length != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/StatsdClient/Bufferize/BufferBuilderHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace StatsdClient.Bufferize
{
/// <summary>
/// BufferBuilderHandler forwards metrics to StatsSender and update telemetry.
/// BufferBuilderHandler forwards metrics to StatsSender and update telemetry.
/// </summary>
internal class BufferBuilderHandler : IBufferBuilderHandler
{
Expand Down
2 changes: 1 addition & 1 deletion src/StatsdClient/Bufferize/IBufferBuilderHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace StatsdClient.Bufferize
{
interface IBufferBuilderHandler
internal interface IBufferBuilderHandler
{
void Handle(byte[] buffer, int length);
}
Expand Down
7 changes: 4 additions & 3 deletions src/StatsdClient/Bufferize/IStatsBufferizeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StatsdClient.Bufferize
/// IStatsBufferizeFactory is a factory for StatsBufferize.
/// It is used to test StatsBufferize.
/// </summary>
interface IStatsBufferizeFactory
internal interface IStatsBufferizeFactory
{
StatsBufferize CreateStatsBufferize(
Telemetry telemetry,
Expand All @@ -19,7 +19,8 @@ StatsBufferize CreateStatsBufferize(

StatsSender CreateUDPStatsSender(IPEndPoint endPoint);

StatsSender CreateUnixDomainSocketStatsSender(UnixEndPoint endPoint,
TimeSpan? udsBufferFullBlockDuration);
StatsSender CreateUnixDomainSocketStatsSender(
UnixEndPoint endPoint,
TimeSpan? udsBufferFullBlockDuration);
}
}
33 changes: 20 additions & 13 deletions src/StatsdClient/Bufferize/StatsBufferize.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using StatsdClient.Worker;

namespace StatsdClient.Bufferize
{
/// <summary>
/// StatsBufferize bufferizes metrics before sending them.
/// </summary>
class StatsBufferize : IStatsdUDP, IDisposable
internal class StatsBufferize : IStatsdUDP, IDisposable
{
readonly AsynchronousWorker<string> _worker;
readonly Telemetry _telemetry;
private readonly AsynchronousWorker<string> _worker;
private readonly Telemetry _telemetry;

public StatsBufferize(
Telemetry telemetry,
Expand All @@ -24,11 +24,12 @@ public StatsBufferize(
var handler = new WorkerHandler(bufferBuilder, maxIdleWaitBeforeSending);

// `handler` (and also `bufferBuilder`) do not need to be thread safe as long as workerMaxItemCount is 1.
this._worker = new AsynchronousWorker<string>(handler,
new Waiter(),
1,
workerMaxItemCount,
blockingQueueTimeout);
this._worker = new AsynchronousWorker<string>(
handler,
new Waiter(),
1,
workerMaxItemCount,
blockingQueueTimeout);
}

public void Send(string command)
Expand All @@ -49,11 +50,11 @@ public Task SendAsync(string command)
throw new NotSupportedException();
}

class WorkerHandler : IAsynchronousWorkerHandler<string>
private class WorkerHandler : IAsynchronousWorkerHandler<string>
{
readonly BufferBuilder _bufferBuilder;
readonly TimeSpan _maxIdleWaitBeforeSending;
System.Diagnostics.Stopwatch _stopwatch;
private readonly BufferBuilder _bufferBuilder;
private readonly TimeSpan _maxIdleWaitBeforeSending;
private System.Diagnostics.Stopwatch _stopwatch;

public WorkerHandler(BufferBuilder bufferBuilder, TimeSpan maxIdleWaitBeforeSending)
{
Expand All @@ -64,14 +65,19 @@ public WorkerHandler(BufferBuilder bufferBuilder, TimeSpan maxIdleWaitBeforeSend
public void OnNewValue(string metric)
{
if (!_bufferBuilder.Add(metric))
{
throw new InvalidOperationException($"The metric size exceeds the buffer capacity: {metric}");
}

_stopwatch = null;
}

public bool OnIdle()
{
if (_stopwatch == null)
{
_stopwatch = System.Diagnostics.Stopwatch.StartNew();
}

if (_stopwatch.ElapsedMilliseconds > _maxIdleWaitBeforeSending.TotalMilliseconds)
{
Expand All @@ -80,6 +86,7 @@ public bool OnIdle()
// No need to wait as sending the value takes time.
return false;
}

return true;
}

Expand Down
18 changes: 10 additions & 8 deletions src/StatsdClient/Bufferize/StatsBufferizeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace StatsdClient.Bufferize
{
class StatsBufferizeFactory : IStatsBufferizeFactory
internal class StatsBufferizeFactory : IStatsBufferizeFactory
{
public StatsBufferize CreateStatsBufferize(
Telemetry telemetry,
Expand All @@ -13,20 +13,22 @@ public StatsBufferize CreateStatsBufferize(
TimeSpan? blockingQueueTimeout,
TimeSpan maxIdleWaitBeforeSending)
{
return new StatsBufferize(telemetry,
bufferBuilder,
workerMaxItemCount,
blockingQueueTimeout,
maxIdleWaitBeforeSending);
return new StatsBufferize(
telemetry,
bufferBuilder,
workerMaxItemCount,
blockingQueueTimeout,
maxIdleWaitBeforeSending);
}

public StatsSender CreateUDPStatsSender(IPEndPoint endPoint)
{
return StatsSender.CreateUDPStatsSender(endPoint);
}

public StatsSender CreateUnixDomainSocketStatsSender(UnixEndPoint endPoint,
TimeSpan? udsBufferFullBlockDuration)
public StatsSender CreateUnixDomainSocketStatsSender(
UnixEndPoint endPoint,
TimeSpan? udsBufferFullBlockDuration)
{
return StatsSender.CreateUnixDomainSocketStatsSender(endPoint, udsBufferFullBlockDuration);
}
Expand Down
22 changes: 13 additions & 9 deletions src/StatsdClient/DogStatsdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ public class DogStatsdService : IDogStatsd, IDisposable
private string _prefix;
private StatsdConfig _config;

public ITelemetryCounters TelemetryCounters => _statsdData?.Telemetry;

public void Configure(StatsdConfig config)
{
if (_statsdBuilder == null)
{
throw new ObjectDisposedException(nameof(DogStatsdService));
}

if (config == null)
{
throw new ArgumentNullException("config");
}

if (_config != null)
{
throw new InvalidOperationException("Configuration for DogStatsdService already performed");
}

_config = config;
_prefix = config.Prefix;
Expand All @@ -35,7 +42,6 @@ public void Event(string title, string text, string alertType = null, string agg
_statsD?.Send(title, text, alertType, aggregationKey, sourceType, dateHappened, priority, hostname, tags);
}


public void Counter<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null)
{
_statsD?.Send<Statsd.Counting, T>(BuildNamespacedStatName(statName), value, sampleRate, tags);
Expand Down Expand Up @@ -111,6 +117,12 @@ public void ServiceCheck(string name, Status status, int? timestamp = null, stri
_statsD?.Send(name, (int)status, timestamp, hostname, tags, message);
}

public void Dispose()
{
_statsdData?.Dispose();
_statsdData = null;
}

private string BuildNamespacedStatName(string statName)
{
if (string.IsNullOrEmpty(_prefix))
Expand All @@ -120,13 +132,5 @@ private string BuildNamespacedStatName(string statName)

return _prefix + "." + statName;
}

public ITelemetryCounters TelemetryCounters => _statsdData?.Telemetry;

public void Dispose()
{
_statsdData?.Dispose();
_statsdData = null;
}
}
}
38 changes: 29 additions & 9 deletions src/StatsdClient/Dogstatsd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,38 @@ public enum Status
OK = 0,
WARNING = 1,
CRITICAL = 2,
UNKNOWN = 3
UNKNOWN = 3,
}

public static class DogStatsd
{
private static readonly DogStatsdService _dogStatsdService = new DogStatsdService();

public static ITelemetryCounters TelemetryCounters => _dogStatsdService.TelemetryCounters;

public static void Configure(StatsdConfig config) => _dogStatsdService.Configure(config);

public static void Event(string title, string text, string alertType = null, string aggregationKey = null,
string sourceType = null, int? dateHappened = null, string priority = null, string hostname = null,
public static void Event(
string title,
string text,
string alertType = null,
string aggregationKey = null,
string sourceType = null,
int? dateHappened = null,
string priority = null,
string hostname = null,
string[] tags = null)
=>
_dogStatsdService.Event(title: title, text: text, alertType: alertType, aggregationKey: aggregationKey, sourceType: sourceType, dateHappened: dateHappened, priority: priority,
hostname: hostname, tags: tags);
_dogStatsdService.Event(
title: title,
text: text,
alertType: alertType,
aggregationKey: aggregationKey,
sourceType: sourceType,
dateHappened: dateHappened,
priority: priority,
hostname: hostname,
tags: tags);

public static void Counter<T>(string statName, T value, double sampleRate = 1.0, string[] tags = null) =>
_dogStatsdService.Counter<T>(statName: statName, value: value, sampleRate: sampleRate, tags: tags);
Expand Down Expand Up @@ -60,15 +77,18 @@ public static void Time(Action action, string statName, double sampleRate = 1.0,
public static T Time<T>(Func<T> func, string statName, double sampleRate = 1.0, string[] tags = null) =>
_dogStatsdService.Time<T>(func: func, statName: statName, sampleRate: sampleRate, tags: tags);

public static void ServiceCheck(string name, Status status, int? timestamp = null, string hostname = null,
string[] tags = null, string message = null) =>
public static void ServiceCheck(
string name,
Status status,
int? timestamp = null,
string hostname = null,
string[] tags = null,
string message = null) =>
_dogStatsdService.ServiceCheck(name, status, timestamp, hostname, tags, message);

public static void Dispose()
{
_dogStatsdService.Dispose();
}

public static ITelemetryCounters TelemetryCounters => _dogStatsdService.TelemetryCounters;
}
}
Loading