Skip to content

Commit

Permalink
Addressed PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarkowsky committed Aug 11, 2021
1 parent 4888929 commit 5a54a07
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
18 changes: 8 additions & 10 deletions Source/common/SNTMetricSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* Provides an abstraction for various metric systems that will be exported to
* monitoring systems via the MetricService. This is used store internal
* monitoring systems via the MetricService. This is used to store internal
* counters and metrics that can be exported to an external monitoring system.
*
* `SNTMetricSet` for storing and creating metrics and counters. This is
Expand All @@ -30,7 +30,7 @@
* * `SNTMetricGaugeInt64`
* * `SNTMetricGaugeDouble`
* * `SNTMetricString`
* * `SNTMetricBoolean`
* * `SNTMetricBool`
*/

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -41,17 +41,15 @@ typedef NS_ENUM(NSInteger, SNTMetricType) {
SNTMetricTypeConstantString = 2,
SNTMetricTypeConstantInt64 = 3,
SNTMetricTypeConstantDouble = 4,
SNTMetricTypeBoolGauge = 5,
SNTMetricTypeStringGauge = 6,
SNTMetricTypeInt64Gauge = 7,
SNTMetricTypeDoubleGauge = 8,
SNTMetricTypeGaugeBool = 5,
SNTMetricTypeGaugeString = 6,
SNTMetricTypeGaugeInt64 = 7,
SNTMetricTypeGaugeDouble = 8,
SNTMetricTypeCounter = 9,
};

/*
*/
@interface SNTMetric : NSObject
- (NSDictionary *)exportNSDictionary;
- (NSDictionary *)export;
@end

@interface SNTMetricCounter : SNTMetric
Expand Down Expand Up @@ -169,7 +167,7 @@ typedef NS_ENUM(NSInteger, SNTMetricType) {
- (void)registerCallback:(void (^)(void))callback;

/** Export creates an NSDictionary of the state of the metrics */
- (NSDictionary *)exportNSDictionary;
- (NSDictionary *)export;
@end

NS_ASSUME_NONNULL_END
30 changes: 15 additions & 15 deletions Source/common/SNTMetricSet.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,27 @@ - (NSDictionary *)encodeMetricValueForFieldValues:(NSArray<NSString *> *)fieldVa

switch (_type) {
case SNTMetricTypeConstantBool:
case SNTMetricTypeBoolGauge:
case SNTMetricTypeGaugeBool:
fieldDict[@"data"] = [NSNumber numberWithBool:[metricValue getBoolValue]];
break;
case SNTMetricTypeConstantInt64:
case SNTMetricTypeCounter:
case SNTMetricTypeInt64Gauge:
case SNTMetricTypeGaugeInt64:
fieldDict[@"data"] = [NSNumber numberWithLongLong:[metricValue getInt64Value]];
break;
case SNTMetricTypeConstantDouble:
case SNTMetricTypeDoubleGauge:
case SNTMetricTypeGaugeDouble:
fieldDict[@"data"] = [NSNumber numberWithDouble:[metricValue getDoubleValue]];
break;
case SNTMetricTypeConstantString:
case SNTMetricTypeStringGauge: fieldDict[@"data"] = [metricValue getStringValue]; break;
case SNTMetricTypeGaugeString: fieldDict[@"data"] = [metricValue getStringValue]; break;
default: break;
}
return [NSDictionary dictionaryWithDictionary:fieldDict];
return fieldDict;
}

- (NSDictionary *)exportNSDictionary {
NSMutableDictionary *metricDict = [[NSMutableDictionary alloc] init];
- (NSDictionary *)export {
NSMutableDictionary *metricDict = [NSMutableDictionary dictionaryWithCapacity:_fieldNames.count];
metricDict[@"type"] = [NSNumber numberWithInt:(int)_type];
metricDict[@"fields"] = [[NSMutableDictionary alloc] init];

Expand All @@ -275,7 +275,7 @@ - (NSDictionary *)exportNSDictionary {
metricDict[@"fields"][fieldName] = fieldVals;
}
}
return [NSDictionary dictionaryWithDictionary:metricDict];
return metricDict;
}
@end

Expand Down Expand Up @@ -321,7 +321,7 @@ - (instancetype)initWithName:(NSString *)name
return [super initWithName:name
fieldNames:fieldNames
helpText:helpText
type:SNTMetricTypeInt64Gauge];
type:SNTMetricTypeGaugeInt64];
}

- (void)set:(long long)value forFieldValues:(NSArray<NSString *> *)fieldValues {
Expand All @@ -348,7 +348,7 @@ - (instancetype)initWithName:(NSString *)name
return [super initWithName:name
fieldNames:fieldNames
helpText:text
type:SNTMetricTypeDoubleGauge];
type:SNTMetricTypeGaugeDouble];
}

- (void)set:(double)value forFieldValues:(NSArray<NSString *> *)fieldValues {
Expand All @@ -374,7 +374,7 @@ - (instancetype)initWithName:(NSString *)name
return [super initWithName:name
fieldNames:fieldNames
helpText:text
type:SNTMetricTypeStringGauge];
type:SNTMetricTypeGaugeString];
}

- (void)set:(NSString *)value forFieldValues:(NSArray<NSString *> *)fieldValues {
Expand All @@ -400,7 +400,7 @@ - (instancetype)initWithName:(NSString *)name
return [super initWithName:name
fieldNames:fieldNames
helpText:helpText
type:SNTMetricTypeBoolGauge];
type:SNTMetricTypeGaugeBool];
}

- (void)set:(BOOL)value forFieldValues:(NSArray<NSString *> *)fieldValues {
Expand Down Expand Up @@ -575,8 +575,8 @@ - (void)addConstantBooleanWithName:(NSString *)name
[self registerMetric:metric];
}

/** exportDictionary exports the SNTMetricSet as a dictinary for use */
- (NSDictionary *)exportNSDictionary {
/** Export current state of the SNTMetricSet as an NSDictionary. */
- (NSDictionary *)export {
NSDictionary *exported = nil;

// Invoke callbacks to ensure metrics are up to date.
Expand All @@ -593,7 +593,7 @@ - (NSDictionary *)exportNSDictionary {
// sort the metrics so we always get the same output.
for (id metricName in _metrics) {
SNTMetric *metric = [_metrics objectForKey:metricName];
exportDict[@"metrics"][metricName] = [metric exportNSDictionary];
exportDict[@"metrics"][metricName] = [metric export];
}

exported = [NSDictionary dictionaryWithDictionary:exportDict];
Expand Down
47 changes: 23 additions & 24 deletions Source/common/SNTMetricSetTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@interface SNTMetricCounterTest : XCTestCase
@end

@interface SNTMetricInt64GaugeTest : XCTestCase
@interface SNTMetricGaugeInt64Test : XCTestCase
@end

@interface SNTMetricDoubleGaugeTest : XCTestCase
Expand Down Expand Up @@ -70,7 +70,7 @@ - (void)testExportNSDictionary {
}
};

XCTAssertEqualObjects([c exportNSDictionary], expected);
XCTAssertEqualObjects([c export], expected);
}
@end

Expand All @@ -95,7 +95,7 @@ - (void)testExportNSDictionary {
XCTAssertNotNil(b);
[b set:true forFieldValues:@[]];
NSDictionary *expected = @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeBoolGauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeBool],
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -106,20 +106,20 @@ - (void)testExportNSDictionary {
}
};

NSDictionary *output = [b exportNSDictionary];
NSDictionary *output = [b export];
XCTAssertEqualObjects(output, expected);
}
@end

