Skip to content

Commit

Permalink
Enable otlp logging and compatibility with env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Jul 13, 2022
1 parent 533bf45 commit 9ab2f05
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
"@mapbox/mapbox-gl-draw": "1.3.0",
"@mapbox/mapbox-gl-rtl-text": "0.2.3",
"@mapbox/vector-tile": "1.3.1",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/api-metrics": "^0.30.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.30.0",
"@opentelemetry/exporter-prometheus": "^0.30.0",
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/monitoring_collection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ monitoring_collection.opentelemetry.metrics:
Authorization: "Bearer SECRET_TOKEN"
```

Alternatively, OTLP Exporter can be configured using environment variables `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`, `OTEL_EXPORTER_OTLP_METRICS_HEADERS`. [See OTLP Exporter docs](https://opentelemetry.io/docs/reference/specification/protocol/exporter/) for details.

It's possible to configure logging for the OTLP integration. If not informed, the default will be `info`

```yml
monitoring_collection.opentelemetry.metrics:
logLevel: warn | info | debug | warn | none | verbose | all
```

For connection-level debug information you can set these variables:

```bash
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/monitoring_collection/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const configSchema = schema.object({
url: schema.maybe(schema.string()),
headers: schema.maybe(schema.recordOf(schema.string(), schema.string())),
exportIntervalMillis: schema.number({ defaultValue: 10000 }),
logLevel: schema.string({ defaultValue: 'info' }),
}),
prometheus: schema.object({
enabled: schema.boolean({ defaultValue: false }),
Expand Down
20 changes: 17 additions & 3 deletions x-pack/plugins/monitoring_collection/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { metrics } from '@opentelemetry/api-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics-base';
import { Resource } from '@opentelemetry/resources';
import { diag, DiagLogger, DiagLogLevel } from '@opentelemetry/api';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import * as grpc from '@grpc/grpc-js';
import { PrometheusExporter } from './lib/prometheus_exporter';
Expand All @@ -41,6 +42,7 @@ export class MonitoringCollectionPlugin implements Plugin<MonitoringCollectionSe
private readonly initializerContext: PluginInitializerContext;
private readonly logger: Logger;
private readonly config: MonitoringCollectionConfig;
private readonly otlpLogger: DiagLogger;

private metrics: Record<string, Metric<any>> = {};

Expand All @@ -50,6 +52,14 @@ export class MonitoringCollectionPlugin implements Plugin<MonitoringCollectionSe
this.initializerContext = initializerContext;
this.logger = initializerContext.logger.get();
this.config = initializerContext.config.get();

this.otlpLogger = {
debug: (message) => this.logger.debug(message),
error: (message) => this.logger.error(message),
info: (message) => this.logger.info(message),
warn: (message) => this.logger.warn(message),
verbose: (message) => this.logger.trace(message),
};
}

async getMetric(type: string) {
Expand Down Expand Up @@ -127,18 +137,22 @@ export class MonitoringCollectionPlugin implements Plugin<MonitoringCollectionSe
metrics.setGlobalMeterProvider(meterProvider);

const otlpConfig = this.config.opentelemetry?.metrics.otlp;
if (otlpConfig?.url) {
const url = otlpConfig?.url ?? process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT;
if (url) {
// Add OTLP exporter
const url = otlpConfig.url;

// Set Authorization headers
// OTLPMetricExporter internally will look at OTEL_EXPORTER_OTLP_METRICS_HEADERS env variable
// if `headers` are not present in the kibana config file
const metadata = new grpc.Metadata();
if (otlpConfig.headers) {
for (const [key, value] of Object.entries(otlpConfig.headers)) {
metadata.add(key, value);
}
}

const otlpLogLevel = otlpConfig.logLevel.toUpperCase() as keyof typeof DiagLogLevel;
diag.setLogger(this.otlpLogger, DiagLogLevel[otlpLogLevel]);

this.logger.debug(`Registering OpenTelemetry metrics exporter to ${url}`);
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
Expand Down

0 comments on commit 9ab2f05

Please sign in to comment.