Skip to content

Commit

Permalink
fix(angular): fall back to element tagName when trace is not prov…
Browse files Browse the repository at this point in the history
…ided

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`.
  • Loading branch information
arturovt committed Dec 18, 2024
1 parent 48877a5 commit 2cbed30
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AfterViewInit, OnDestroy, OnInit } from '@angular/core';
import type { AfterViewInit, ElementRef, OnDestroy, OnInit } from '@angular/core';
import { Directive, Injectable, Input, NgModule } from '@angular/core';
import type { ActivatedRouteSnapshot, Event, RouterState } from '@angular/router';
// Duplicated import to work around a TypeScript bug where it'd complain that `Router` isn't imported as a type.
Expand Down Expand Up @@ -235,8 +235,6 @@ export class TraceService implements OnDestroy {
}
}

const UNKNOWN_COMPONENT = 'unknown';

/**
* A directive that can be used to capture initialization lifecycle of the whole component.
*/
Expand All @@ -246,13 +244,19 @@ export class TraceDirective implements OnInit, AfterViewInit {

private _tracingSpan?: Span;

public constructor(private readonly _host: ElementRef<HTMLElement>) {}

/**
* Implementation of OnInit lifecycle method
* @inheritdoc
*/
public ngOnInit(): void {
if (!this.componentName) {
this.componentName = UNKNOWN_COMPONENT;
// Technically, the `trace` binding should always be provided.
// However, if it is incorrectly declared on the element without a
// value (e.g., `<app-component trace />`), we fall back to using `tagName`
// (which is `APP-COMPONENT`).
this.componentName = this._host.nativeElement.tagName.toLowerCase();
}

if (getActiveSpan()) {
Expand Down

0 comments on commit 2cbed30

Please sign in to comment.