Merge pull request #988 from vector-im/dbkr/enable_otel_by_collector
Allow different OpenTelemetry collectors to be enabled/disabled
This commit is contained in:
commit
dd1485a277
1 changed files with 39 additions and 27 deletions
|
@ -23,12 +23,11 @@ import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
|
||||||
import opentelemetry, { Tracer } from "@opentelemetry/api";
|
import opentelemetry, { Tracer } from "@opentelemetry/api";
|
||||||
import { Resource } from "@opentelemetry/resources";
|
import { Resource } from "@opentelemetry/resources";
|
||||||
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
|
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
|
||||||
import { logger } from "@sentry/utils";
|
import { logger } from "matrix-js-sdk/src/logger";
|
||||||
|
|
||||||
import { PosthogSpanExporter } from "../analytics/OtelPosthogExporter";
|
import { PosthogSpanExporter } from "../analytics/OtelPosthogExporter";
|
||||||
import { Anonymity } from "../analytics/PosthogAnalytics";
|
import { Anonymity } from "../analytics/PosthogAnalytics";
|
||||||
import { Config } from "../config/Config";
|
import { Config } from "../config/Config";
|
||||||
import { getSetting, settingsBus } from "../settings/useSetting";
|
|
||||||
|
|
||||||
const SERVICE_NAME = "element-call";
|
const SERVICE_NAME = "element-call";
|
||||||
|
|
||||||
|
@ -38,10 +37,23 @@ export class ElementCallOpenTelemetry {
|
||||||
private _provider: WebTracerProvider;
|
private _provider: WebTracerProvider;
|
||||||
private _tracer: Tracer;
|
private _tracer: Tracer;
|
||||||
private _anonymity: Anonymity;
|
private _anonymity: Anonymity;
|
||||||
|
private _otlpExporter: OTLPTraceExporter;
|
||||||
|
|
||||||
static globalInit(): void {
|
static globalInit(): void {
|
||||||
settingsBus.on("opt-in-analytics", recheckOTelEnabledStatus);
|
// we always enable opentelemetry in general. We only enable the OTLP
|
||||||
recheckOTelEnabledStatus(getSetting("opt-in-analytics", false));
|
// collector if a URL is defined (and in future if another setting is defined)
|
||||||
|
// The posthog exporteer is always enabled, posthog reporting is enabled or disabled
|
||||||
|
// within the posthog code.
|
||||||
|
const shouldEnableOtlp = Boolean(Config.get().opentelemetry?.collector_url);
|
||||||
|
|
||||||
|
if (!sharedInstance || sharedInstance.isOtlpEnabled !== shouldEnableOtlp) {
|
||||||
|
logger.info("(Re)starting OpenTelemetry debug reporting");
|
||||||
|
sharedInstance?.dispose();
|
||||||
|
|
||||||
|
sharedInstance = new ElementCallOpenTelemetry(
|
||||||
|
Config.get().opentelemetry?.collector_url
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static get instance(): ElementCallOpenTelemetry {
|
static get instance(): ElementCallOpenTelemetry {
|
||||||
|
@ -49,12 +61,6 @@ export class ElementCallOpenTelemetry {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(collectorUrl: string | undefined) {
|
constructor(collectorUrl: string | undefined) {
|
||||||
const otlpExporter = new OTLPTraceExporter({
|
|
||||||
url: collectorUrl,
|
|
||||||
});
|
|
||||||
const consoleExporter = new ConsoleSpanExporter();
|
|
||||||
const posthogExporter = new PosthogSpanExporter();
|
|
||||||
|
|
||||||
// This is how we can make Jaeger show a reaonsable service in the dropdown on the left.
|
// This is how we can make Jaeger show a reaonsable service in the dropdown on the left.
|
||||||
const providerConfig = {
|
const providerConfig = {
|
||||||
resource: new Resource({
|
resource: new Resource({
|
||||||
|
@ -63,7 +69,20 @@ export class ElementCallOpenTelemetry {
|
||||||
};
|
};
|
||||||
this._provider = new WebTracerProvider(providerConfig);
|
this._provider = new WebTracerProvider(providerConfig);
|
||||||
|
|
||||||
this._provider.addSpanProcessor(new SimpleSpanProcessor(otlpExporter));
|
if (collectorUrl) {
|
||||||
|
logger.info("Enabling OTLP collector with URL " + collectorUrl);
|
||||||
|
this._otlpExporter = new OTLPTraceExporter({
|
||||||
|
url: collectorUrl,
|
||||||
|
});
|
||||||
|
this._provider.addSpanProcessor(
|
||||||
|
new SimpleSpanProcessor(this._otlpExporter)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logger.info("OTLP collector disabled");
|
||||||
|
}
|
||||||
|
const consoleExporter = new ConsoleSpanExporter();
|
||||||
|
const posthogExporter = new PosthogSpanExporter();
|
||||||
|
|
||||||
this._provider.addSpanProcessor(new SimpleSpanProcessor(posthogExporter));
|
this._provider.addSpanProcessor(new SimpleSpanProcessor(posthogExporter));
|
||||||
this._provider.addSpanProcessor(new SimpleSpanProcessor(consoleExporter));
|
this._provider.addSpanProcessor(new SimpleSpanProcessor(consoleExporter));
|
||||||
opentelemetry.trace.setGlobalTracerProvider(this._provider);
|
opentelemetry.trace.setGlobalTracerProvider(this._provider);
|
||||||
|
@ -74,6 +93,15 @@ export class ElementCallOpenTelemetry {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public dispose(): void {
|
||||||
|
opentelemetry.trace.setGlobalTracerProvider(null);
|
||||||
|
this._provider?.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isOtlpEnabled(): boolean {
|
||||||
|
return Boolean(this._otlpExporter);
|
||||||
|
}
|
||||||
|
|
||||||
public get tracer(): Tracer {
|
public get tracer(): Tracer {
|
||||||
return this._tracer;
|
return this._tracer;
|
||||||
}
|
}
|
||||||
|
@ -86,19 +114,3 @@ export class ElementCallOpenTelemetry {
|
||||||
return this._anonymity;
|
return this._anonymity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function recheckOTelEnabledStatus(optInAnalayticsEnabled: boolean): void {
|
|
||||||
const shouldEnable =
|
|
||||||
optInAnalayticsEnabled &&
|
|
||||||
Boolean(Config.get().opentelemetry?.collector_url);
|
|
||||||
|
|
||||||
if (shouldEnable && !sharedInstance) {
|
|
||||||
logger.info("Starting OpenTelemetry debug reporting");
|
|
||||||
sharedInstance = new ElementCallOpenTelemetry(
|
|
||||||
Config.get().opentelemetry?.collector_url
|
|
||||||
);
|
|
||||||
} else if (!shouldEnable && sharedInstance) {
|
|
||||||
logger.info("Stopping OpenTelemetry debug reporting");
|
|
||||||
sharedInstance = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue