admin管理员组

文章数量:1287517

I have a front-end application (React) that would like to send logs to a collector. The problem is in my current setup I set my token at init, and can't seem to find a smooth way to update my token (valid for 5mins). So I need a way to update it.

Here's a part of the code:

Adding logs to the queue:

logger.emit({
  severityText: logLevel,
  body: message,
  attributes: {
    ...attributes,
    timestamp: new Date().toISOString(),
  },
});

This is where I create the logger provider.

const loggerProvider = new LoggerProvider({ resource });
const logExporter = new OTLPLogExporter({ url: `${COLLECTOR_URL}/logs`, headers: commonHeaders });
loggerProvider.addLogRecordProcessor(new BatchLogRecordProcessor(logExporter));

const logger = loggerProvider.getLogger("default-logger");
export { logger };

I've tried to override the send method in the OTLPLogExporter.

This is my attempt to override.

class DynamicOTLPLogExporter extends OTLPLogExporter {
  constructor(url: string) {
    super({ url });
  }

  send(objects: any[], onSuccess: () => void, onError: (error: any) => void) {
    this.headers = {
        Authorization: window.accessToken ?? "",
        concurrencyLimit: "1",
    }
    super.send(objects, onSuccess, onError);
  }
}```

I have a front-end application (React) that would like to send logs to a collector. The problem is in my current setup I set my token at init, and can't seem to find a smooth way to update my token (valid for 5mins). So I need a way to update it.

Here's a part of the code:

Adding logs to the queue:

logger.emit({
  severityText: logLevel,
  body: message,
  attributes: {
    ...attributes,
    timestamp: new Date().toISOString(),
  },
});

This is where I create the logger provider.

const loggerProvider = new LoggerProvider({ resource });
const logExporter = new OTLPLogExporter({ url: `${COLLECTOR_URL}/logs`, headers: commonHeaders });
loggerProvider.addLogRecordProcessor(new BatchLogRecordProcessor(logExporter));

const logger = loggerProvider.getLogger("default-logger");
export { logger };

I've tried to override the send method in the OTLPLogExporter.

This is my attempt to override.

class DynamicOTLPLogExporter extends OTLPLogExporter {
  constructor(url: string) {
    super({ url });
  }

  send(objects: any[], onSuccess: () => void, onError: (error: any) => void) {
    this.headers = {
        Authorization: window.accessToken ?? "",
        concurrencyLimit: "1",
    }
    super.send(objects, onSuccess, onError);
  }
}```
Share Improve this question asked Feb 24 at 7:25 CedervallCedervall 1,9394 gold badges17 silver badges22 bronze badges 3
  • I'm not sure updating the token is something that you should care about really. Thw attack vector of stopping someone accessing the collector who can just download the package again isn't really something you can avoid. Rate limiting at the collector end, with filtering and hopefully burst protection for pricing on your backend are the ways to go. – MartinDotNet Commented Feb 24 at 9:59
  • The thing is I can't do anything about the collector service, and that service require a valid token. – Cedervall Commented Feb 24 at 14:47
  • I'm saying that requiring a token in your collector isn't the right approach, it's not stopping anything, so remove the access token from the collector itself further, changing to token without a deployment (if you're sending directly to an external service) isn't something to optimize for. – MartinDotNet Commented Feb 24 at 18:19
Add a comment  | 

1 Answer 1

Reset to default 0

I found an issue on github: https://github/open-telemetry/opentelemetry-js/issues/2903?reload=1 where a similar problem. I modified it a little bit and here's the solution.

I read somewhere that they're redoing the export functions so while this works now ("@opentelemetry/exporter-logs-otlp-http": "^0.48.0") it might not work on future implementations.

class CustomOTLPLogExporter extends OTLPLogExporter {
  private readonly getToken: () => string | undefined;
  private _headers: Record<string, unknown>;

  constructor(config: OTLPExporterNodeConfigBase, getToken: () => string | undefined) {
    super(config);
    this.getToken = getToken;
    this._headers = { ...config.headers };
  }

  export(items: LogRecord[], resultCallback: (result: ExportResult) => void) {
    // Update headers with a fresh token on each export call
    this._headers = { ...this._headers, Authorization: `${this.getToken()}` };
    return super.export(items, resultCallback);
  }
}

本文标签: typescriptToken update for open telemetry in frontendStack Overflow