Skip to content

Commit

Permalink
feat(otel): switch to global registration of meter provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jdharvey-ibm committed Aug 31, 2023
1 parent 0d14985 commit c4284ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function run() {
const gitOrigin = exec('git remote get-url origin')
const repository = tokenizeRepository(gitOrigin)

const { metricReader } = initializeOpenTelemetry({
const metricReader = initializeOpenTelemetry({
[ResourceAttributes.EMITTER_NAME]: packageJsonInfo.name,
[ResourceAttributes.EMITTER_VERSION]: packageJsonInfo.version,
[ResourceAttributes.PROJECT_ID]: config.projectId,
Expand Down
23 changes: 12 additions & 11 deletions src/main/core/initialize-open-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* 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 opentelemetry from '@opentelemetry/api'
import { Resource } from '@opentelemetry/resources'
import { MeterProvider } from '@opentelemetry/sdk-metrics'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'

import { ManualMetricReader } from './manual-metric-reader.js'
import type * as ResourceAttributes from './resource-attributes.js'
Expand All @@ -25,26 +27,25 @@ interface InitializeOpenTelemetryConfig {
* Initializes the OpenTelemetry tooling based on the provided config.
*
* @param config - The configuration options needed to initialize OpenTelemetry.
* @returns An object containing a metric reader and a meter provider.
* @returns A metric reader that will contain all collected data.
*/
function initializeOpenTelemetry(config: InitializeOpenTelemetryConfig) {
const resource = Resource.default().merge(new Resource({ ...config }))

const resource = Resource.default().merge(
new Resource({
// By default, remove the service name attribute, since it is unused
[SemanticResourceAttributes.SERVICE_NAME]: undefined,
...config
})
)
const metricReader = new ManualMetricReader()

const meterProvider = new MeterProvider({ resource })

meterProvider.addMetricReader(metricReader)

// TODO: can we get back to only using the global object for this? Or do we still want to have
// direct access to it via return values from this function?
// Set this MeterProvider to be global to the app being instrumented.
// otel.metrics.setGlobalMeterProvider(myServiceMeterProvider)
opentelemetry.metrics.setGlobalMeterProvider(meterProvider)

return {
metricReader,
meterProvider
}
return metricReader
}

export { initializeOpenTelemetry }

0 comments on commit c4284ea

Please sign in to comment.