Skip to content

Flesh out the log event and log writer classes #2175

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

Merged
merged 8 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/** A struct to hold data pertaining to a snapshot in time. */
typedef struct {
/** The current time in millis. */
int64_t timeMillis;

/** The device uptime in millis. */
int64_t uptimeMillis;

/** The timezone offset in millis. */
int64_t timezoneOffsetMillis;
} GDLLogClockSnapshot;

/** This class manages the device clock and produces snapshots of the current time. */
@interface GDLLogClock : NSObject

// TODO(mikehaney24): - (GDLLogClockSnapshot)snapshot;

@end

NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogClock.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "GDLLogClock.h"

@implementation GDLLogClock

@end
48 changes: 48 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,54 @@

#import <Foundation/Foundation.h>

#import "GDLLogClock.h"
#import "GDLLogProto.h"

NS_ASSUME_NONNULL_BEGIN

/** The different possible log quality of service specifiers. High values indicate high priority. */
typedef NS_ENUM(NSInteger, GDLLogQoS) {
/** The QoS tier wasn't set, and won't ever be sent. */
GDLLogQoSUnknown = 0,

/** This log is internal telemetry data that should not be sent on its own if possible. */
GDLLogQoSTelemetry = 1,

/** This log should be sent, but in a batch only roughly once per day. */
GDLLogQoSDaily = 2,

/** This log should be sent when requested by the uploader. */
GDLLogQosDefault = 3,

/** This log should be sent immediately along with any other data that can be batched. */
GDLLogQoSFast = 4
};

@interface GDLLogEvent : NSObject

/** The log map identifier, to allow backends to map the extension property to a proto. */
@property(readonly, nonatomic) NSString *logMapID;

/** The log object itself, encapsulated in the transport of your choice, as long as it implements
* the GDLLogProto protocol. */
@property(nonatomic) id<GDLLogProto> extension;

/** The quality of service tier this log belongs to. */
@property(nonatomic) GDLLogQoS qosTier;

/** The clock snapshot at the time of logging. */
@property(nonatomic) GDLLogClockSnapshot clockSnapshot;

// Please use the designated initializer.
- (instancetype)init NS_UNAVAILABLE;

/** Initializes an instance using the given logMapID.
*
* @param logMapID The log map identifier.
* @return An instance of this class.
*/
- (instancetype)initWithLogMapID:(NSString *)logMapID NS_DESIGNATED_INITIALIZER;

@end

NS_ASSUME_NONNULL_END
9 changes: 9 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@

@implementation GDLLogEvent

- (instancetype)initWithLogMapID:(NSString *)logMapID {
NSAssert(logMapID.length > 0, @"Please give a valid log map ID");
self = [super init];
if (self) {
_logMapID = logMapID;
}
return self;
}

@end
30 changes: 30 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogProto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

/** This protocol defines the common interface that log protos should implement regardless of the
* underlying transport technology (protobuf, nanopb, etc).
*/
@protocol GDLLogProto <NSObject>

/** Returns the serialized proto bytes of the implementing log proto.
*
* @return the serialized proto bytes of the implementing log proto.
*/
- (NSData *)protoBytes;

@end
18 changes: 18 additions & 0 deletions GoogleDataLogger/GoogleDataLogger/Classes/GDLLogWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@

#import <Foundation/Foundation.h>

@class GDLLogEvent;

@protocol GDLLogTransformer;

/** Manages the writing and log-time transforming of logs. */
@interface GDLLogWriter : NSObject

/** Instantiates or returns the log writer singleton.
*
* @return The singleton instance of the log writer.
*/
+ (instancetype)sharedInstance;

/** Writes the result of applying the given transformers' -transform method on the given log.
*
* @param log The log to apply transformers on.
* @param logTransformers The list of transformers to apply.
*/
- (void)writeLog:(GDLLogEvent *)log
afterApplyingTransformers:(nullable NSArray<id<GDLLogTransformer>> *)logTransformers;

@end
41 changes: 40 additions & 1 deletion GoogleDataLogger/GoogleDataLogger/Classes/GDLLogWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,46 @@
*/

#import "GDLLogWriter.h"
#import "GDLLogWriter_Private.h"

@implementation GDLLogWriter : NSObject
#import <GoogleDataLogger/GDLLogTransformer.h>

