Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use this with ng standalone? #209

Open
obabac opened this issue Aug 29, 2024 · 1 comment
Open

How can I use this with ng standalone? #209

obabac opened this issue Aug 29, 2024 · 1 comment

Comments

@obabac
Copy link

obabac commented Aug 29, 2024

What I did:

  1. Install the Necessary Packages
npm install @jufab/opentelemetry-angular-interceptor @opentelemetry/api
  1. Create a Custom Module for OpenTelemetry
    Standalone components don't directly support ModuleWithProviders types in the imports array. To work around this, create a separate Angular module to handle the OpenTelemetry configuration:
import { NgModule } from '@angular/core';
import {
    CompositePropagatorModule, OpenTelemetryConfig,
    OpenTelemetryInterceptorModule,
    OtelColExporterModule
} from "@jufab/opentelemetry-angular-interceptor";

const otelConfig: OpenTelemetryConfig = {
    commonConfig: {
        console: true,
        production: false,
        serviceName: 'demo-ui',
        resourceAttributes: {
            'service.name': 'Demo-UI',
        },
        logBody: true,
        probabilitySampler: '1',
        logLevel: DiagLogLevel.ALL,
    },
    otelcolConfig: {
        url: 'http://localhost:5080/api/default/traces',
        timeoutMillis: 10000,
        headers: {
            'Authorization': 'Basic dd9vdEBleGFtcGxlLmNvbTpDb21wbGV4cGFzcyMxMjM=',
        }
    }
};

@NgModule({
    imports: [
        OpenTelemetryInterceptorModule.forRoot(otelConfig),
        OtelColExporterModule,
        CompositePropagatorModule,
    ],
})
export class OpenTelemetryModule {
    constructor() {
        console.log('OpenTelemetryModule initialized');
    }
}
  1. Import the Module in Your Standalone Component
    In your standalone component, import this module:
import { Component } from '@angular/core';
import { IonApp, IonRouterOutlet } from '@ionic/angular/standalone';
import { OpenTelemetryModule } from './open-telemetry.module';

@Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    standalone: true,
    imports: [
        IonApp, IonRouterOutlet,
        OpenTelemetryModule  // Import the custom OpenTelemetry module here
    ]
})
export class AppComponent {
    constructor() {
        console.log('AppComponent initialized with OpenTelemetry');
    }
}

This approach doesn't work for me. It does not intercept my requests neither does it send any trace. The modules seems to get initialized (see the console log) though. Any help would be appreciated

@obabac
Copy link
Author

obabac commented Aug 29, 2024

I found a solution. No NgModule needed

Add this to your app.config.ts:

            provideAnimations(),
            provideRouter(routes, withComponentInputBinding()),
            provideHttpClient(withInterceptorsFromDi(), withFetch()),
...
            importProvidersFrom(OpenTelemetryInterceptorModule.forRoot(config)),
            importProvidersFrom(OtelColExporterModule),
            importProvidersFrom(W3CTraceContextPropagatorModule)

and it should work as expected with the OpenTelemetryInterceptorModule. But I cannot get OtelWebTracerModule to work for the Instrumentation though:

            provideAnimations(),
            provideRouter(routes, withComponentInputBinding()),
            provideHttpClient(withInterceptorsFromDi(), withFetch()),
...
            {
                provide: OTEL_INSTRUMENTATION_PLUGINS,
                useValue: [new XMLHttpRequestInstrumentation()]
            },
            importProvidersFrom(OtelWebTracerModule.forRoot(config)),
            importProvidersFrom(OtelColExporterModule),
            importProvidersFrom(W3CTraceContextPropagatorModule)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant