-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathapp.service.ts
113 lines (93 loc) · 3.01 KB
/
app.service.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
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { Cron, SchedulerRegistry } from '@nestjs/schedule';
import * as Sentry from '@sentry/nestjs';
import { SentryCron, SentryTraced } from '@sentry/nestjs';
import type { MonitorConfig } from '@sentry/types';
const monitorConfig: MonitorConfig = {
schedule: {
type: 'crontab',
value: '* * * * *',
},
};
@Injectable()
export class AppService {
constructor(private schedulerRegistry: SchedulerRegistry) {}
testTransaction() {
Sentry.startSpan({ name: 'test-span' }, () => {
Sentry.startSpan({ name: 'child-span' }, () => {});
});
}
testSpan() {
// span that should not be a child span of the middleware span
Sentry.startSpan({ name: 'test-controller-span' }, () => {});
}
testException(id: string) {
throw new Error(`This is an exception with id ${id}`);
}
testExpected400Exception(id: string) {
throw new HttpException(`This is an expected 400 exception with id ${id}`, HttpStatus.BAD_REQUEST);
}
testExpected500Exception(id: string) {
throw new HttpException(`This is an expected 500 exception with id ${id}`, HttpStatus.INTERNAL_SERVER_ERROR);
}
testExpectedRpcException(id: string) {
throw new RpcException(`This is an expected RPC exception with id ${id}`);
}
@SentryTraced('wait and return a string')
async wait() {
await new Promise(resolve => setTimeout(resolve, 500));
return 'test';
}
async testSpanDecoratorAsync() {
return await this.wait();
}
@SentryTraced('return a string')
getString(): { result: string } {
return { result: 'test' };
}
@SentryTraced('return the function name')
getFunctionName(): { result: string } {
return { result: this.getFunctionName.name };
}
async testSpanDecoratorSync() {
const returned = this.getString();
// Will fail if getString() is async, because returned will be a Promise<>
return returned.result;
}
/*
Actual cron schedule differs from schedule defined in config because Sentry
only supports minute granularity, but we don't want to wait (worst case) a
full minute for the tests to finish.
*/
@Cron('*/5 * * * * *', { name: 'test-cron-job' })
@SentryCron('test-cron-slug', monitorConfig)
async testCron() {
console.log('Test cron!');
}
/*
Actual cron schedule differs from schedule defined in config because Sentry
only supports minute granularity, but we don't want to wait (worst case) a
full minute for the tests to finish.
*/
@Cron('*/5 * * * * *', { name: 'test-cron-error' })
@SentryCron('test-cron-error-slug', monitorConfig)
async testCronError() {
throw new Error('Test error from cron job');
}
async killTestCron(job: string) {
this.schedulerRegistry.deleteCronJob(job);
}
use() {
console.log('Test use!');
}
transform() {
console.log('Test transform!');
}
intercept() {
console.log('Test intercept!');
}
canActivate() {
console.log('Test canActivate!');
}
}