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

fix: Use correct types for event context data #2910

Merged
merged 6 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion packages/apm/src/integrations/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ export class Tracing implements Integration {
Tracing._log(`[Tracing] Transaction: ${SpanStatus.Cancelled} since it maxed out maxTransactionDuration`);
if (event.contexts && event.contexts.trace) {
event.contexts.trace = {
...event.contexts.trace,
...(typeof event.contexts.trace === 'object' && event.contexts.trace),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we now do a type check 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.

Because unknown is more restrictive than any, and requires type check before it can be spread. On the other hand, it doesnt require unnecessary no-explcit-any linting rule.

Copy link
Contributor

Choose a reason for hiding this comment

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

event.contexts.trace has type Context, it must be an object, mustn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

status: SpanStatus.DeadlineExceeded,
};
event.tags = {
Expand Down
19 changes: 12 additions & 7 deletions packages/hub/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import {
Breadcrumb,
CaptureContext,
Context,
Contexts,
Event,
EventHint,
EventProcessor,
Expand Down Expand Up @@ -40,12 +42,10 @@ export class Scope implements ScopeInterface {
protected _tags: { [key: string]: string } = {};

/** Extra */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected _extra: { [key: string]: any } = {};
protected _extra: Extras = {};

/** Contexts */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected _contexts: { [key: string]: any } = {};
protected _contexts: Contexts = {};

/** Fingerprint */
protected _fingerprint?: string[];
Expand Down Expand Up @@ -185,9 +185,14 @@ export class Scope implements ScopeInterface {
/**
* @inheritDoc
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setContext(key: string, context: { [key: string]: any } | null): this {
this._contexts = { ...this._contexts, [key]: context };
public setContext(key: string, context: Context | null): this {
if (context === null) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this._contexts[key];
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm this is more than changing the types, would the previous code delete keys?! Was this a bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

From https://develop.sentry.dev/sdk/unified-api/
image

So now setting a context to null deletes it. Let's document this in the changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was a bug, fixed.

} else {
this._contexts = { ...this._contexts, [key]: context };
}

this._notifyScopeListeners();
return this;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Context = unknown;
kamilogorek marked this conversation as resolved.
Show resolved Hide resolved
export type Contexts = Record<string, Context>;
8 changes: 4 additions & 4 deletions packages/types/src/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Breadcrumb } from './breadcrumb';
import { Contexts } from './context';
import { Exception } from './exception';
import { Extras } from './extra';
import { Request } from './request';
import { CaptureContext } from './scope';
import { SdkInfo } from './sdkinfo';
Expand Down Expand Up @@ -31,11 +33,9 @@ export interface Event {
};
stacktrace?: Stacktrace;
breadcrumbs?: Breadcrumb[];
contexts?: {
[key: string]: Record<any, any>;
};
contexts?: Contexts;
tags?: { [key: string]: string };
extra?: { [key: string]: any };
extra?: Extras;
user?: User;
type?: EventType;
spans?: Span[];
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Breadcrumb, BreadcrumbHint } from './breadcrumb';
export { Client } from './client';
export { Context, Contexts } from './context';
export { Dsn, DsnComponents, DsnLike, DsnProtocol } from './dsn';
export { ExtendedError } from './error';
export { Event, EventHint } from './event';
Expand Down
7 changes: 4 additions & 3 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Breadcrumb } from './breadcrumb';
import { Context, Contexts } from './context';
import { EventProcessor } from './eventprocessor';
import { Extra, Extras } from './extra';
import { Severity } from './severity';
Expand All @@ -13,8 +14,8 @@ export type CaptureContext = Scope | Partial<ScopeContext> | ((scope: Scope) =>
export interface ScopeContext {
user: User;
level: Severity;
extra: { [key: string]: any };
contexts: { [key: string]: any };
rhcarvalho marked this conversation as resolved.
Show resolved Hide resolved
extra: Extras;
contexts: Contexts;
tags: { [key: string]: string };
fingerprint: string[];
}
Expand Down Expand Up @@ -82,7 +83,7 @@ export interface Scope {
* @param name of the context
* @param context Any kind of data. This data will be normailzed.
*/
setContext(name: string, context: { [key: string]: any } | null): this;
setContext(name: string, context: Context | null): this;

/**
* Sets the Span on the scope.
Expand Down