Skip to content

Commit

Permalink
Merge branch 'main' into sw/context-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Jan 31, 2022
2 parents 8162621 + a824578 commit 5e9368a
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/w3c-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --ignore-scripts
npx lerna bootstrap --no-ci --scope=propagation-validation-server --include-dependencies
npx lerna bootstrap --no-ci --hoist --scope=propagation-validation-server --include-dependencies
- name: Install and Bootstrap (cache hit) 🔧
if: steps.cache.outputs.cache-hit == 'true'
run: |
npm ci --ignore-scripts
npx lerna bootstrap --scope=propagation-validation-server --include-dependencies
npx lerna bootstrap --hoist --scope=propagation-validation-server --include-dependencies
- name: Generate version.ts files
run: lerna run version
Expand Down
30 changes: 30 additions & 0 deletions doc/development-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,33 @@ npm run docs
```

The document will be available under `packages/opentelemetry-api/docs/out` path.

## Platform conditional exports

Universal packages are packages that can be used in both web browsers and
Node.js environment. These packages may be implemented on top of different
platform APIs to achieve the same goal. Like accessing the _global_ reference,
we have different preferred ways to do it:

- In Node.js, we access the _global_ reference with `globalThis` or `global`:

```js
/// packages/opentelemetry-core/src/platform/node/globalThis.ts
export const _globalThis = typeof globalThis === 'object' ? globalThis : global;
```

- In web browser, we access the _global_ reference with the following definition:

```js
/// packages/opentelemetry-core/src/platform/browser/globalThis.ts
export const _globalThis: typeof globalThis =
typeof globalThis === 'object' ? globalThis :
typeof self === 'object' ? self :
typeof window === 'object' ? window :
typeof global === 'object' ? global :
{} as typeof globalThis;
```

Even though the implementation may differ, the exported names must be aligned.
It can be confusing if exported names present in one environment but not in the
others.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"compile": "tsc --build"
},
"dependencies": {
"@opentelemetry/api": "^1.0.3",
"@opentelemetry/api": "~1.0.3",
"@opentelemetry/context-async-hooks": "1.0.1",
"@opentelemetry/core": "1.0.1",
"@opentelemetry/sdk-trace-base": "1.0.1",
Expand Down
23 changes: 9 additions & 14 deletions packages/opentelemetry-exporter-jaeger/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan {
spanId: Utils.encodeInt64(span.spanContext().spanId),
parentSpanId: parentSpan,
operationName: span.name,
references: spanLinksToThriftRefs(span.links, span.parentSpanId),
references: spanLinksToThriftRefs(span.links),
flags: span.spanContext().traceFlags || DEFAULT_FLAGS,
startTime: Utils.encodeInt64(hrTimeToMicroseconds(span.startTime)),
duration: Utils.encodeInt64(hrTimeToMicroseconds(span.duration)),
Expand All @@ -120,21 +120,16 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan {
/** Translate OpenTelemetry {@link Link}s to Jaeger ThriftReference. */
function spanLinksToThriftRefs(
links: Link[],
parentSpanId?: string
): ThriftReference[] {
return links
.map((link): ThriftReference | null => {
if (link.context.spanId === parentSpanId) {
const refType = ThriftReferenceType.FOLLOWS_FROM;
const traceId = link.context.traceId;
const traceIdHigh = Utils.encodeInt64(traceId.slice(0, 16));
const traceIdLow = Utils.encodeInt64(traceId.slice(16));
const spanId = Utils.encodeInt64(link.context.spanId);
return { traceIdLow, traceIdHigh, spanId, refType };
}
return null;
})
.filter(ref => !!ref) as ThriftReference[];
.map((link): ThriftReference => {
const refType = ThriftReferenceType.FOLLOWS_FROM;
const traceId = link.context.traceId;
const traceIdHigh = Utils.encodeInt64(traceId.slice(0, 16));
const traceIdLow = Utils.encodeInt64(traceId.slice(16));
const spanId = Utils.encodeInt64(link.context.spanId);
return { traceIdLow, traceIdHigh, spanId, refType };
});
}

/** Translate OpenTelemetry attribute value to Jaeger TagValue. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ describe('transform', () => {
assert.strictEqual(tag7.key, 'cost');
assert.strictEqual(tag7.vType, 'DOUBLE');
assert.strictEqual(tag7.vDouble, 112.12);
assert.strictEqual(thriftSpan.references.length, 0);

assert.strictEqual(thriftSpan.references.length, 1);
const [reference1] = thriftSpan.references;
assert.strictEqual(reference1.refType, ThriftReferenceType.FOLLOWS_FROM);
assert.strictEqual(reference1.spanId.toString('hex'), readableSpan.links[0].context.spanId);
assert.strictEqual(reference1.traceIdLow.toString('hex'), readableSpan.links[0].context.traceId.substring(16, 32));
assert.strictEqual(reference1.traceIdHigh.toString('hex'), readableSpan.links[0].context.traceId.substring(0, 16));

assert.strictEqual(thriftSpan.logs.length, 1);
const [log1] = thriftSpan.logs;
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-resources/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* 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
*
* http://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.
*/

const karmaWebpackConfig = require('../../karma.webpack');
const karmaBaseConfig = require('../../karma.base');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
}))
};
15 changes: 14 additions & 1 deletion packages/opentelemetry-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"scripts": {
"compile": "tsc --build tsconfig.all.json",
"clean": "tsc --build --clean tsconfig.all.json",
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:browser": "nyc karma start --single-run",
"tdd": "npm run test -- --watch-extensions ts --watch",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"version": "node ../../scripts/version-update.js",
Expand Down Expand Up @@ -59,14 +61,25 @@
"@types/mocha": "8.2.3",
"@types/node": "14.17.33",
"@types/sinon": "10.0.6",
"@types/webpack-env": "1.16.3",
"codecov": "3.8.3",
"karma": "6.3.8",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
"karma-mocha-webworker": "1.3.0",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "4.0.2",
"mocha": "7.2.0",
"nock": "13.0.11",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "12.0.1",
"ts-mocha": "8.0.0",
"typescript": "4.4.4"
"typescript": "4.4.4",
"webpack": "4.46.0",
"webpack-cli": "4.9.1",
"webpack-merge": "5.8.0"
},
"peerDependencies": {
"@opentelemetry/api": ">=1.0.0 <1.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
import { diag } from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
Detector,
Resource,
ResourceDetectionConfig,
ResourceAttributes,
} from '../../../';
import { Resource } from '../Resource';
import { Detector, ResourceAttributes } from '../types';
import { ResourceDetectionConfig } from '../config';

/**
* EnvDetector can be used to detect the presence of and create a Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

import { diag } from '@opentelemetry/api';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Detector, Resource, ResourceDetectionConfig } from '../../../';
import { ResourceAttributes } from '../../../types';
import { Resource } from '../Resource';
import { Detector, ResourceAttributes } from '../types';
import { ResourceDetectionConfig } from '../config';

/**
* ProcessDetector will be used to detect the resources related current process running
* and being instrumented from the NodeJS Process module.
*/
class ProcessDetector implements Detector {
async detect(config?: ResourceDetectionConfig): Promise<Resource> {
// Skip if not in Node.js environment.
if (typeof process !== 'object') {
return Resource.empty();
}
const processResource: ResourceAttributes = {
[SemanticResourceAttributes.PROCESS_PID]: process.pid,
[SemanticResourceAttributes.PROCESS_EXECUTABLE_NAME]: process.title || '',
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './Resource';
export * from './platform';
export * from './types';
export * from './config';
export * from './detectors';
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@

export * from './default-service-name';
export * from './detect-resources';
export * from './detectors';
13 changes: 12 additions & 1 deletion packages/opentelemetry-resources/test/Resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as assert from 'assert';
import { SDK_INFO } from '@opentelemetry/core';
import { Resource } from '../src';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { describeBrowser, describeNode } from './util';

describe('Resource', () => {
const resource1 = new Resource({
Expand Down Expand Up @@ -100,7 +101,7 @@ describe('Resource', () => {
});
});

describe('.default()', () => {
describeNode('.default()', () => {
it('should return a default resource', () => {
const resource = Resource.default();
assert.strictEqual(resource.attributes[SemanticResourceAttributes.TELEMETRY_SDK_NAME], SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_NAME]);
Expand All @@ -109,4 +110,14 @@ describe('Resource', () => {
assert.strictEqual(resource.attributes[SemanticResourceAttributes.SERVICE_NAME], `unknown_service:${process.argv0}`);
});
});

describeBrowser('.default()', () => {
it('should return a default resource', () => {
const resource = Resource.default();
assert.strictEqual(resource.attributes[SemanticResourceAttributes.TELEMETRY_SDK_NAME], SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_NAME]);
assert.strictEqual(resource.attributes[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE], SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]);
assert.strictEqual(resource.attributes[SemanticResourceAttributes.TELEMETRY_SDK_VERSION], SDK_INFO[SemanticResourceAttributes.TELEMETRY_SDK_VERSION]);
assert.strictEqual(resource.attributes[SemanticResourceAttributes.SERVICE_NAME], 'unknown_service');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 { RAW_ENVIRONMENT } from '@opentelemetry/core';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { envDetector, Resource } from '../../../src';
import {
assertEmptyResource,
assertWebEngineResource,
} from '../../util/resource-assertions';
import { describeBrowser } from '../../util';

describeBrowser('envDetector() on web browser', () => {
describe('with valid env', () => {
before(() => {
(globalThis as typeof globalThis & RAW_ENVIRONMENT).OTEL_RESOURCE_ATTRIBUTES =
'webengine.name="chromium",webengine.version="99",webengine.description="Chromium"';
});

after(() => {
delete (globalThis as typeof globalThis & RAW_ENVIRONMENT).OTEL_RESOURCE_ATTRIBUTES;
});

it('should return resource information from environment variable', async () => {
const resource: Resource = await envDetector.detect();
assertWebEngineResource(resource, {
[SemanticResourceAttributes.WEBENGINE_NAME]: 'chromium',
[SemanticResourceAttributes.WEBENGINE_VERSION]: '99',
[SemanticResourceAttributes.WEBENGINE_DESCRIPTION]: 'Chromium',
});
});
});

describe('with empty env', () => {
it('should return empty resource', async () => {
const resource: Resource = await envDetector.detect();
assertEmptyResource(resource);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 * as sinon from 'sinon';
import { processDetector, Resource } from '../../../src';
import {
assertEmptyResource,
} from '../../util/resource-assertions';
import { describeBrowser } from '../../util';

describeBrowser('processDetector() on web browser', () => {
afterEach(() => {
sinon.restore();
});

it('should return empty resource', async () => {
const resource: Resource = await processDetector.detect();
assertEmptyResource(resource);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { envDetector, Resource } from '../../src';
import { envDetector, Resource } from '../../../src';
import {
assertK8sResource,
assertEmptyResource,
} from '../util/resource-assertions';
} from '../../util/resource-assertions';
import { describeNode } from '../../util';

describe('envDetector()', () => {
describeNode('envDetector() on Node.js', () => {
describe('with valid env', () => {
before(() => {
process.env.OTEL_RESOURCE_ATTRIBUTES =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/
import * as sinon from 'sinon';
import { processDetector, Resource } from '../../src';
import { processDetector, Resource } from '../../../src';
import {
assertProcessResource,
assertEmptyResource,
} from '../util/resource-assertions';
} from '../../util/resource-assertions';
import { describeNode } from '../../util';

describe('processDetector()', () => {
describeNode('processDetector() on Node.js', () => {
afterEach(() => {
sinon.restore();
});
Expand Down
Loading

0 comments on commit 5e9368a

Please sign in to comment.