forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Collector Metric Exporter for the Web (open-telemetry#1308)
Co-authored-by: Daniel Dyla <[email protected]>
- Loading branch information
1 parent
f0323e6
commit 4d92d39
Showing
15 changed files
with
666 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
global: | ||
scrape_interval: 15s # Default is every 1 minute. | ||
|
||
scrape_configs: | ||
- job_name: 'collector' | ||
# metrics_path defaults to '/metrics' | ||
# scheme defaults to 'http'. | ||
static_configs: | ||
- targets: ['collector:9464'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const { CollectorMetricExporter } = require('@opentelemetry/exporter-collector'); | ||
const { MeterProvider } = require('@opentelemetry/metrics'); | ||
|
||
const metricExporter = new CollectorMetricExporter({ | ||
serviceName: 'basic-metric-service', | ||
// logger: new ConsoleLogger(LogLevel.DEBUG), | ||
}); | ||
|
||
const meter = new MeterProvider({ | ||
exporter: metricExporter, | ||
interval: 1000, | ||
}).getMeter('example-prometheus'); | ||
|
||
const requestCounter = meter.createCounter('requests', { | ||
description: 'Example of a Counter', | ||
}); | ||
|
||
const upDownCounter = meter.createUpDownCounter('test_up_down_counter', { | ||
description: 'Example of a UpDownCounter', | ||
}); | ||
|
||
const labels = { pid: process.pid, environment: 'staging' }; | ||
|
||
setInterval(() => { | ||
requestCounter.bind(labels).add(1); | ||
upDownCounter.bind(labels).add(Math.random() > 0.5 ? 1 : -1); | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ges/opentelemetry-exporter-collector/src/platform/browser/CollectorExporterBrowserBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { CollectorExporterBase } from '../../CollectorExporterBase'; | ||
import { CollectorExporterConfigBrowser } from './types'; | ||
import * as collectorTypes from '../../types'; | ||
import { parseHeaders } from '../../util'; | ||
import { sendWithBeacon, sendWithXhr } from './util'; | ||
|
||
/** | ||
* Collector Metric Exporter abstract base class | ||
*/ | ||
export abstract class CollectorExporterBrowserBase< | ||
ExportItem, | ||
ServiceRequest | ||
> extends CollectorExporterBase< | ||
CollectorExporterConfigBrowser, | ||
ExportItem, | ||
ServiceRequest | ||
> { | ||
private _headers: Record<string, string>; | ||
private _useXHR: boolean = false; | ||
|
||
/** | ||
* @param config | ||
*/ | ||
constructor(config: CollectorExporterConfigBrowser = {}) { | ||
super(config); | ||
this._useXHR = | ||
!!config.headers || typeof navigator.sendBeacon !== 'function'; | ||
if (this._useXHR) { | ||
this._headers = parseHeaders(config.headers, this.logger); | ||
} else { | ||
this._headers = {}; | ||
} | ||
} | ||
|
||
onInit(): void { | ||
window.addEventListener('unload', this.shutdown); | ||
} | ||
|
||
onShutdown(): void { | ||
window.removeEventListener('unload', this.shutdown); | ||
} | ||
|
||
send( | ||
items: ExportItem[], | ||
onSuccess: () => void, | ||
onError: (error: collectorTypes.CollectorExporterError) => void | ||
) { | ||
const serviceRequest = this.convert(items); | ||
const body = JSON.stringify(serviceRequest); | ||
|
||
if (this._useXHR) { | ||
sendWithXhr( | ||
body, | ||
this.url, | ||
this._headers, | ||
this.logger, | ||
onSuccess, | ||
onError | ||
); | ||
} else { | ||
sendWithBeacon(body, this.url, this.logger, onSuccess, onError); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/opentelemetry-exporter-collector/src/platform/browser/CollectorMetricExporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { MetricRecord, MetricExporter } from '@opentelemetry/metrics'; | ||
import * as collectorTypes from '../../types'; | ||
import { CollectorExporterBrowserBase } from './CollectorExporterBrowserBase'; | ||
import { toCollectorExportMetricServiceRequest } from '../../transformMetrics'; | ||
import { CollectorExporterConfigBrowser } from './types'; | ||
|
||
const DEFAULT_COLLECTOR_URL = 'http://localhost:55680/v1/metrics'; | ||
const DEFAULT_SERVICE_NAME = 'collector-metric-exporter'; | ||
|
||
/** | ||
* Collector Metric Exporter for Web | ||
*/ | ||
export class CollectorMetricExporter | ||
extends CollectorExporterBrowserBase< | ||
MetricRecord, | ||
collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest | ||
> | ||
implements MetricExporter { | ||
// Converts time to nanoseconds | ||
private readonly _startTime = new Date().getTime() * 1000000; | ||
|
||
convert( | ||
metrics: MetricRecord[] | ||
): collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest { | ||
return toCollectorExportMetricServiceRequest( | ||
metrics, | ||
this._startTime, | ||
this | ||
); | ||
} | ||
|
||
getDefaultUrl(config: CollectorExporterConfigBrowser): string { | ||
return config.url || DEFAULT_COLLECTOR_URL; | ||
} | ||
|
||
getDefaultServiceName(config: CollectorExporterConfigBrowser): string { | ||
return config.serviceName || DEFAULT_SERVICE_NAME; | ||
} | ||
} |
Oops, something went wrong.