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

feat: Custom measurements API #2268

Merged
merged 18 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ class ViewController: UIViewController {

@IBAction func captureTransaction(_ sender: Any) {
let transaction = SentrySDK.startTransaction(name: "Some Transaction", operation: "Some Operation")

transaction.setMeasurement(SentryMeasurement(name: "duration", value: 44, unit: SentryUnitDuration.nanoseconds))
transaction.setMeasurement(SentryMeasurement(name: "duration-custom", value: 22, unit: SentryUnit(symbol: "custom")))

let span = transaction.startChild(operation: "user", description: "calls out")

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
Expand Down
16 changes: 16 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@
7BC8523724588115005A70F0 /* SentryDataCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC8523624588115005A70F0 /* SentryDataCategory.h */; };
7BC852392458830A005A70F0 /* SentryEnvelopeItemType.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC852382458830A005A70F0 /* SentryEnvelopeItemType.h */; settings = {ATTRIBUTES = (Public, ); }; };
7BC8523B2458849E005A70F0 /* SentryDataCategoryMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BC8523A2458849E005A70F0 /* SentryDataCategoryMapperTests.swift */; };
7BC9A20028F41016001E7C4C /* SentryMeasurement.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC9A1FF28F41016001E7C4C /* SentryMeasurement.h */; settings = {ATTRIBUTES = (Public, ); }; };
7BC9A20228F41350001E7C4C /* SentryUnit.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BC9A20128F41350001E7C4C /* SentryUnit.h */; settings = {ATTRIBUTES = (Public, ); }; };
7BC9A20428F4166D001E7C4C /* SentryMeasurement.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BC9A20328F4166D001E7C4C /* SentryMeasurement.m */; };
7BC9A20628F41781001E7C4C /* SentryUnit.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BC9A20528F41781001E7C4C /* SentryUnit.m */; };
7BCFA71627D0BB50008C662C /* SentryANRTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BCFA71527D0BB50008C662C /* SentryANRTracker.m */; };
7BCFBD672681C95000BC27D8 /* SentryScopeObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BCFBD662681C95000BC27D8 /* SentryScopeObserver.h */; };
7BCFBD6D2681D0A900BC27D8 /* SentryCrashScopeObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BCFBD6C2681D0A900BC27D8 /* SentryCrashScopeObserver.h */; };
Expand Down Expand Up @@ -1224,6 +1228,10 @@
7BC8523624588115005A70F0 /* SentryDataCategory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDataCategory.h; path = include/SentryDataCategory.h; sourceTree = "<group>"; };
7BC852382458830A005A70F0 /* SentryEnvelopeItemType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryEnvelopeItemType.h; path = Public/SentryEnvelopeItemType.h; sourceTree = "<group>"; };
7BC8523A2458849E005A70F0 /* SentryDataCategoryMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDataCategoryMapperTests.swift; sourceTree = "<group>"; };
7BC9A1FF28F41016001E7C4C /* SentryMeasurement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryMeasurement.h; path = Public/SentryMeasurement.h; sourceTree = "<group>"; };
7BC9A20128F41350001E7C4C /* SentryUnit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryUnit.h; path = Public/SentryUnit.h; sourceTree = "<group>"; };
7BC9A20328F4166D001E7C4C /* SentryMeasurement.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryMeasurement.m; sourceTree = "<group>"; };
7BC9A20528F41781001E7C4C /* SentryUnit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUnit.m; sourceTree = "<group>"; };
7BC9CD4326A99F660047518E /* SentryUIViewControllerSwizzling+Test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryUIViewControllerSwizzling+Test.h"; sourceTree = "<group>"; };
7BCFA71427D0BAB7008C662C /* SentryANRTracker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryANRTracker.h; path = include/SentryANRTracker.h; sourceTree = "<group>"; };
7BCFA71527D0BB50008C662C /* SentryANRTracker.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryANRTracker.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1612,6 +1620,10 @@
7BD4BD4427EB29F50071F4FF /* SentryClientReport.m */,
7BD4BD4627EB2A3D0071F4FF /* SentryDiscardedEvent.h */,
7BD4BD4827EB2A5D0071F4FF /* SentryDiscardedEvent.m */,
7BC9A1FF28F41016001E7C4C /* SentryMeasurement.h */,
7BC9A20328F4166D001E7C4C /* SentryMeasurement.m */,
7BC9A20128F41350001E7C4C /* SentryUnit.h */,
7BC9A20528F41781001E7C4C /* SentryUnit.m */,
);
name = Protocol;
sourceTree = "<group>";
Expand Down Expand Up @@ -2895,6 +2907,7 @@
0A2D8DA8289BC905008720F6 /* SentryViewHierarchy.h in Headers */,
8EAE980C261E9F530073B6B3 /* SentryUIViewControllerPerformanceTracker.h in Headers */,
63FE717D20DA4C1100CDBAE8 /* SentryCrashCachedData.h in Headers */,
7BC9A20028F41016001E7C4C /* SentryMeasurement.h in Headers */,
7BBC826D25DFCFDE005F1ED8 /* SentryInAppLogic.h in Headers */,
03BCC38A27E1BF49003232C7 /* SentryTime.h in Headers */,
7B0A54222521C21E00A71716 /* SentryFrameRemover.h in Headers */,
Expand Down Expand Up @@ -2987,6 +3000,7 @@
7DC27EC523997EB7006998B5 /* SentryAutoBreadcrumbTrackingIntegration.h in Headers */,
63FE707F20DA4C1000CDBAE8 /* SentryCrashVarArgs.h in Headers */,
03F84D2627DD414C008FE43F /* SentryThreadMetadataCache.hpp in Headers */,
7BC9A20228F41350001E7C4C /* SentryUnit.h in Headers */,
7BB654FB253DC14A00887E87 /* SentryUserFeedback.h in Headers */,
7B8ECBFA26498907005FE2EF /* SentryAppStateManager.h in Headers */,
639FCFA01EBC804600778193 /* SentryException.h in Headers */,
Expand Down Expand Up @@ -3414,6 +3428,7 @@
8EAE9806261E87120073B6B3 /* SentryUIViewControllerPerformanceTracker.m in Sources */,
D88817D826D7149100BF2251 /* SentryTraceContext.m in Sources */,
8EBF870926140D37001A6853 /* SentryPerformanceTracker.m in Sources */,
7BC9A20428F4166D001E7C4C /* SentryMeasurement.m in Sources */,
D859696B27BECD8F0036A46E /* SentryCoreDataTrackingIntegration.m in Sources */,
7BD86EC7264A641D005439DB /* SentrySysctl.m in Sources */,
D859697327BECDD20036A46E /* SentryCoreDataSwizzling.m in Sources */,
Expand All @@ -3430,6 +3445,7 @@
0356A571288B4612008BF593 /* SentryProfilesSampler.m in Sources */,
63FE710B20DA4C1000CDBAE8 /* SentryCrashMach.c in Sources */,
63FE707720DA4C1000CDBAE8 /* Container+SentryDeepSearch.m in Sources */,
7BC9A20628F41781001E7C4C /* SentryUnit.m in Sources */,
63FE71A020DA4C1100CDBAE8 /* SentryCrashInstallation.m in Sources */,
63FE713520DA4C1100CDBAE8 /* SentryCrashMemory.c in Sources */,
63FE714520DA4C1100CDBAE8 /* SentryCrashObjC.c in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/Public/Sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ FOUNDATION_EXPORT const unsigned char SentryVersionString[];
#import "SentryHub.h"
#import "SentryId.h"
#import "SentryIntegrationProtocol.h"
#import "SentryMeasurement.h"
#import "SentryMechanism.h"
#import "SentryMechanismMeta.h"
#import "SentryMessage.h"
Expand All @@ -46,5 +47,6 @@ FOUNDATION_EXPORT const unsigned char SentryVersionString[];
#import "SentryThread.h"
#import "SentryTraceHeader.h"
#import "SentryTransactionContext.h"
#import "SentryUnit.h"
#import "SentryUser.h"
#import "SentryUserFeedback.h"
81 changes: 81 additions & 0 deletions Sources/Sentry/Public/SentryDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,84 @@ typedef NS_ENUM(NSInteger, SentryTransactionNameSource) {
kSentryTransactionNameSourceComponent,
kSentryTransactionNameSourceTask
};

/**
* A time duration.
*/
typedef NS_ENUM(NSInteger, SentryDurationUnit) {
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
/** Nanosecond (`"nanosecond"`), 10^-9 seconds. */
kSentryDurationUnitNanoSecond = 0,
/** Microsecond (`"microsecond"`), 10^-6 seconds. */
kSentryDurationUnitMicroSecond,
/** Millisecond (`"millisecond"`), 10^-3 seconds. */
kSentryDurationUnitMilliSecond,
/** Full second (`"second"`). */
kSentryDurationUnitSecond,
/** Minute (`"minute"`), 60 seconds. */
kSentryDurationUnitMinute,
/** Hour (`"hour"`), 3600 seconds. */
kSentryDurationUnitHour,
/** Day (`"day"`), 86,400 seconds. */
kSentryDurationUnitDay,
/** Week (`"week"`), 604,800 seconds. */
kSentryDurationUnitWeek
} NS_SWIFT_NAME(DurationUnit);

