Skip to content

Commit

Permalink
Merge pull request open-telemetry#190 from opentelemetrybot/opentelem…
Browse files Browse the repository at this point in the history
…etrybot/merge-main

[AutoMerge][Main] Merging staged change(s) to main
  • Loading branch information
MSNev committed Sep 21, 2023
2 parents 9d604e0 + 8644ba6 commit 7cbfbe5
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,29 @@ export class ContainerDetector implements Detector {
this.UTF8_UNICODE
);
const splitData = rawData.trim().split('\n');
for (const str of splitData) {
if (str.length >= this.CONTAINER_ID_LENGTH) {
return str.substring(str.length - this.CONTAINER_ID_LENGTH);
for (const line of splitData) {
const lastSlashIdx = line.lastIndexOf('/');
if (lastSlashIdx === -1) {
continue;
}
const lastSection = line.substring(lastSlashIdx + 1);
const colonIdx = lastSection.lastIndexOf(':');
if (colonIdx !== -1) {
// since containerd v1.5.0+, containerId is divided by the last colon when the cgroupDriver is systemd:
// https://github.com/containerd/containerd/blob/release/1.5/pkg/cri/server/helpers_linux.go#L64
return lastSection.substring(colonIdx + 1);
} else {
let startIdx = lastSection.lastIndexOf('-');
let endIdx = lastSection.lastIndexOf('.');

startIdx = startIdx === -1 ? 0 : startIdx + 1;
if (endIdx === -1) {
endIdx = lastSection.length;
}
if (startIdx > endIdx) {
continue;
}
return lastSection.substring(startIdx, endIdx);
}
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ContainerDetector } from '../src';
describe('ContainerDetector', () => {
let readStub;
const correctCgroupV1Data =
'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
'12:pids:/kubepods.slice/bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';
const correctCgroupV2Data = `tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse`;
Expand Down
2 changes: 2 additions & 0 deletions auto-merge/js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :house: (Internal)

* test: added a performance benchmark test for span creation [#4105](https://github.com/open-telemetry/opentelemetry-js/pull/4105)

## 1.17.0

### :bug: (Bug Fix)
Expand Down
10 changes: 10 additions & 0 deletions auto-merge/js/experimental/examples/opencensus-shim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The example has:
- Root Spans (on client), instrumented with OpenCensus's HTTP instrumentation
- Child Span from a remote parent (on server), instrumented with OpenCensus's HTTP instrumentation
- Another Child Span created in the server representing some work being done, instrumented manually with OpenTelemetry.
- Server metrics coming from OpenCensus's HTTP instrumentation, available through the
OpenTelemetry's Prometheus exporter.

## Installation

Expand Down Expand Up @@ -64,6 +66,14 @@ Go to Jaeger with your browser <http://localhost:16686/> and click on the "Servi

<p align="center"><img src="./images/jaeger-trace.png"/></p>

## Check the Prometheus metrics

Load the Prometheus metrics endpoint of the server at <http://localhost:9464/metrics> in your
browser. You should see the `opencensus_io_http_server_*` related metrics in
the output.

<p align="center"><img src="./images/prom-metrics.png"/></p>

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
},
"dependencies": {
"@opencensus/core": "0.1.0",
"@opencensus/instrumentation-http": "0.1.0",
"@opencensus/nodejs-base": "0.1.0",
"@opentelemetry/api": "1.6.0",
"@opentelemetry/exporter-prometheus": "0.43.0",
"@opentelemetry/exporter-trace-otlp-grpc": "0.43.0",
"@opentelemetry/resources": "1.17.0",
"@opentelemetry/sdk-metrics": "1.17.0",
"@opentelemetry/sdk-trace-node": "1.17.0",
"@opentelemetry/semantic-conventions": "1.17.0",
"@opentelemetry/shim-opencensus": "0.43.0"
Expand Down
2 changes: 2 additions & 0 deletions auto-merge/js/experimental/examples/opencensus-shim/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const setup = require('./setup');
const utils = require('./utils');
const { trace } = require('@opentelemetry/api');

const oc = require('@opencensus/core');

setup('opencensus-shim-example-server');
const http = require('http');

Expand Down
49 changes: 38 additions & 11 deletions auto-merge/js/experimental/examples/opencensus-shim/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,64 @@
*/
'use strict';

const { DiagConsoleLogger, diag, DiagLogLevel } = require('@opentelemetry/api');
const { diag, metrics } = require('@opentelemetry/api');
const {
NodeTracerProvider,
BatchSpanProcessor,
} = require('@opentelemetry/sdk-trace-node');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const {
OTLPTraceExporter,
} = require('@opentelemetry/exporter-trace-otlp-grpc');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { Resource } = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
} = require('@opentelemetry/semantic-conventions');
const { OpenCensusMetricProducer } = require('@opentelemetry/shim-opencensus');
const instrumentationHttp = require('@opencensus/instrumentation-http');
const { TracingBase } = require('@opencensus/nodejs-base');
const oc = require('@opencensus/core');

module.exports = function setup(serviceName) {
const tracing = require('@opencensus/nodejs-base');
/**
* You can alternatively just use the @opentelemetry/nodejs package directly:
*
* ```js
* const tracing = require('@opencensus/nodejs');
* ```
*/
const tracing = new TracingBase(['http']);
tracing.tracer = new oc.CoreTracer();

diag.setLogger(new DiagConsoleLogger(), { logLevel: DiagLogLevel.ALL });
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
const resource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
});
provider.addSpanProcessor(
const tracerProvider = new NodeTracerProvider({ resource });
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(new OTLPTraceExporter(), {
scheduledDelayMillis: 5000,
})
);
provider.register();
tracerProvider.register();

const meterProvider = new MeterProvider({ resource });
meterProvider.addMetricReader(
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
);
metrics.setGlobalMeterProvider(meterProvider);

// Start OpenCensus tracing
tracing.start({ samplingRate: 1, logger: diag });
tracing.start({ samplingRate: 1, logger: diag, stats: oc.globalStats });
// Register OpenCensus HTTP stats views
instrumentationHttp.registerAllViews(oc.globalStats);

return provider;
return tracerProvider;
};
23 changes: 21 additions & 2 deletions auto-merge/js/experimental/packages/shim-opencensus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ More details are available in the [OpenCensus Compatibility Specification](https
npm install --save @opentelemetry/shim-opencensus
```

## Usage
## Tracing usage

### Installing the shim's require-in-the-middle hook

This is the recommended way to use the shim.
This is the recommended way to use the shim for tracing.

This package provides a `require-in-the-middle` hook which replaces OpenCensus's `CoreTracer`
class with a shim implementation that writes to the OpenTelemetry API. This will cause all
Expand Down Expand Up @@ -72,6 +72,25 @@ tracer.startRootSpan({name: 'main'}, rootSpan => {
});
```

## Metrics usage

OpenCensus metrics can be collected and sent to an OpenTelemetry exporter by providing the
`OpenCensusMetricProducer` to your `MetricReader`. For example, to export OpenCensus metrics
through the OpenTelemetry Prometheus exporter:

```js
meterProvider.addMetricReader(
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
);
```

## Example

See [examples/opencensus-shim](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/examples/opencensus-shim) for a short example.
Expand Down
2 changes: 2 additions & 0 deletions auto-merge/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:browser": "lerna run test:browser",
"test:webworker": "lerna run test:webworker",
"test:backcompat": "lerna run test:backcompat",
"test:bench": "lerna run test:bench",
"bootstrap": "lerna bootstrap --hoist --nohoist='zone.js'",
"changelog": "lerna-changelog",
"codecov": "lerna run codecov",
Expand Down Expand Up @@ -65,6 +66,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "5.59.11",
"@typescript-eslint/parser": "5.59.11",
"benchmark": "2.1.4",
"eslint": "8.44.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-header": "3.1.1",
Expand Down
30 changes: 15 additions & 15 deletions common/config/rush/npm-shrinkwrap.json

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

1 change: 1 addition & 0 deletions pkgs/sdk/trace/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"tdd:browser": "karma start",
"tdd:node": "npm run test -- --watch-extensions ts --watch",
"test": "npm run test:node && npm run test:browser && npm run test:webworker",
"test:bench": "node test/performance/benchmark/index.js",
"test:browser": "nyc karma start ./karma.conf.js --single-run",
"test:debug": "nyc karma start ./karma.debug.conf.js --wait",
"test:node": "",
Expand Down
17 changes: 17 additions & 0 deletions pkgs/sdk/trace/base/test/performance/benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

require('./span');
43 changes: 43 additions & 0 deletions pkgs/sdk/trace/base/test/performance/benchmark/span.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.
*/

const Benchmark = require('benchmark');
const { BasicTracerProvider } = require('../../../build/src');

const tracerProvider = new BasicTracerProvider();
const tracer = tracerProvider.getTracer('test')

const suite = new Benchmark.Suite();

suite.on('cycle', event => {
console.log(String(event.target));
});

suite.add('create spans (10 attributes)', function() {
const span = tracer.startSpan('span');
span.setAttribute('aaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('bbbbbbbbbbbbbbbbbbbb', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('cccccccccccccccccccc', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('dddddddddddddddddddd', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('eeeeeeeeeeeeeeeeeeee', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('ffffffffffffffffffff', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('gggggggggggggggggggg', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('hhhhhhhhhhhhhhhhhhhh', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('iiiiiiiiiiiiiiiiiiii', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('jjjjjjjjjjjjjjjjjjjj', 'aaaaaaaaaaaaaaaaaaaa');
});

suite.run();

0 comments on commit 7cbfbe5

Please sign in to comment.