Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JS Instrumentation Docs #4096

4 changes: 2 additions & 2 deletions assets/js/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';

const collectorOptions = {
Expand All @@ -16,7 +16,7 @@ const collectorOptions = {
const exporter = new OTLPTraceExporter(collectorOptions);

const resources = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'opentelemetry.io',
[SEMRESATTRS_SERVICE_NAME]: 'opentelemetry.io',
'browser.language': navigator.language,
});

Expand Down
17 changes: 8 additions & 9 deletions content/en/docs/demo/services/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ span = tracer.startSpan(`HTTP ${method}`, {
links: [{ context: syntheticSpan.spanContext() }],
attributes: {
'app.synthetic_request': true,
[SemanticAttributes.HTTP_TARGET]: target,
[SemanticAttributes.HTTP_STATUS_CODE]: response.statusCode,
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '',
[SemanticAttributes.HTTP_URL]: `${headers.host}${url}`,
[SemanticAttributes.HTTP_FLAVOR]: httpVersion,
[SEMATTRS_HTTP_TARGET]: target,
[SEMATTRS_HTTP_STATUS_CODE]: response.statusCode,
[SEMATTRS_HTTP_METHOD]: method,
[SEMATTRS_HTTP_USER_AGENT]: headers['user-agent'] || '',
[SEMATTRS_HTTP_URL]: `${headers.host}${url}`,
[SEMATTRS_HTTP_FLAVOR]: httpVersion,
},
});
```
Expand Down Expand Up @@ -168,16 +168,15 @@ import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

const FrontendTracer = async () => {
const { ZoneContextManager } = await import('@opentelemetry/context-zone');

const provider = new WebTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]:
process.env.NEXT_PUBLIC_OTEL_SERVICE_NAME,
[SEMRESATTRS_SERVICE_NAME]: process.env.NEXT_PUBLIC_OTEL_SERVICE_NAME,
}),
});

Expand Down
62 changes: 40 additions & 22 deletions content/en/docs/languages/js/instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,15 @@ import {
ConsoleMetricExporter,
} from '@opentelemetry/sdk-metrics';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';

const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'yourServiceName',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0',
[SEMRESATTRS_SERVICE_NAME]: 'yourServiceName',
[SEMRESATTRS_SERVICE_VERSION]: '1.0',
}),
traceExporter: new ConsoleSpanExporter(),
metricReader: new PeriodicExportingMetricReader({
Expand All @@ -250,13 +253,14 @@ const {
} = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} = require('@opentelemetry/semantic-conventions');

const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'dice-server',
[SemanticResourceAttributes.SERVICE_VERSION]: '0.1.0',
[SEMRESATTRS_SERVICE_NAME]: 'dice-server',
[SEMRESATTRS_SERVICE_VERSION]: '0.1.0',
}),
traceExporter: new ConsoleSpanExporter(),
metricReader: new PeriodicExportingMetricReader({
Expand Down Expand Up @@ -347,7 +351,10 @@ SDK initialization code in it:

```ts
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import {
BatchSpanProcessor,
Expand All @@ -356,8 +363,8 @@ import {

const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'service-name-here',
[SemanticResourceAttributes.SERVICE_VERSION]: '0.1.0',
[SEMRESATTRS_SERVICE_NAME]: 'service-name-here',
[SEMRESATTRS_SERVICE_VERSION]: '0.1.0',
}),
);

Expand All @@ -377,7 +384,8 @@ provider.register();
const opentelemetry = require('@opentelemetry/api');
const { Resource } = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} = require('@opentelemetry/semantic-conventions');
const { WebTracerProvider } = require('@opentelemetry/sdk-trace-web');
const {
Expand All @@ -387,8 +395,8 @@ const {

const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'service-name-here',
[SemanticResourceAttributes.SERVICE_VERSION]: '0.1.0',
[SEMRESATTRS_SERVICE_NAME]: 'service-name-here',
[SEMRESATTRS_SERVICE_VERSION]: '0.1.0',
}),
);

Expand Down Expand Up @@ -933,13 +941,19 @@ Add the following to the top of your application file:
{{< tabpane text=true >}} {{% tab TypeScript %}}

```ts
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_CODE_FUNCTION,
SEMATTRS_CODE_FILEPATH,
} from '@opentelemetry/semantic-conventions';
```

{{% /tab %}} {{% tab JavaScript %}}

```js
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
const {
SEMATTRS_CODE_FUNCTION,
SEMATTRS_CODE_FILEPATH,
} = require('@opentelemetry/semantic-conventions');
```

