Improve JS Instrumentation Docs (#4096)
Co-authored-by: Trent Mick <trentm@gmail.com>
This commit is contained in:
parent
ffb4ca28e6
commit
0b86a2df4b
|
|
@ -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 = {
|
||||
|
|
@ -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,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
|
@ -168,7 +168,7 @@ 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 () => {
|
||||
|
|
@ -176,8 +176,7 @@ const FrontendTracer = async () => {
|
|||
|
||||
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,
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
@ -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({
|
||||
|
|
@ -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,
|
||||
|
|
@ -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',
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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',
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
@ -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 >}}
|
||||
|
|
@ -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(SEMATTRS_CODE_FILEPATH, __filename);
|
||||
|
||||
// Do some work...
|
||||
|
||||
|
|
@ -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',
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
@ -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',
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
@ -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,
|
||||
|
|
@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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, SEMRESATTRS_SERVICE_INSTANCE_ID } = require('@opentelemetry/semantic-conventions');
|
||||
...
|
||||
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",
|
||||
})
|
||||
...
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
@ -243,7 +243,7 @@ const {
|
|||
|
||||
const providerConfig = {
|
||||
resource: new Resource({
|
||||
[SemanticResourceAttributes.SERVICE_NAME]: '<your function name>',
|
||||
[SEMRESATTRS_SERVICE_NAME]: '<your function name>',
|
||||
}),
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue