docs: update old/deprecated 'SemanticAttributes' usage (#5664)

This commit is contained in:
Trent Mick 2025-05-09 09:42:09 -07:00 committed by GitHub
parent b76281a32c
commit 039b6b4df1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 37 deletions

View File

@ -293,10 +293,10 @@ can be used to information about the record measurement itself.
```typescript ```typescript
async function myTask() { async function myTask() {
const httpServerDuration = meter.createHistogram("http.server.duration", { const httpServerDuration = meter.createHistogram("my.http.server.request.duration", {
description: 'A http server duration', description: 'HTTP server request duration',
unit: 'milliseconds', unit: 's',
valueType: ValueType.INT valueType: ValueType.DOUBLE
}); });
const startTime = new Date().getTime() const startTime = new Date().getTime()
try { try {
@ -305,12 +305,12 @@ async function myTask() {
} catch (err) { } catch (err) {
} finally { } finally {
const endTime = new Date().getTime() const endTime = new Date().getTime()
const executionTime = endTime - startTime const executionTime = (endTime - startTime) / 1000
httpServerDuration.record(executionTime, { httpServerDuration.record(executionTime, {
[SemanticAttributes.HTTP_METHOD]: 'POST', [ATTR_HTTP_REQUEST_METHOD]: 'POST',
[SemanticAttributes.HTTP_STATUS_CODE]: '200', [ATTR_HTTP_RESPONSE_STATUS_CODE]: '200',
[SemanticAttributes.HTTP_SCHEME]: 'https', [ATTR_URL_SCHEME]: 'https',
}) })
} }
} }
@ -416,17 +416,17 @@ const meterProvider = new MeterProvider({
}); });
// Create histogram metric // Create histogram metric
const httpServerDuration = meter.createHistogram("http.server.duration", { const httpServerDuration = meter.createHistogram('my.http.server.request.duration', {
description: 'A http server duration', description: 'HTTP server request duration',
unit: 'milliseconds', unit: 's',
valueType: ValueType.INT valueType: ValueType.DOUBLE
}); });
// Record measurement for histogram // Record measurement for histogram
httpServerDuration.record(50, { httpServerDuration.record(50, {
[SemanticAttributes.HTTP_METHOD]: 'POST', [ATTR_HTTP_REQUEST_METHOD]: 'POST',
[SemanticAttributes.HTTP_STATUS_CODE]: '200', [ATTR_HTTP_RESPONSE_STATUS_CODE]: '200',
[SemanticAttributes.HTTP_SCHEME]: 'https', [ATTR_URL_SCHEME]: 'https',
}); });
``` ```

View File

@ -79,20 +79,31 @@ server.on("GET", "/user/:id", onGet);
Using span relationships, attributes, kind, and the related [semantic conventions](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/trace.md), we can more accurately describe the span in a way our tracing backend will more easily understand. The following example uses these mechanisms, which are described below. Using span relationships, attributes, kind, and the related [semantic conventions](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/trace.md), we can more accurately describe the span in a way our tracing backend will more easily understand. The following example uses these mechanisms, which are described below.
```typescript ```typescript
import { NetTransportValues, SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { trace, context, SpanKind, SpanStatusCode } from '@opentelemetry/api'; import { trace, context, SpanKind, SpanStatusCode } from '@opentelemetry/api';
import {
ATTR_HTTP_REQUEST_METHOD,
ATTR_URL_PATH,
ATTR_URL_SCHEME,
ATTR_HTTP_RESPONSE_STATUS_CODE,
ATTR_NETWORK_PEER_ADDRESS,
ATTR_DB_SYSTEM_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
} from '@opentelemetry/semantic-conventions';
async function onGet(request, response) { async function onGet(request, response) {
// HTTP semantic conventions determine the span name and attributes for this span // HTTP semantic conventions determine the span name and attributes for this span
const span = tracer.startSpan(`GET /user/:id`, { const span = tracer.startSpan('GET /user/:id', {
// attributes can be added when the span is started // attributes can be added when the span is started
attributes: { attributes: {
// Attributes from the HTTP trace semantic conventions // Attributes from the HTTP trace semantic conventions
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md // https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-server-span
[SemanticAttributes.HTTP_METHOD]: "GET", [ATTR_HTTP_REQUEST_METHOD]: 'POST',
[SemanticAttributes.HTTP_FLAVOR]: "1.1", [ATTR_URL_PATH]: request.url,
[SemanticAttributes.HTTP_URL]: request.url, [ATTR_URL_SCHEME]: 'https',
[SemanticAttributes.NET_PEER_IP]: "192.0.2.5", [ATTR_HTTP_RESPONSE_STATUS_CODE]: '200',
[ATTR_NETWORK_PEER_ADDRESS]: '192.0.2.5',
}, },
// This span represents a remote incoming synchronous request // This span represents a remote incoming synchronous request
kind: SpanKind.SERVER kind: SpanKind.SERVER
@ -142,26 +153,16 @@ async function getUser(userId) {
const span = tracer.startSpan("SELECT ShopDb.Users", { const span = tracer.startSpan("SELECT ShopDb.Users", {
attributes: { attributes: {
// Attributes from the database trace semantic conventions // Attributes from the database trace semantic conventions
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md // https://opentelemetry.io/docs/specs/semconv/database/database-spans/#span-definition
[SemanticAttributes.DB_SYSTEM]: "mysql", [ATTR_DB_SYSTEM_NAME]: "mysql",
[SemanticAttributes.DB_CONNECTION_STRING]: "Server=shopdb.example.com;Database=ShopDb;Uid=billing_user;TableCache=true;UseCompression=True;MinimumPoolSize=10;MaximumPoolSize=50;", [ATTR_DB_NAMESPACE]: "ShopDb",
[SemanticAttributes.DB_USER]: "app_user", [ATTR_DB_OPERATION_NAME]: "SELECT",
[SemanticAttributes.NET_PEER_NAME]: "shopdb.example.com", [ATTR_DB_QUERY_TEXT]: `SELECT * from Users WHERE id = ?`,
[SemanticAttributes.NET_PEER_IP]: "192.0.2.12",
[SemanticAttributes.NET_PEER_PORT]: 3306,
[SemanticAttributes.NET_TRANSPORT]: NetTransportValues.IP_TCP,
[SemanticAttributes.DB_NAME]: "ShopDb",
[SemanticAttributes.DB_STATEMENT]: `Select * from Users WHERE user_id = ${userId}`,
[SemanticAttributes.DB_OPERATION]: "SELECT",
[SemanticAttributes.DB_SQL_TABLE]: "Users",
}, },
kind: SpanKind.CLIENT, kind: SpanKind.CLIENT,
}); });
const user = await db.select("Users", { id: userId }); const user = await db.select("Users", { id: userId });
span.setStatus({
code: SpanStatusCode.OK,
});
span.end(); span.end();
return user; return user;
} }