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 1 commit
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
7 changes: 7 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,13 @@ class ViewController: UIViewController {

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

transaction.setMeasurement(name: "custom", value: 44, customUnit: "custom-unit")
transaction.setMeasurement(name: "none", value: 55)
transaction.setMeasurement(name: "duration", value: 1.23, durationUnit: .milliSecond)
transaction.setMeasurement(name: "information", value: 134.3, informationUnit: .gigabyte)
transaction.setMeasurement(name: "fraction", value: 9.3, fractionUnit: .percent)

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

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
Expand Down
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);
22 changes: 22 additions & 0 deletions Sources/Sentry/Public/SentrySpanProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ NS_SWIFT_NAME(Span)
*/
- (void)removeTagForKey:(NSString *)key NS_SWIFT_NAME(removeTag(key:));

- (void)setMeasurement:(NSString *)name
Copy link
Member

Choose a reason for hiding this comment

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

Should we add some comments briefly explaining which units can be used with which overload or do we expect people to simply drill down on the enum they can pass?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will add comments for sure. Thanks for pointing that out.

Copy link
Contributor

@marandaneto marandaneto Oct 7, 2022

Choose a reason for hiding this comment

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

So apparently ObjC has the same issues as Dart, on Dart, we've used a single enum for now (with all the units), and will refactor later once we are able to upgrade to the latest version.
Here we have an overload per unit type, not sure if it's good or bad, maybe just a method that takes a String would be enough.
Dart does not have overloads, so I could not do this either.
Maybe drop a message to folks that were part of the setMeasurement API and see if they are fine with that due to the restrictions of the language?

The trade-off here is:
Exposing many public APIs, maybe more in the future, so it's overwhelming for the user.

Or a single setMeasurement method that takes a unit of the type String which is open and users would need to learn all the unit types thru the docs.
@sl0thentr0py and @jan-auer specifically were driver and approver.

Copy link
Member

Choose a reason for hiding this comment

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

we said we wouldn't validate sdk side, so it's just a string in most sdks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, but the implementation should be UX friendly, at least for strongly typed languages, an enum would be ideal, but one can set a string as well.
On java one can do:

transaction.setMeasurement("metric1", 2, MeasurementUnit.Duration.DAY)
transaction.setMeasurement("metric1", 2, MeasurementUnit.Custom("myOwnUnit"))

Copy link
Member

Choose a reason for hiding this comment

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

yea that's fine, i think i added type hints to python as well

Copy link
Member

Choose a reason for hiding this comment

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

sry forgot to follow up earlier, was on phone.
I have these type hints in python
https://github.com/getsentry/sentry-python/blob/f71a8f45e780525e52fa5868f45bb876dcf0994b/sentry_sdk/_types.py#L53-L82

value:(NSNumber *)value NS_SWIFT_NAME(setMeasurement(name:value:));

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
durationUnit:(SentryDurationUnit)durationUnit
NS_SWIFT_NAME(setMeasurement(name:value:durationUnit:));

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
informationUnit:(SentryInformationUnit)informationUnit
NS_SWIFT_NAME(setMeasurement(name:value:informationUnit:));

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
fractionUnit:(SentryFractionUnit)fractionUnit
NS_SWIFT_NAME(setMeasurement(name:value:fractionUnit:));

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
customUnit:(NSString *)customUnit NS_SWIFT_NAME(setMeasurement(name:value:customUnit:));

/**
* Finishes the span by setting the end time.
*/
Expand Down
26 changes: 26 additions & 0 deletions Sources/Sentry/SentryNoOpSpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ - (void)removeTagForKey:(NSString *)key
{
}

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
durationUnit:(SentryDurationUnit)durationUnit
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
informationUnit:(SentryInformationUnit)informationUnit
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
fractionUnit:(SentryFractionUnit)fractionUnit
{
}

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value customUnit:(NSString *)customUnit
{
}