{{% /tab %}} {{< /tabpane >}}
Expand All @@ -949,8 +963,8 @@ Finally, you can update your file to include semantic attributes:
```javascript
const doWork = () => {
tracer.startActiveSpan('app.doWork', (span) => {
span.setAttribute(SemanticAttributes.CODE_FUNCTION, 'doWork');
span.setAttribute(SemanticAttributes.CODE_FILEPATH, __filename);
span.setAttribute(SEMATTRS_CODE_FUNCTION, 'doWork');
span.setAttribute(SSEMATTRS_CODE_FILEPATH, __filename);
legalimpurity marked this conversation as resolved.
Show resolved Hide resolved

// Do some work...

Expand Down Expand Up @@ -1244,12 +1258,15 @@ import {
PeriodicExportingMetricReader,
} from '@opentelemetry/sdk-metrics';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';

const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'dice-server',
[SemanticResourceAttributes.SERVICE_VERSION]: '0.1.0',
[SEMRESATTRS_SERVICE_NAME]: 'dice-server',
[SEMRESATTRS_SERVICE_VERSION]: '0.1.0',
}),
);

Expand Down Expand Up @@ -1279,13 +1296,14 @@ const {
} = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_VERSION,
} = require('@opentelemetry/semantic-conventions');

const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'service-name-here',
[SemanticResourceAttributes.SERVICE_VERSION]: '0.1.0',
[SEMRESATTRS_SERVICE_NAME]: 'service-name-here',
[SEMRESATTRS_SERVICE_VERSION]: '0.1.0',
}),
);

Expand Down
18 changes: 12 additions & 6 deletions content/en/docs/languages/js/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ with a request hook:

```typescript
import { Span } from '@opentelemetry/api';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_URL,
} from '@opentelemetry/semantic-conventions';
import {
ExpressInstrumentation,
ExpressLayerType,
Expand All @@ -230,8 +233,8 @@ import {
const expressInstrumentation = new ExpressInstrumentation({
requestHook: function (span: Span, info: ExpressRequestInfo) {
if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
span.setAttribute(SemanticAttributes.HTTP_METHOD, info.request.method);
span.setAttribute(SemanticAttributes.HTTP_URL, info.request.baseUrl);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
span.setAttribute(SEMATTRS_HTTP_URL, info.request.baseUrl);
}
},
});
Expand All @@ -243,7 +246,10 @@ const expressInstrumentation = new ExpressInstrumentation({

```javascript
/*instrumentation.js*/
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
const {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_URL,
} = require('@opentelemetry/semantic-conventions');
const {
ExpressInstrumentation,
ExpressLayerType,
Expand All @@ -252,8 +258,8 @@ const {
const expressInstrumentation = new ExpressInstrumentation({
requestHook: function (span, info) {
if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
span.setAttribute(SemanticAttributes.HTTP_METHOD, info.request.method);
span.setAttribute(SemanticAttributes.HTTP_URL, info.request.baseUrl);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
span.setAttribute(SEMATTRS_HTTP_URL, info.request.baseUrl);
}
},
});
Expand Down
10 changes: 5 additions & 5 deletions content/en/docs/languages/js/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ configuration option, where you can set them. For example you can update the
```javascript
...
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_NAMESPACE, SEMRESATTRS_SERVICE_VERSION } = require('@opentelemetry/semantic-conventions');
legalimpurity marked this conversation as resolved.
Show resolved Hide resolved
...
const sdk = new opentelemetry.NodeSDK({
...
resource: new Resource({
[ SemanticResourceAttributes.SERVICE_NAME ]: "yourServiceName",
[ SemanticResourceAttributes.SERVICE_NAMESPACE ]: "yourNameSpace",
[ SemanticResourceAttributes.SERVICE_VERSION ]: "1.0",
[ SemanticResourceAttributes.SERVICE_INSTANCE_ID ]: "my-instance-id-1",
[ SEMRESATTRS_SERVICE_NAME ]: "yourServiceName",
[ SEMRESATTRS_SERVICE_NAMESPACE ]: "yourNameSpace",
[ SEMRESATTRS_SERVICE_VERSION ]: "1.0",
[ SEMRESATTRS_SERVICE_INSTANCE_ID ]: "my-instance-id-1",
})
...
});
Expand Down
4 changes: 2 additions & 2 deletions content/en/docs/languages/js/serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ service. Please make sure that you provide a `SERVICE_NAME` and that you set the

const { Resource } = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
SEMRESATTRS_SERVICE_NAME,
} = require('@opentelemetry/semantic-conventions');
const api = require('@opentelemetry/api');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
Expand All @@ -243,7 +243,7 @@ const {

const providerConfig = {
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: '<your function name>',
[SEMRESATTRS_SERVICE_NAME]: '<your function name>',
}),
};

Expand Down