/**
* Size of information derived from bytes.
*/
typedef NS_ENUM(NSInteger, SentryInformationUnit) {
/** Bit (`"bit"`), corresponding to 1/8 of a byte. */
kSentryInformationUnitBit,

/** Byte (`"byte"`). */
kSentryInformationUnitByte,

/** Kilobyte (`"kilobyte"`), 10^3 bytes. */
kSentryInformationUnitKilobyte,

/** Kibibyte (`"kibibyte"`), 2^10 bytes. */
kSentryInformationUnitKibibyte,

/** Megabyte (`"megabyte"`), 10^6 bytes. */
kSentryInformationUnitMegabyte,

/** Mebibyte (`"mebibyte"`), 2^20 bytes. */
kSentryInformationUnitMebibyte,

/** Gigabyte (`"gigabyte"`), 10^9 bytes. */
kSentryInformationUnitGigabyte,

/** Gibibyte (`"gibibyte"`), 2^30 bytes. */
kSentryInformationUnitGibibyte,

/** Terabyte (`"terabyte"`), 10^12 bytes. */
kSentryInformationUnitTerabyte,

/** Tebibyte (`"tebibyte"`), 2^40 bytes. */
kSentryInformationUnitTebibyte,

/** Petabyte (`"petabyte"`), 10^15 bytes. */
kSentryInformationUnitPetabyte,

/** Pebibyte (`"pebibyte"`), 2^50 bytes. */
kSentryInformationUnitPebibyte,

/** Exabyte (`"exabyte"`), 10^18 bytes. */
kSentryInformationUnitExabyte,

/** Exbibyte (`"exbibyte"`), 2^60 bytes. */
kSentryInformationUnitExbibyte
} NS_SWIFT_NAME(InformationUnit);

