Skip to content

Commit baf79c5

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 baf79c5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/angular/src/tracing.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
2+
import { ElementRef } from '@angular/core';
13
import type { AfterViewInit, OnDestroy, OnInit } from '@angular/core';
24
import { Directive, Injectable, Input, NgModule } from '@angular/core';
35
import type { ActivatedRouteSnapshot, Event, RouterState } from '@angular/router';
@@ -235,8 +237,6 @@ export class TraceService implements OnDestroy {
235237
}
236238
}
237239

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

247247
private _tracingSpan?: Span;
248248

249+
public constructor(private readonly _host: ElementRef<HTMLElement>) {}
250+
249251
/**
250252
* Implementation of OnInit lifecycle method
251253
* @inheritdoc
252254
*/
253255
public ngOnInit(): void {
254256
if (!this.componentName) {
255-
this.componentName = UNKNOWN_COMPONENT;
257+
// Technically, the `trace` binding should always be provided.
258+
// However, if it is incorrectly declared on the element without a
259+
// value (e.g., `<app-component trace />`), we fall back to using `tagName`
260+
// (which is `APP-COMPONENT`).
261+
this.componentName = this._host.nativeElement.tagName.toLowerCase();
256262
}
257263

258264
if (getActiveSpan()) {

0 commit comments

Comments
 (0)