Skip to content

Commit

Permalink
fix: otel currency updates, get ready for export
Browse files Browse the repository at this point in the history
  • Loading branch information
jharvey10 committed Nov 6, 2023
1 parent 2a612db commit f070c57
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 25 deletions.
101 changes: 101 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@commitlint/config-conventional": "^17.8.1",
"@ibm/telemetry-config-schema": "^0.2.0",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.44.0",
"@opentelemetry/resources": "^1.17.1",
"@opentelemetry/sdk-metrics": "^1.17.1",
"@opentelemetry/semantic-conventions": "^1.17.1",
Expand Down
1 change: 0 additions & 1 deletion src/main/core/custom-resource-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

export const CustomResourceAttributes = Object.freeze({
//
// General attributes
Expand Down
3 changes: 1 addition & 2 deletions src/main/core/initialize-open-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { ManualMetricReader } from './manual-metric-reader.js'
function initializeOpenTelemetry(config: Attributes) {
const resource = Resource.default().merge(
new Resource({
// By default, remove the service name attribute, since it is unused
[SemanticResourceAttributes.SERVICE_NAME]: undefined,
[SemanticResourceAttributes.SERVICE_NAME]: 'IBM Telemetry',
...config
})
)
Expand Down
22 changes: 2 additions & 20 deletions src/main/core/manual-metric-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,15 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { type CollectionResult, type DataPoint, MetricReader } from '@opentelemetry/sdk-metrics'
import { type CollectionResult, MetricReader } from '@opentelemetry/sdk-metrics'
import { type CommonReaderOptions } from '@opentelemetry/sdk-metrics/build/src/types.js'

interface UnlockedDataPoint extends DataPoint<number> {
startTime: DataPoint<number>['startTime']
endTime: DataPoint<number>['endTime']
}

/**
* A metric reader that can be invoked manually with `collect()` to obtain its metrics.
*/
export class ManualMetricReader extends MetricReader {
override async collect(options?: CommonReaderOptions | undefined): Promise<CollectionResult> {
const results = await super.collect(options)

// Zero out all startTime and EndTime attributes in data points
results.resourceMetrics.scopeMetrics.forEach((scopeMetric) => {
scopeMetric.metrics.forEach((metric) => {
metric.dataPoints.forEach((dataPoint) => {
const unlocked = dataPoint as UnlockedDataPoint
unlocked.startTime = [0, 0]
unlocked.endTime = [0, 0]
})
})
})

return results
return await super.collect(options)
}

protected override async onShutdown(): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions src/main/core/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import { type ConfigSchema } from '@ibm/telemetry-config-schema'
import opentelemetry, { type Counter, type Meter } from '@opentelemetry/api'
import opentelemetry, { type Counter, type Meter, ValueType } from '@opentelemetry/api'

import { Loggable } from './log/loggable.js'
import { type Logger } from './log/logger.js'
Expand Down Expand Up @@ -74,7 +74,9 @@ export abstract class Scope extends Loggable {

// Ensure a counter exists
if (this.metrics[dataPoint.name] === undefined) {
this.metrics[dataPoint.name] = this.scopeMeter.createCounter(dataPoint.name)
this.metrics[dataPoint.name] = this.scopeMeter.createCounter(dataPoint.name, {
valueType: ValueType.INT
})
}

// Log the metric
Expand Down
15 changes: 15 additions & 0 deletions src/main/telemetry-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import { type ConfigSchema } from '@ibm/telemetry-config-schema'
// import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'
// import { AggregationTemporality } from '@opentelemetry/sdk-metrics'
import { type Schema } from 'ajv'

import { anonymize } from './core/anonymize.js'
Expand Down Expand Up @@ -107,6 +109,19 @@ export class TelemetryCollector {
- transmit the data to the remote server
*/

// const exporter = new OTLPMetricExporter({
// url: 'http://localhost:3000/v1/metrics',
// headers: {
// // This is where we could define project-specific headers to "authenticate" requests
// Authorization: 'Bearer abc123'
// },
// temporalityPreference: AggregationTemporality.DELTA
// })

// exporter.export(results.resourceMetrics, (exportResult) => {
// console.log(exportResult)
// })

this.logger.debug('Collection results:')
this.logger.debug(JSON.stringify(results, undefined, 2))
}
Expand Down

0 comments on commit f070c57

Please sign in to comment.