/**
* Fractions such as percentages.
*/
typedef NS_ENUM(NSInteger, SentryFractionUnit) {

/** Floating point fraction of `1`. */
kSentryFractionUnitRatio = 0,

/** Ratio expressed as a fraction of `100`. `100%` equals a ratio of `1.0`. */
kSentryFractionUnitPercent
} NS_SWIFT_NAME(FractionUnit);
18 changes: 18 additions & 0 deletions Sources/Sentry/Public/SentryMeasurement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import "SentryDefines.h"
#import "SentrySerializable.h"
#import "SentryUnit.h"

NS_ASSUME_NONNULL_BEGIN

@interface SentryMeasurement<UnitType : SentryUnit *> : NSObject <SentrySerializable>
SENTRY_NO_INIT

- (instancetype)initWithName:(NSString *)name value:(NSNumber *)value unit:(UnitType)unit;

@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy, readonly) NSNumber *value;
@property (readonly, copy) UnitType unit;

@end

NS_ASSUME_NONNULL_END
4 changes: 3 additions & 1 deletion Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

NS_ASSUME_NONNULL_BEGIN

@class SentryDsn, SentrySdkInfo;
@class SentryDsn, SentrySdkInfo, SentryMeasurement;

NS_SWIFT_NAME(Options)
@interface SentryOptions : NSObject
Expand Down Expand Up @@ -196,6 +196,8 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, assign) BOOL enableAutoPerformanceTracking;

- (void)setMeasurement:(SentryMeasurement *)measurement;

#if SENTRY_HAS_UIKIT
/**
* When enabled, the SDK tracks performance for UIViewController subclasses. The default is
Expand Down
4 changes: 3 additions & 1 deletion Sources/Sentry/Public/SentrySpanProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

NS_ASSUME_NONNULL_BEGIN

@class SentrySpanId, SentryId, SentryTraceHeader;
@class SentrySpanId, SentryId, SentryTraceHeader, SentryMeasurement;

NS_SWIFT_NAME(Span)
@protocol SentrySpan <SentrySerializable>
Expand Down Expand Up @@ -87,6 +87,8 @@ NS_SWIFT_NAME(Span)
*/
- (void)removeTagForKey:(NSString *)key NS_SWIFT_NAME(removeTag(key:));

- (void)setMeasurement:(SentryMeasurement *)measurement;

/**
* Finishes the span by setting the end time.
*/
Expand Down
47 changes: 47 additions & 0 deletions Sources/Sentry/Public/SentryUnit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#import "SentryDefines.h"

NS_ASSUME_NONNULL_BEGIN

@interface SentryUnit : NSObject <NSCopying>
SENTRY_NO_INIT