#import "GDLLogStorage.h"

@implementation GDLLogWriter

// This class doesn't have to be a singleton, but allocating an instance for every logger could be
// wasteful.
+ (instancetype)sharedInstance {
static GDLLogWriter *logWriter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
logWriter = [[self alloc] init];
});
return logWriter;
}

- (instancetype)init {
self = [super init];
if (self) {
_logWritingQueue = dispatch_queue_create("com.google.GDLLogWriter", DISPATCH_QUEUE_SERIAL);
}
return self;
}

- (void)writeLog:(GDLLogEvent *)log
afterApplyingTransformers:(NSArray<id<GDLLogTransformer>> *)logTransformers {
NSAssert(log, @"You can't write a nil log");
dispatch_async(_logWritingQueue, ^{
GDLLogEvent *transformedLog = log;
for (id<GDLLogTransformer> transformer in logTransformers) {
transformedLog = [transformer transform:transformedLog];
if (!transformedLog) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is WAI, because a valid transformation is to nil-out the log. For example: when sampling is applied and this log should be dropped.

}
}
// TODO(mikehaney24): [[GDLLogStorage sharedInstance] storeLog:transformedLog];
});
}

@end
2 changes: 1 addition & 1 deletion GoogleDataLogger/GoogleDataLogger/Classes/GDLLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (void)logDataEvent:(GDLLogEvent *)logEvent {
}

- (GDLLogEvent *)newEvent {
return [[GDLLogEvent alloc] init];
return [[GDLLogEvent alloc] initWithLogMapID:_logMapID];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "GDLLogWriter.h"

@interface GDLLogWriter ()

/** The queue on which all work will occur. */
@property(nonatomic) dispatch_queue_t logWritingQueue;

@end
3 changes: 2 additions & 1 deletion GoogleDataLogger/Tests/GDLLogEventTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ @interface GDLLogEventTest : XCTestCase
@implementation GDLLogEventTest

- (void)testInit {
XCTAssertNotNil([[GDLLogEvent alloc] init]);
XCTAssertNotNil([[GDLLogEvent alloc] initWithLogMapID:@"1"]);
XCTAssertThrows([[GDLLogEvent alloc] initWithLogMapID:@""]);
}

@end
41 changes: 41 additions & 0 deletions GoogleDataLogger/Tests/GDLLogWriterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@

#import <XCTest/XCTest.h>

#import <GoogleDataLogger/GDLLogTransformer.h>

#import "GDLLogEvent.h"
#import "GDLLogWriter.h"
#import "GDLLogWriter_Private.h"

@interface GDLLogWriterTestNilingTransformer : NSObject <GDLLogTransformer>

@end

@implementation GDLLogWriterTestNilingTransformer

- (GDLLogEvent *)transform:(GDLLogEvent *)logEvent {
return nil;
}

@end

@interface GDLLogWriterTest : XCTestCase

Expand All @@ -29,4 +45,29 @@ - (void)testInit {
XCTAssertNotNil([[GDLLogWriter alloc] init]);
}

/** Tests the pointer equality of result of the -sharedInstance method. */
- (void)testSharedInstance {
XCTAssertEqual([GDLLogWriter sharedInstance], [GDLLogWriter sharedInstance]);
}

- (void)testWriteLogWithoutTransformers {
GDLLogWriter *writer = [GDLLogWriter sharedInstance];
GDLLogEvent *log = [[GDLLogEvent alloc] initWithLogMapID:@"1"];
XCTAssertNoThrow([writer writeLog:log afterApplyingTransformers:nil]);
dispatch_sync(writer.logWritingQueue, ^{
// TODO(mikehaney24): Assert that storage contains the log.
});
}

- (void)testWriteLogWithTransformersThatNilTheLog {
GDLLogWriter *writer = [GDLLogWriter sharedInstance];
GDLLogEvent *log = [[GDLLogEvent alloc] initWithLogMapID:@"2"];
NSArray<id<GDLLogTransformer>> *transformers =
@[ [[GDLLogWriterTestNilingTransformer alloc] init] ];
XCTAssertNoThrow([writer writeLog:log afterApplyingTransformers:transformers]);
dispatch_sync(writer.logWritingQueue, ^{
// TODO(mikehaney24): Assert that storage does not contain the log.
});
}

@end