- (NSDictionary<NSString *, id> *)tags
{
return @{};
Expand Down
26 changes: 26 additions & 0 deletions Sources/Sentry/SentrySpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ - (void)removeTagForKey:(NSString *)key
}
}

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
durationUnit:(SentryDurationUnit)durationUnit
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
informationUnit:(SentryInformationUnit)informationUnit
{
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
fractionUnit:(SentryFractionUnit)fractionUnit
{
}

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value customUnit:(NSString *)customUnit
{
}

- (NSDictionary<NSString *, id> *)tags
{
@synchronized(_tags) {
Expand Down
106 changes: 106 additions & 0 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ @implementation SentryTracer {
SentryAppStartMeasurement *appStartMeasurement;
NSMutableDictionary<NSString *, id> *_tags;
NSMutableDictionary<NSString *, id> *_data;
NSMutableDictionary<NSString *, id> *_measurements;
dispatch_block_t _idleTimeoutBlock;
NSMutableArray<id<SentrySpan>> *_children;

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

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value
{
_measurements[name] = @{ @"value" : value, @"unit" : @"none" };
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
durationUnit:(SentryDurationUnit)durationUnit
{
_measurements[name] =
@{ @"value" : value, @"unit" : [self stringForDurationUnit:durationUnit] };
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
informationUnit:(SentryInformationUnit)informationUnit
{
_measurements[name] =
@{ @"value" : value, @"unit" : [self stringForInformationUnit:informationUnit] };
}

- (void)setMeasurement:(NSString *)name
value:(NSNumber *)value
fractionUnit:(SentryFractionUnit)fractionUnit
{
_measurements[name] =
@{ @"value" : value, @"unit" : [self stringForFractionUnit:fractionUnit] };
}

- (void)setMeasurement:(NSString *)name value:(NSNumber *)value customUnit:(NSString *)customUnit
{
_measurements[name] = @{ @"value" : value, @"unit" : customUnit };
}

- (NSString *)stringForDurationUnit:(SentryDurationUnit)duration
{
switch (duration) {
case kSentryDurationUnitNanoSecond:
return @"nanosecond";
case kSentryDurationUnitMicroSecond:
return @"microsecond";
case kSentryDurationUnitMilliSecond:
return @"millisecond";
case kSentryDurationUnitSecond:
return @"second";
case kSentryDurationUnitMinute:
return @"minute";
case kSentryDurationUnitHour:
return @"hour";
case kSentryDurationUnitDay:
return @"day";
case kSentryDurationUnitWeek:
return @"week";
}
}

- (NSString *)stringForInformationUnit:(SentryInformationUnit)informationUnit
{
switch (informationUnit) {
case kSentryInformationUnitBit:
return @"bit";
case kSentryInformationUnitByte:
return @"byte";
case kSentryInformationUnitKilobyte:
return @"kilobyte";
case kSentryInformationUnitKibibyte:
return @"kibibyte";
case kSentryInformationUnitMegabyte:
return @"megabyte";
case kSentryInformationUnitMebibyte:
return @"mebibyte";
case kSentryInformationUnitGigabyte:
return @"gigabyte";
case kSentryInformationUnitGibibyte:
return @"gibibyte";
case kSentryInformationUnitTerabyte:
return @"terabyte";
case kSentryInformationUnitTebibyte:
return @"tebibyte";
case kSentryInformationUnitPetabyte:
return @"petabyte";
case kSentryInformationUnitPebibyte:
return @"pebibyte";
case kSentryInformationUnitExabyte:
return @"exabyte";
case kSentryInformationUnitExbibyte:
return @"exbibyte";
}
}

- (NSString *)stringForFractionUnit:(SentryFractionUnit)fraction
{
switch (fraction) {
case kSentryFractionUnitRatio:
return @"ratio";
case kSentryFractionUnitPercent:
return @"percent";
}
}
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved

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

for (NSString *key in _measurements) {
[transaction setMeasurementValue:_measurements[key] forKey:key];
}
}

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