Skip to content

Commit 2c6c713

Browse files
authored
feat(instrumentationKey): ability to set instrumentationKey later
Optional instrumentationKey in forRoot Closes TrilonIO#24 Closes TrilonIO#17
2 parents dc17ea9 + ec047d8 commit 2c6c713

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/app-insight.service.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'rxjs/add/operator/filter';
55
import IAppInsights = Microsoft.ApplicationInsights.IAppInsights;
66

77
export class AppInsightsConfig implements Microsoft.ApplicationInsights.IConfig {
8+
instrumentationKeySetlater?: boolean;
89
instrumentationKey?: string;
910
endpointUrl?: string;
1011
emitLineDelimitedJson?: boolean;
@@ -41,14 +42,14 @@ export class AppInsightsService implements IAppInsights {
4142
context: Microsoft.ApplicationInsights.ITelemetryContext;
4243
queue: Array<() => void>;
4344
config: AppInsightsConfig;
44-
constructor(@Optional() _config: AppInsightsConfig, public router: Router) {
45+
constructor( @Optional() _config: AppInsightsConfig, public router: Router) {
4546
this.config = _config;
4647
}
4748

4849
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
4950
// trackEvent(name: string, properties?: {[string]:string}, measurements?: {[string]:number})
5051
// Log a user action or other occurrence.
51-
trackEvent(eventName: string, eventProperties?: {[name: string]: string}, metricProperty?: {[name: string]: number}) {
52+
trackEvent(eventName: string, eventProperties?: { [name: string]: string }, metricProperty?: { [name: string]: number }) {
5253
try {
5354
AppInsights.trackEvent(eventName, eventProperties, metricProperty);
5455
} catch (ex) {
@@ -75,7 +76,7 @@ export class AppInsightsService implements IAppInsights {
7576
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
7677
// trackPageView(name?: string, url?: string, properties?:{[string]:string}, measurements?: {[string]:number}, duration?: number)
7778
// Logs that a page or similar container was displayed to the user.
78-
trackPageView(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}, duration?: number) {
79+
trackPageView(name?: string, url?: string, properties?: { [name: string]: string }, measurements?: { [name: string]: number }, duration?: number) {
7980
try {
8081
AppInsights.trackPageView(name, url, properties, measurements, duration);
8182
} catch (ex) {
@@ -100,7 +101,7 @@ export class AppInsightsService implements IAppInsights {
100101
// stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object)
101102
// Stops the timer that was started by calling startTrackPage and sends the page view telemetry with the
102103
// specified properties and measurements. The duration of the page view will be the time between calling startTrackPage and stopTrackPage.
103-
stopTrackPage(name?: string, url?: string, properties?: {[name: string]: string}, measurements?: {[name: string]: number}) {
104+
stopTrackPage(name?: string, url?: string, properties?: { [name: string]: string }, measurements?: { [name: string]: number }) {
104105
try {
105106
AppInsights.stopTrackPage(name, url, properties, measurements);
106107
} catch (ex) {
@@ -112,7 +113,7 @@ export class AppInsightsService implements IAppInsights {
112113
// trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[string]:string})
113114
// Log a positive numeric value that is not associated with a specific event.
114115
// Typically used to send regular reports of performance indicators.
115-
trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: {[name: string]: string}) {
116+
trackMetric(name: string, average: number, sampleCount?: number, min?: number, max?: number, properties?: { [name: string]: string }) {
116117
try {
117118
AppInsights.trackMetric(name, average, sampleCount, min, max, properties);
118119
} catch (ex) {
@@ -123,8 +124,8 @@ export class AppInsightsService implements IAppInsights {
123124
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackexception
124125
// trackException(exception: Error, handledAt?: string, properties?: {[string]:string}, measurements?: {[string]:number}, severityLevel?: AI.SeverityLevel)
125126
// Log an exception you have caught. (Exceptions caught by the browser are also logged.)
126-
trackException(exception: Error, handledAt?: string, properties?: {[name: string]: string},
127-
measurements?: {[name: string]: number}, severityLevel?: AI.SeverityLevel) {
127+
trackException(exception: Error, handledAt?: string, properties?: { [name: string]: string },
128+
measurements?: { [name: string]: number }, severityLevel?: AI.SeverityLevel) {
128129
try {
129130
AppInsights.trackException(exception, handledAt, properties, measurements, severityLevel);
130131
} catch (ex) {
@@ -135,7 +136,7 @@ export class AppInsightsService implements IAppInsights {
135136
// https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#tracktrace
136137
// trackTrace(message: string, properties?: {[string]:string}, measurements?: {[string]:number})
137138
// Log a diagnostic event such as entering or leaving a method.
138-
trackTrace(message: string, properties?: {[name: string]: string}) {
139+
trackTrace(message: string, properties?: { [name: string]: string }) {
139140
try {
140141
AppInsights.trackTrace(message, properties);
141142
} catch (ex) {
@@ -195,28 +196,33 @@ export class AppInsightsService implements IAppInsights {
195196
}
196197

197198
public init(): void {
198-
if (this.config.instrumentationKey) {
199-
try {
200-
AppInsights.downloadAndSetup(this.config);
199+
if (this.config) {
200+
if (this.config.instrumentationKey) {
201+
try {
202+
AppInsights.downloadAndSetup(this.config);
201203

202-
if (!this.config.overrideTrackPageMetrics) {
203-
this.router.events.filter(event => event instanceof NavigationStart)
204+
if (!this.config.overrideTrackPageMetrics) {
205+
this.router.events.filter(event => event instanceof NavigationStart)
204206
.subscribe((event: NavigationStart) => {
205207
this.startTrackPage(event.url);
206208
});
207209

208-
this.router.events.filter(event => event instanceof NavigationEnd)
210+
this.router.events.filter(event => event instanceof NavigationEnd)
209211
.subscribe((event: NavigationEnd) => {
210212
this.stopTrackPage(event.url);
211213
});
214+
}
215+
} catch (ex) {
216+
console.warn('Angular application insights Error [downloadAndSetup]: ', ex);
217+
}
218+
} else {
219+
if (this.config.instrumentationKeySetlater) {
220+
console.warn('An instrumentationKey value is required to initialize AppInsightsService');
212221
}
213-
} catch (ex) {
214-
console.warn('Angular application insights Error [downloadAndSetup]: ', ex);
215222
}
216223
} else {
217-
console.warn('An instrumentationKey value is required to initialize AppInsightsService ');
224+
console.warn('You need forRoot on ApplicationInsightsModule, with or instrumentationKeySetlater or instrumentationKey set at least');
218225
}
219-
220226
}
221227
}
222228

0 commit comments

Comments
 (0)