Skip to content

Commit 2cbed30

Browse files
committed
fix(angular): fall back to element tagName when trace is not provided
The `trace` directive should typically be declared on components to validly trace the lifecycle (from `ngOnInit` to `ngAfterViewInit`, when child views are also rendered). If `trace` is mistakenly not provided, we fall back to `tagName`.
1 parent 48877a5 commit 2cbed30

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/angular/src/tracing.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AfterViewInit, OnDestroy, OnInit } from '@angular/core';
1+
import type { AfterViewInit, ElementRef, OnDestroy, OnInit } from '@angular/core';
22
import { Directive, Injectable, Input, NgModule } from '@angular/core';
33
import type { ActivatedRouteSnapshot, Event, RouterState } from '@angular/router';
44
// Duplicated import to work around a TypeScript bug where it'd complain that `Router` isn't imported as a type.
@@ -235,8 +235,6 @@ export class TraceService implements OnDestroy {
235235
}
236236
}
237237

238-
const UNKNOWN_COMPONENT = 'unknown';
239-
240238
/**
241239
* A directive that can be used to capture initialization lifecycle of the whole component.
242240
*/
@@ -246,13 +244,19 @@ export class TraceDirective implements OnInit, AfterViewInit {
246244

247245
private _tracingSpan?: Span;
248246

247+
public constructor(private readonly _host: ElementRef<HTMLElement>) {}
248+
249249
/**
250250
* Implementation of OnInit lifecycle method
251251
* @inheritdoc
252252
*/
253253
public ngOnInit(): void {
254254
if (!this.componentName) {
255-
this.componentName = UNKNOWN_COMPONENT;
255+
// Technically, the `trace` binding should always be provided.
256+
// However, if it is incorrectly declared on the element without a
257+
// value (e.g., `<app-component trace />`), we fall back to using `tagName`
258+
// (which is `APP-COMPONENT`).
259+
this.componentName = this._host.nativeElement.tagName.toLowerCase();
256260
}
257261

258262
if (getActiveSpan()) {

0 commit comments

Comments
 (0)