Skip to content

Commit

Permalink
Add OTLP Exporters (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg committed May 23, 2023
1 parent 2a9d9ee commit 1282707
Show file tree
Hide file tree
Showing 25 changed files with 844 additions and 415 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const appInsights = new ApplicationInsightsClient(config);
| enableAutoCollectExceptions | Sets the state of exception tracking. If true uncaught exceptions will be sent to Application Insights | true|
| enableAutoCollectPerformance | Sets the state of performance tracking. If true performance counters will be collected every second and sent to Application Insights | true|
| enableAutoCollectStandardMetrics | Sets the state of Standard Metrics tracking. If true Standard Metrics will be collected every minute and sent to Application Insights | true|
| enableAutoCollectHeartbeat | Sets the state of request tracking. If true HeartBeat metric data will be collected every 15 minutes and sent to Application Insights | true|
| instrumentations| Allow configuration of OpenTelemetry Instrumentations. | {"http": { enabled: true },"azureSdk": { enabled: false },"mongoDb": { enabled: false },"mySql": { enabled: false },"postgreSql": { enabled: false },"redis": { enabled: false }}|
| logInstrumentations| Allow configuration of Log Instrumentations. | {"console": { enabled: false },"bunyan": { enabled: false },"winston": { enabled: false }}|
| extendedMetrics | Enable/Disable specific extended Metrics(gc, heap and loop). |{"gc":false,"heap":false,"loop":false}|
Expand All @@ -125,7 +124,6 @@ All these properties except aadTokenCredential and resource could be configured
"azureMonitorExporterConfig": {"connectionString":"<YOUR_CONNECTION_STRING>"},
"samplingRatio": 0.8,
"enableAutoCollectExceptions": true,
"enableAutoCollectHeartbeat": true,
"instrumentations":{
"azureSdk": {
"enabled": false
Expand Down
578 changes: 568 additions & 10 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@
"@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.1",
"@opentelemetry/api": "^1.4.1",
"@opentelemetry/core": "^1.11.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.39.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.39.1",
"@opentelemetry/instrumentation": "^0.35.1",
"@opentelemetry/instrumentation-http": "^0.35.1",
"@opentelemetry/instrumentation-mongodb": "^0.34.0",
"@opentelemetry/instrumentation-mysql": "^0.32.0",
"@opentelemetry/instrumentation-pg": "^0.34.0",
"@opentelemetry/instrumentation-redis": "^0.34.1",
"@opentelemetry/instrumentation-redis-4": "^0.34.1",
"@opentelemetry/otlp-exporter-base": "^0.39.1",
"@opentelemetry/resources": "^1.11.0",
"@opentelemetry/sdk-metrics": "^1.11.0",
"@opentelemetry/sdk-trace-base": "^1.11.0",
Expand Down
1 change: 0 additions & 1 deletion src/agent/agentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class AgentLoader {
this._config = new ApplicationInsightsConfig();
this._config.azureMonitorExporterConfig.disableOfflineStorage = false;
this._config.enableAutoCollectExceptions = true;
this._config.enableAutoCollectHeartbeat = true;
this._config.enableAutoCollectPerformance = true;
this._config.enableAutoCollectStandardMetrics = true;
this._config.samplingRatio = 1; // Sample all telemetry by default
Expand Down
23 changes: 17 additions & 6 deletions src/metrics/handlers/customMetricsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
PeriodicExportingMetricReader,
PeriodicExportingMetricReaderOptions,
} from "@opentelemetry/sdk-metrics";
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { ApplicationInsightsConfig } from "../../shared";


export class CustomMetricsHandler {
private _config: ApplicationInsightsConfig;
private _collectionInterval = 60000; // 60 seconds
private _meterProvider: MeterProvider;
private _azureExporter: AzureMonitorMetricExporter;
private _metricReader: PeriodicExportingMetricReader;
private _azureMonitorExporter: AzureMonitorMetricExporter;
private _otlpExporter: OTLPMetricExporter;
private _meter: Meter;

constructor(config: ApplicationInsightsConfig, options?: { collectionInterval: number }) {
Expand All @@ -22,13 +24,22 @@ export class CustomMetricsHandler {
resource: this._config.resource,
};
this._meterProvider = new MeterProvider(meterProviderConfig);
this._azureExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
this._azureMonitorExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
const metricReaderOptions: PeriodicExportingMetricReaderOptions = {
exporter: this._azureExporter as any,
exporter: this._azureMonitorExporter,
exportIntervalMillis: options?.collectionInterval || this._collectionInterval,
};
this._metricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(this._metricReader);
const azureMonitorMetricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(azureMonitorMetricReader);

if (config.otlpMetricExporterConfig?.enabled) {
this._otlpExporter = new OTLPMetricExporter(config.otlpMetricExporterConfig.baseConfig);
const otlpMetricReader = new PeriodicExportingMetricReader({
exporter: this._otlpExporter,
exportIntervalMillis: options?.collectionInterval || this._collectionInterval,
});
this._meterProvider.addMetricReader(otlpMetricReader);
}
this._meter = this._meterProvider.getMeter("ApplicationInsightsCustomMetricsMeter");
}

Expand Down
113 changes: 0 additions & 113 deletions src/metrics/handlers/heartBeatHandler.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/metrics/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
// Licensed under the MIT license.

export { CustomMetricsHandler } from "./customMetricsHandler";
export { HeartBeatHandler } from "./heartBeatHandler";
export { PerformanceCounterMetricsHandler } from "./performanceCounterMetricsHandler";
export { StandardMetricsHandler } from "./standardMetricsHandler";
23 changes: 10 additions & 13 deletions src/metrics/handlers/performanceCounterMetricsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import {
PeriodicExportingMetricReaderOptions,
View,
} from "@opentelemetry/sdk-metrics";
import { ReadableSpan } from "@opentelemetry/sdk-trace-base";
import {
MetricName,
NativeMetricsCounter,
PerformanceCounter,
} from "../types";
import { ProcessMetrics } from "../collection/processMetrics";
import { RequestMetrics } from "../collection/requestMetrics";
import { ApplicationInsightsConfig } from "../../shared";
import { NativePerformanceMetrics } from "../collection/nativePerformanceMetrics";
import { ReadableSpan } from "@opentelemetry/sdk-trace-base";


export class PerformanceCounterMetricsHandler {
private _config: ApplicationInsightsConfig;
private _collectionInterval = 60000; // 60 seconds
private _meterProvider: MeterProvider;
private _azureExporter: AzureMonitorMetricExporter;
private _metricReader: PeriodicExportingMetricReader;
private _azureMonitorExporter: AzureMonitorMetricExporter;
private _meter: Meter;
private _processMetrics: ProcessMetrics;
private _requestMetrics: RequestMetrics;
Expand All @@ -39,22 +36,22 @@ export class PerformanceCounterMetricsHandler {
views: this._getViews(),
};
this._meterProvider = new MeterProvider(meterProviderConfig);
this._azureExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
this._azureMonitorExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
const metricReaderOptions: PeriodicExportingMetricReaderOptions = {
exporter: this._azureExporter as any,
exporter: this._azureMonitorExporter,
exportIntervalMillis: options?.collectionInterval || this._collectionInterval,
};
this._metricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(this._metricReader);
const azureMonitorMetricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(azureMonitorMetricReader);
this._meter = this._meterProvider.getMeter("ApplicationInsightsPerfMetricsMeter");
this._processMetrics = new ProcessMetrics(this._meter);
this._requestMetrics = new RequestMetrics(this._meter);
}

/**
* @deprecated This should not be used
*/
public start() {
/**
* @deprecated This should not be used
*/
public start() {
// No Op
}

Expand Down
26 changes: 19 additions & 7 deletions src/metrics/handlers/standardMetricsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import {
} from "@opentelemetry/sdk-metrics";
import { ReadableSpan } from "@opentelemetry/sdk-trace-base";
import { SemanticAttributes, SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { ApplicationInsightsConfig } from "../../shared";
import { DependencyMetrics } from "../collection/dependencyMetrics";
import { ExceptionMetrics } from "../collection/exceptionMetrics";
import { RequestMetrics } from "../collection/requestMetrics";
import { TraceMetrics } from "../collection/traceMetrics";
import { IMetricDependencyDimensions, IMetricRequestDimensions, IMetricTraceDimensions, IStandardMetricBaseDimensions, MetricName, PreAggregatedMetricPropertyNames, StandardMetric, StandardMetricIds } from "../types";
import { IMetricDependencyDimensions, IMetricRequestDimensions, IMetricTraceDimensions, IStandardMetricBaseDimensions, MetricName, StandardMetric, StandardMetricIds } from "../types";


export class StandardMetricsHandler {
private _config: ApplicationInsightsConfig;
private _collectionInterval = 60000; // 60 seconds
private _meterProvider: MeterProvider;
private _azureExporter: AzureMonitorMetricExporter;
private _metricReader: PeriodicExportingMetricReader;
private _azureMonitorExporter: AzureMonitorMetricExporter;
private _otlpExporter: OTLPMetricExporter;
private _meter: Meter;
private _dependencyMetrics: DependencyMetrics;
private _requestMetrics: RequestMetrics;
Expand All @@ -36,13 +38,23 @@ export class StandardMetricsHandler {
views: this._getViews(),
};
this._meterProvider = new MeterProvider(meterProviderConfig);
this._azureExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
this._azureMonitorExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterConfig);
const metricReaderOptions: PeriodicExportingMetricReaderOptions = {
exporter: this._azureExporter as any,
exporter: this._azureMonitorExporter,
exportIntervalMillis: options?.collectionInterval || this._collectionInterval,
};
this._metricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(this._metricReader);
const azureMonitorMetricReader = new PeriodicExportingMetricReader(metricReaderOptions);
this._meterProvider.addMetricReader(azureMonitorMetricReader);

if (config.otlpMetricExporterConfig?.enabled) {
this._otlpExporter = new OTLPMetricExporter(config.otlpMetricExporterConfig.baseConfig);
const otlpMetricReader = new PeriodicExportingMetricReader({
exporter: this._otlpExporter,
exportIntervalMillis: options?.collectionInterval || this._collectionInterval,
});
this._meterProvider.addMetricReader(otlpMetricReader);
}

this._meter = this._meterProvider.getMeter("ApplicationInsightsStandardMetricsMeter");
this._requestMetrics = new RequestMetrics(this._meter);
this._dependencyMetrics = new DependencyMetrics(this._meter);
Expand Down
7 changes: 0 additions & 7 deletions src/metrics/metricHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions"
import { ApplicationInsightsConfig } from "../shared";
import {
CustomMetricsHandler,
HeartBeatHandler,
StandardMetricsHandler,
PerformanceCounterMetricsHandler,
} from "./handlers";
Expand All @@ -16,7 +15,6 @@ export class MetricHandler {
private _config: ApplicationInsightsConfig;
private _perfCounterMetricsHandler: PerformanceCounterMetricsHandler;
private _standardMetricsHandler: StandardMetricsHandler;
private _heartbeatHandler: HeartBeatHandler;
private _customMetricsHandler: CustomMetricsHandler;

constructor(config: ApplicationInsightsConfig) {
Expand All @@ -28,9 +26,6 @@ export class MetricHandler {
if (this._config.enableAutoCollectPerformance) {
this._perfCounterMetricsHandler = new PerformanceCounterMetricsHandler(this._config);
}
if (this._config.enableAutoCollectHeartbeat) {
this._heartbeatHandler = new HeartBeatHandler(this._config);
}
}

/**
Expand All @@ -44,12 +39,10 @@ export class MetricHandler {
this._customMetricsHandler?.shutdown();
this._perfCounterMetricsHandler?.shutdown();
this._standardMetricsHandler?.shutdown();
this._heartbeatHandler?.shutdown();
}

public async flush(): Promise<void> {
await this._customMetricsHandler?.flush();
await this._heartbeatHandler?.flush();
await this._standardMetricsHandler?.flush();
await this._perfCounterMetricsHandler?.flush();
}
Expand Down
1 change: 0 additions & 1 deletion src/metrics/statsbeat/statsbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export class Statsbeat {
this._azureVm = new AzureVirtualMachine();
this._statsbeatConfig = new ApplicationInsightsConfig();
this._statsbeatConfig.connectionString = this._connectionString;
this._statsbeatConfig.enableAutoCollectHeartbeat = false;
this._statsbeatConfig.enableAutoCollectPerformance = false;
this._statsbeatConfig.enableAutoCollectStandardMetrics = false;

Expand Down

0 comments on commit 1282707

Please sign in to comment.