- (instancetype)initWithSymbol:(NSString *)symbol;

@property (readonly, copy) NSString *symbol;

@end

@interface SentryUnitDuration : SentryUnit
SENTRY_NO_INIT

/** Nanosecond (`"nanosecond"`), 10^-9 seconds. */
@property (class, readonly, copy) SentryUnitDuration *nanoseconds;

// ...

@end

@interface SentryUnitInformation : SentryUnit
SENTRY_NO_INIT

/** Bit (`"bit"`), corresponding to 1/8 of a byte. */
@property (class, readonly, copy) SentryUnitInformation *bit;

// ...

@end

@interface SentryUnitFraction : SentryUnit
SENTRY_NO_INIT

/** Floating point fraction of `1`. */
@property (class, readonly, copy) SentryUnitFraction *ratio;

/** Ratio expressed as a fraction of `100`. `100%` equals a ratio of `1.0`. */
@property (class, readonly, copy) SentryUnitFraction *percent;

// ...

@end

NS_ASSUME_NONNULL_END
24 changes: 24 additions & 0 deletions Sources/Sentry/SentryMeasurement.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#import "SentryMeasurement.h"

NS_ASSUME_NONNULL_BEGIN

@implementation SentryMeasurement

- (instancetype)initWithName:(NSString *)name value:(NSNumber *)value unit:(SentryUnit *)unit
{
if (self = [super init]) {
_name = name;
_value = value;
_unit = unit;
}
return self;
}

- (NSDictionary<NSString *, id> *)serialize
{
return @{ @"name" : self.name, @"value" : self.value, @"unit" : self.unit.symbol };
}

@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryNoOpSpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ - (void)removeTagForKey:(NSString *)key
{
}

- (void)setMeasurement:(SentryMeasurement *)measurement
{
}

- (NSDictionary<NSString *, id> *)tags
{
return @{};
Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

@implementation SentryOptions

- (void)setMeasurement:(SentryMeasurement *)measurement
{
}

+ (NSArray<NSString *> *)defaultIntegrations
{
return @[
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentrySpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "NSDictionary+SentrySanitize.h"
#import "SentryCurrentDate.h"
#import "SentryLog.h"
#import "SentryMeasurement.h"
#import "SentryNoOpSpan.h"
#import "SentryTraceHeader.h"
#import "SentryTracer.h"
Expand Down Expand Up @@ -89,6 +90,10 @@ - (void)removeTagForKey:(NSString *)key
}
}

- (void)setMeasurement:(SentryMeasurement *)measurement
{
}

- (NSDictionary<NSString *, id> *)tags
{
@synchronized(_tags) {
Expand Down
14 changes: 14 additions & 0 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "SentryTransactionContext.h"
#import "SentryUIViewControllerPerformanceTracker.h"
#import <SentryDispatchQueueWrapper.h>
#import <SentryMeasurement.h>
#import <SentryScreenFrames.h>
#import <SentrySpanOperations.h>

Expand Down Expand Up @@ -61,6 +62,7 @@ @implementation SentryTracer {
SentryAppStartMeasurement *appStartMeasurement;
NSMutableDictionary<NSString *, id> *_tags;
NSMutableDictionary<NSString *, id> *_data;
NSMutableArray<SentryMeasurement *> *_measurements;
dispatch_block_t _idleTimeoutBlock;
NSMutableArray<id<SentrySpan>> *_children;

Expand Down Expand Up @@ -160,6 +162,7 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti
_waitForChildren = waitForChildren;
_tags = [[NSMutableDictionary alloc] init];
_data = [[NSMutableDictionary alloc] init];
_measurements = [[NSMutableArray alloc] init];
self.finishStatus = kSentrySpanStatusUndefined;
self.idleTimeout = idleTimeout;
self.dispatchQueueWrapper = dispatchQueueWrapper;
Expand Down Expand Up @@ -395,6 +398,11 @@ - (void)removeTagForKey:(NSString *)key
}
}

- (void)setMeasurement:(SentryMeasurement *)measurement
{
[_measurements addObject:measurement];
}

- (SentryTraceHeader *)toTraceHeader
{
return [self.rootSpan toTraceHeader];
Expand Down Expand Up @@ -727,6 +735,12 @@ - (void)addMeasurements:(SentryTransaction *)transaction
}
}
#endif

for (SentryMeasurement *measurement in _measurements) {
[transaction
setMeasurementValue:@{ @"value" : measurement.value, @"unit" : measurement.unit.symbol }
forKey:measurement.name];
}
}

- (id<SentrySpan>)buildSpan:(SentrySpanId *)parentId
Expand Down
Loading