@implementation SNTMetricInt64GaugeTest
@implementation SNTMetricGaugeInt64Test
- (void)testSimpleGauge {
SNTMetricSet *metricSet = [[SNTMetricSet alloc] init];
SNTMetricInt64Gauge *g =
[metricSet int64GaugeWithName:@"/santa/rules"
fieldNames:@[ @"rule_type" ]
helpText:@"Count of rules broken out by rule type."];

XCTAssertNotNil(g, @"Expected returned SNTMetricInt64Gauge to not be nil");
XCTAssertNotNil(g, @"Expected returned SNTMetricGaugeInt64 to not be nil");
// set from zero
[g set:250 forFieldValues:@[ @"binary" ]];
XCTAssertEqual(250, [g getGaugeValueForFieldValues:@[ @"binary" ]]);
Expand All @@ -143,13 +143,13 @@ - (void)testExportNSDictionary {
fieldNames:@[ @"rule_type" ]
helpText:@"Count of rules broken out by rule type."];

XCTAssertNotNil(g, @"Expected returned SNTMetricInt64Gauge to not be nil");
XCTAssertNotNil(g, @"Expected returned SNTMetricGaugeInt64 to not be nil");
// set from zero
[g set:250 forFieldValues:@[ @"binary" ]];
XCTAssertEqual(250, [g getGaugeValueForFieldValues:@[ @"binary" ]]);

NSDictionary *expected = @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeInt64Gauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"fields" : @{
@"rule_type" : @[ @{
@"value" : @"binary",
Expand All @@ -160,7 +160,7 @@ - (void)testExportNSDictionary {
}
};

XCTAssertEqualObjects([g exportNSDictionary], expected);
XCTAssertEqualObjects([g export], expected);
}
@end

Expand Down Expand Up @@ -200,7 +200,7 @@ - (void)testExportNSDictionary {
[g set:(double)0.90 forFieldValues:@[ @"system" ]];

NSDictionary *expected = @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeDoubleGauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeDouble],
@"fields" : @{
@"mode" : @[
@{
Expand All @@ -218,7 +218,7 @@ - (void)testExportNSDictionary {
]
}
};
XCTAssertEqualObjects([g exportNSDictionary], expected);
XCTAssertEqualObjects([g export], expected);
}
@end

Expand All @@ -243,7 +243,7 @@ - (void)testExportNSDictionary {
[s set:@"testValue" forFieldValues:@[]];

NSDictionary *expected = @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeStringGauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeString],
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -254,7 +254,7 @@ - (void)testExportNSDictionary {
}
};

XCTAssertEqualObjects([s exportNSDictionary], expected);
XCTAssertEqualObjects([s export], expected);
}
@end

Expand All @@ -265,7 +265,7 @@ - (void)testRootLabels {

NSDictionary *expected = @{@"root_labels" : @{@"hostname" : @"localhost"}, @"metrics" : @{}};

NSDictionary *output = [metricSet exportNSDictionary];
NSDictionary *output = [metricSet export];
XCTAssertEqualObjects(output, expected);
}

Expand Down Expand Up @@ -300,7 +300,7 @@ - (void)testRegisterCallback {
}];

// ensure the callback is called.
[metricSet exportNSDictionary];
[metricSet export];

XCTAssertEqual([gauge getGaugeValueForFieldValues:@[]], 1);
}
Expand All @@ -325,7 +325,7 @@ - (void)testAddConstantBool {
}
};

XCTAssertEqualObjects([metricSet exportNSDictionary][@"metrics"], expected);
XCTAssertEqualObjects([metricSet export][@"metrics"], expected);
}

- (void)testAddConstantString {
Expand All @@ -341,7 +341,6 @@ - (void)testAddConstantString {
@"fields" : @{
@"" : @[ @{
@"value" : @"",

@"created" : [NSDate date],
@"last_updated" : [NSDate date],
@"data" : @"20210806.0.1"
Expand All @@ -350,7 +349,7 @@ - (void)testAddConstantString {
}
};

XCTAssertEqualObjects([metricSet exportNSDictionary][@"metrics"], expected);
XCTAssertEqualObjects([metricSet export][@"metrics"], expected);
}

- (void)testAddConstantInt {
Expand All @@ -373,7 +372,7 @@ - (void)testAddConstantInt {
}
};

XCTAssertEqualObjects([metricSet exportNSDictionary][@"metrics"], expected);
XCTAssertEqualObjects([metricSet export][@"metrics"], expected);
}

- (void)testExportNSDictionary {
Expand Down Expand Up @@ -416,7 +415,7 @@ - (void)testExportNSDictionary {
[metricSet int64GaugeWithName:@"/proc/memory/resident_size"
fieldNames:@[]
helpText:@"The resident set siz of this process."];

[metricSet registerCallback:^(void) {
[virtualMemoryGauge set:987654321 forFieldValues:@[]];
[residentMemoryGauge set:123456789 forFieldValues:@[]];
Expand Down Expand Up @@ -456,7 +455,7 @@ - (void)testExportNSDictionary {
},
},
@"/santa/rules" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeInt64Gauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"fields" : @{
@"rule_type" : @[
@{
Expand Down Expand Up @@ -497,7 +496,7 @@ - (void)testExportNSDictionary {
},
},
@"/proc/memory/virtual_size" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeInt64Gauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -508,7 +507,7 @@ - (void)testExportNSDictionary {
}
},
@"/proc/memory/resident_size" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeInt64Gauge],
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -521,7 +520,7 @@ - (void)testExportNSDictionary {
}
};

XCTAssertEqualObjects([metricSet exportNSDictionary], expected);
XCTAssertEqualObjects([metricSet export], expected);
}

@end

0 comments on commit 5a54a07

Please sign in to comment.