forked from toddanglin/nativescript-trace-raven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace-raven.ts
176 lines (161 loc) · 5.58 KB
/
trace-raven.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Raven = require("raven-js");
import * as http from "http";
import * as platform from "platform";
import * as trace from "trace";
import * as app from "application";
import * as utils from "utils/utils";
import { DeviceOrientation } from "ui/enums";
import { Page, ShownModallyData } from "ui/page";
import { EventData } from "data/observable";
let page = require("ui/page").Page; // Needed for global events
let appversion = require("nativescript-appversion");
let orientation = require('nativescript-orientation');
require("nativescript-globalevents");
export class TraceRaven {
private batteryPercent: number;
constructor(dsn: string, environment = "debug", enableAppBreadcrumbs = true) {
if (dsn === undefined || dsn === "") {
throw new Error("Sentry DSN string required to configure Raven TraceWriter");
}
this.initRaven(dsn, environment, enableAppBreadcrumbs);
}
public write(message: string, category: string, type?: number): void {
if (typeof(Raven) === "undefined") return; // Do not process if Raven plugin not loaded
// Sentry only recognizes 'info', 'warning' and 'error' ('error' is default)
let level = "error";
if (type === trace.messageType.log || type === trace.messageType.info) {
level = "info";
} else if (type === trace.messageType.warn) {
level = "warning"
}
// Add category as a tag for log
Raven.captureMessage(message, { level: level, tags: { trace_category: category } });
}
private initRaven(dsn: string, environment: string, enableAppBreadcrumbs: boolean) {
Raven
.config(dsn, {
logger: 'nativescript',
environment: environment,
serverName: platform.device.uuid,
tags: {
device_type: platform.device.deviceType,
device_lang: platform.device.language,
},
dataCallback: (data) => {
// Enrich with additional context
data.contexts = {
device: {
family: platform.device.manufacturer,
model: platform.device.model,
orientation: DeviceOrientation[orientation.getOrientation()],
battery_level: this.batteryPercent
},
os: {
name: platform.device.os,
version: platform.device.osVersion
},
runtime: {
name: 'nativescript',
version: global.__runtimeVersion
}
}
return data;
},
transport: (options) => {
let url = `${options.url}?sentry_version=${encodeURIComponent(options.auth.sentry_version)}` +
`&sentry_client=${encodeURIComponent(options.auth.sentry_client)}` +
`&sentry_key=${encodeURIComponent(options.auth.sentry_key)}`;
http.request({
method: "POST",
headers: {
"Content-Type": "application/json",
"Origin": "nativescript://"
},
url: url,
timeout: 2000,
content: JSON.stringify(options.data)
})
.then((result) => {
if (result.statusCode !== 200) {
throw new Error(`Unexpcted HTTP status code (${result.statusCode})`);
}
options.onSuccess();
})
.catch((err) => {
let msg = `Raven Transport Error: ${err}`;
console.warn(msg);
options.onFailure();
});
},
})
.install();
if (enableAppBreadcrumbs) {
this.initAutoCrumbs();
}
this.initAppVersion();
this.initBatteryStatus();
}
private initAutoCrumbs() {
// Loaded
page.on(Page.loadedEvent, (args: EventData) => {
let p = <Page>args.object;
Raven.captureBreadcrumb({
message: `Page loaded`,
category: "debug",
data: {
binding_context: p.bindingContext
},
level: "info"
});
});
// NavigatedTo
page.on(Page.navigatedToEvent, (args: EventData) => {
let p = <Page>args.object;
Raven.captureBreadcrumb({
message: `App navigated to new page`,
category: "navigation",
data: {
binding_context: p.bindingContext,
nav_context: p.navigationContext
},
level: "info"
})
});
//Shown Modally
page.on(Page.shownModallyEvent, (args: ShownModallyData) => {
let p = <Page>args.object;
Raven.captureBreadcrumb({
message: `Page shown modally`,
category: "navigation",
data: {
binding_context: p.bindingContext,
nav_context: args.context
},
level: "info"
})
});
}
private initAppVersion() {
// Add app version tag (async)
appversion.getVersionName()
.then((version) => {
Raven.setTagsContext({ app_version: version });
Raven.setRelease(version);
});
}
private initBatteryStatus() {
if (platform.isAndroid) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
(context: android.content.Context, intent: android.content.Intent) => {
let level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
let scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
this.batteryPercent = (level / scale) * 100.0;
});
} else {
app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
(notification: NSNotification) => {
this.batteryPercent = utils.ios.getter(UIDevice, UIDevice.currentDevice).batteryLevel * 100;
});
}
}
}