Skip to content

Commit

Permalink
feat(resource): create sync resource with some attributes that resolv…
Browse files Browse the repository at this point in the history
…e asynchronously (open-telemetry#3460)


Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 13, 2023
1 parent 3bc0807 commit 47444f2
Show file tree
Hide file tree
Showing 68 changed files with 1,537 additions and 555 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* feat(resource): create sync resource with some attributes that resolve asynchronously [#3460](https://github.com/open-telemetry/opentelemetry-js/pull/3460) @samimusallam
* feat (api-logs): separate Events API into its own package [3550](https://github.com/open-telemetry/opentelemetry-js/pull/3550) @martinkuba
* feat(sdk-metrics): apply binary search in histogram recording [#3539](https://github.com/open-telemetry/opentelemetry-js/pull/3539) @legendecas
* perf(propagator-jaeger): improve deserializeSpanContext performance [#3541](https://github.com/open-telemetry/opentelemetry-js/pull/3541) @doochik
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ const sdk = new opentelemetry.NodeSDK({

// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start()
.then(() => console.log('Tracing initialized'))
.catch((error) => console.log('Error initializing tracing', error));
sdk.start();

// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
Expand Down Expand Up @@ -277,6 +275,11 @@ These instrumentations are hosted at <https://github.com/open-telemetry/opentele

## Upgrade guidelines

### 0.35.x to 0.36.0

- `@opentelemetry/sdk-node` changed `await start()` to now be synchronous
- `@opentelemetry/sdk-node` changed `await detectResources()` to now be synchronous

### 0.28.x to 0.29.x

- `@opentelemetry/exporter-trace-otlp-http` is now exporting `scopeSpans` instead of `instrumentationLibrarySpans`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { diag } from '@opentelemetry/api';
import {
Detector,
IResource,
Resource,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
Expand All @@ -27,7 +28,7 @@ import { BROWSER_ATTRIBUTES, UserAgentData } from './types';
* BrowserDetector will be used to detect the resources related to browser.
*/
class BrowserDetector implements Detector {
async detect(config?: ResourceDetectionConfig): Promise<Resource> {
async detect(config?: ResourceDetectionConfig): Promise<IResource> {
const isBrowser = typeof navigator !== 'undefined';
if (!isBrowser) {
return Resource.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import * as sinon from 'sinon';
import { Resource } from '@opentelemetry/resources';
import { IResource } from '@opentelemetry/resources';
import { browserDetector } from '../src/BrowserDetector';
import { describeBrowser, assertResource, assertEmptyResource } from './util';

Expand Down Expand Up @@ -47,7 +47,7 @@ describeBrowser('browserDetector()', () => {
},
});

const resource: Resource = await browserDetector.detect();
const resource: IResource = await browserDetector.detect();
assertResource(resource, {
platform: 'platform',
brands: ['Chromium 106', 'Google Chrome 106', 'Not;A=Brand 99'],
Expand All @@ -63,7 +63,7 @@ describeBrowser('browserDetector()', () => {
userAgentData: undefined,
});

const resource: Resource = await browserDetector.detect();
const resource: IResource = await browserDetector.detect();
assertResource(resource, {
language: 'en-US',
user_agent: 'dddd',
Expand All @@ -74,7 +74,7 @@ describeBrowser('browserDetector()', () => {
sinon.stub(globalThis, 'navigator').value({
userAgent: '',
});
const resource: Resource = await browserDetector.detect();
const resource: IResource = await browserDetector.detect();
assertEmptyResource(resource);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { Suite } from 'mocha';
import * as assert from 'assert';
import { BROWSER_ATTRIBUTES } from '../src/types';
import { Resource } from '@opentelemetry/resources';
import { IResource } from '@opentelemetry/resources';

export function describeBrowser(title: string, fn: (this: Suite) => void) {
title = `Browser: ${title}`;
Expand All @@ -27,7 +27,7 @@ export function describeBrowser(title: string, fn: (this: Suite) => void) {
}

export const assertResource = (
resource: Resource,
resource: IResource,
validations: {
platform?: string;
brands?: string[];
Expand Down Expand Up @@ -74,6 +74,6 @@ export const assertResource = (
*
* @param resource the Resource to validate
*/
export const assertEmptyResource = (resource: Resource) => {
export const assertEmptyResource = (resource: IResource) => {
assert.strictEqual(Object.keys(resource.attributes).length, 0);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Histogram,
} from '@opentelemetry/sdk-metrics';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { IResource } from '@opentelemetry/resources';

type PrometheusDataTypeLiteral =
| 'counter'
Expand Down Expand Up @@ -340,7 +340,7 @@ export class PrometheusSerializer {
return results;
}

protected _serializeResource(resource: Resource): string {
protected _serializeResource(resource: IResource): string {
const name = 'target_info';
const help = `# HELP ${name} Target metadata`;
const type = `# TYPE ${name} gauge`;
Expand Down
18 changes: 10 additions & 8 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {
} from '@opentelemetry/instrumentation';
import {
Detector,
detectResources,
DetectorSync,
detectResourcesSync,
envDetector,
IResource,
processDetector,
Resource,
ResourceDetectionConfig,
Expand Down Expand Up @@ -63,8 +65,8 @@ export class NodeSDK {
private _meterProviderConfig?: MeterProviderConfig;
private _instrumentations: InstrumentationOption[];

private _resource: Resource;
private _resourceDetectors: Detector[];
private _resource: IResource;
private _resourceDetectors: Array<Detector | DetectorSync>;

private _autoDetectResources: boolean;

Expand Down Expand Up @@ -183,7 +185,7 @@ export class NodeSDK {
}

/** Detect resource attributes */
public async detectResources(): Promise<void> {
public detectResources(): void {
if (this._disabled) {
return;
}
Expand All @@ -192,18 +194,18 @@ export class NodeSDK {
detectors: this._resourceDetectors,
};

this.addResource(await detectResources(internalConfig));
this.addResource(detectResourcesSync(internalConfig));
}

/** Manually add a resource */
public addResource(resource: Resource): void {
public addResource(resource: IResource): void {
this._resource = this._resource.merge(resource);
}

/**
* Once the SDK has been configured, call this method to construct SDK components and register them with the OpenTelemetry API.
*/
public async start(): Promise<void> {
public start(): void {
if (this._disabled) {
return;
}
Expand All @@ -213,7 +215,7 @@ export class NodeSDK {
});

if (this._autoDetectResources) {
await this.detectResources();
this.detectResources();
}

this._resource =
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { ContextManager, SpanAttributes } from '@opentelemetry/api';
import { TextMapPropagator } from '@opentelemetry/api';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { Detector, Resource } from '@opentelemetry/resources';
import { Detector, DetectorSync, Resource } from '@opentelemetry/resources';
import { MetricReader, View } from '@opentelemetry/sdk-metrics';
import {
Sampler,
Expand All @@ -35,7 +35,7 @@ export interface NodeSDKConfiguration {
views: View[];
instrumentations: InstrumentationOption[];
resource: Resource;
resourceDetectors: Detector[];
resourceDetectors: Array<Detector | DetectorSync>;
sampler: Sampler;
serviceName?: string;
spanProcessor: SpanProcessor;
Expand Down
Loading

0 comments on commit 47444f2

Please sign in to comment.