Skip to content

Commit

Permalink
Migrate away from deprecated semantic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
haines committed Sep 30, 2024
1 parent 43b663b commit 60b51fc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 50 deletions.
4 changes: 3 additions & 1 deletion packages/opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## [Unreleased]

No notable changes.
### Changed

- Migrate away from [deprecated semantic attributes](https://github.com/open-telemetry/opentelemetry-js/pull/4690) ([#1018](https://github.com/cerbos/cerbos-sdk-javascript/pull/1018))

## [0.5.3] - 2024-09-23

Expand Down
7 changes: 7 additions & 0 deletions packages/opentelemetry/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
unreleased:
type: patch

changed:
- summary: Migrate away from [deprecated semantic attributes](https://github.com/open-telemetry/opentelemetry-js/pull/4690)
pull: 1018

releases:
- version: 0.5.3
date: 2024-09-23
Expand Down
20 changes: 10 additions & 10 deletions packages/opentelemetry/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
trace,
} from "@opentelemetry/api";
import {
SEMATTRS_RPC_GRPC_STATUS_CODE,
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
SEMATTRS_RPC_SYSTEM,
} from "@opentelemetry/semantic-conventions";
ATTR_RPC_GRPC_STATUS_CODE,
ATTR_RPC_METHOD,
ATTR_RPC_SERVICE,
ATTR_RPC_SYSTEM,
} from "@opentelemetry/semantic-conventions/incubating";

import type {
_AbortHandler,
Expand Down Expand Up @@ -162,9 +162,9 @@ export class Transport implements _Transport {
const status: SpanStatus = { code: SpanStatusCode.UNSET };

const attributes: Attributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: serviceName,
[SEMATTRS_RPC_METHOD]: methodName,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: serviceName,
[ATTR_RPC_METHOD]: methodName,
};

const span = this.tracer.startSpan(`${serviceName}/${methodName}`, {
Expand All @@ -189,7 +189,7 @@ export class Transport implements _Transport {

const call = {
succeeded: (): void => {
attributes[SEMATTRS_RPC_GRPC_STATUS_CODE] = Status.OK;
attributes[ATTR_RPC_GRPC_STATUS_CODE] = Status.OK;
finish();
},
failed: (error: unknown): void => {
Expand All @@ -200,7 +200,7 @@ export class Transport implements _Transport {
attributes["cerbos.error"] = error.message;

if (error instanceof NotOK) {
attributes[SEMATTRS_RPC_GRPC_STATUS_CODE] = error.code;
attributes[ATTR_RPC_GRPC_STATUS_CODE] = error.code;
}
}

Expand Down
77 changes: 38 additions & 39 deletions private/test/src/matrix-cerbos/instrumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
} from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import {
SEMATTRS_RPC_GRPC_STATUS_CODE,
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
SEMATTRS_RPC_SYSTEM,
} from "@opentelemetry/semantic-conventions";
ATTR_RPC_GRPC_STATUS_CODE,
ATTR_RPC_METHOD,
ATTR_RPC_SERVICE,
ATTR_RPC_SYSTEM,
} from "@opentelemetry/semantic-conventions/incubating";
import { UnsecuredJWT } from "jose";
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";

Expand Down Expand Up @@ -51,10 +51,10 @@ import {
} from "../servers";

interface ExpectedAttributes extends Attributes {
[SEMATTRS_RPC_SYSTEM]: string;
[SEMATTRS_RPC_SERVICE]: string;
[SEMATTRS_RPC_METHOD]: string;
[SEMATTRS_RPC_GRPC_STATUS_CODE]: Status;
[ATTR_RPC_SYSTEM]: string;
[ATTR_RPC_SERVICE]: string;
[ATTR_RPC_METHOD]: string;
[ATTR_RPC_GRPC_STATUS_CODE]: Status;
"cerbos.error"?: string;
}

Expand Down Expand Up @@ -149,10 +149,10 @@ describe("CerbosInstrumentation", () => {
);

const attributes: ExpectedAttributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: "cerbos.svc.v1.CerbosService",
[SEMATTRS_RPC_METHOD]: "CheckResources",
[SEMATTRS_RPC_GRPC_STATUS_CODE]: 0,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: "cerbos.svc.v1.CerbosService",
[ATTR_RPC_METHOD]: "CheckResources",
[ATTR_RPC_GRPC_STATUS_CODE]: 0,
};

expect(result).toEqual({ value: false });
Expand Down Expand Up @@ -191,10 +191,10 @@ describe("CerbosInstrumentation", () => {
);

const attributes: ExpectedAttributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: "cerbos.svc.v1.CerbosService",
[SEMATTRS_RPC_METHOD]: "CheckResources",
[SEMATTRS_RPC_GRPC_STATUS_CODE]: Status.INVALID_ARGUMENT,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: "cerbos.svc.v1.CerbosService",
[ATTR_RPC_METHOD]: "CheckResources",
[ATTR_RPC_GRPC_STATUS_CODE]: Status.INVALID_ARGUMENT,
"cerbos.error": invalidArgumentDetails,
};

Expand Down Expand Up @@ -268,10 +268,10 @@ describe("CerbosInstrumentation", () => {
});

const attributes: ExpectedAttributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[SEMATTRS_RPC_METHOD]: "ListAuditLogEntries",
[SEMATTRS_RPC_GRPC_STATUS_CODE]: 0,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[ATTR_RPC_METHOD]: "ListAuditLogEntries",
[ATTR_RPC_GRPC_STATUS_CODE]: 0,
};

expect(result).toEqual({ value: entry });
Expand All @@ -297,10 +297,10 @@ describe("CerbosInstrumentation", () => {
);

const attributes: ExpectedAttributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[SEMATTRS_RPC_METHOD]: "ListAuditLogEntries",
[SEMATTRS_RPC_GRPC_STATUS_CODE]: Status.INVALID_ARGUMENT,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[ATTR_RPC_METHOD]: "ListAuditLogEntries",
[ATTR_RPC_GRPC_STATUS_CODE]: Status.INVALID_ARGUMENT,
"cerbos.error": invalidArgumentDetails,
};

Expand Down Expand Up @@ -353,10 +353,10 @@ describe("CerbosInstrumentation", () => {
);

const attributes = {
[SEMATTRS_RPC_SYSTEM]: "grpc",
[SEMATTRS_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[SEMATTRS_RPC_METHOD]: "ListAuditLogEntries",
[SEMATTRS_RPC_GRPC_STATUS_CODE]: Status.CANCELLED,
[ATTR_RPC_SYSTEM]: "grpc",
[ATTR_RPC_SERVICE]: "cerbos.svc.v1.CerbosAdminService",
[ATTR_RPC_METHOD]: "ListAuditLogEntries",
[ATTR_RPC_GRPC_STATUS_CODE]: Status.CANCELLED,
"cerbos.error": expect.stringContaining("Aborted"),
} satisfies Attributes;

Expand All @@ -375,7 +375,7 @@ describe("CerbosInstrumentation", () => {

await expectServerSpan(span, {
...attributes,
[SEMATTRS_RPC_GRPC_STATUS_CODE]: Status.OK, // Although the call is aborted after returning early from for-await on the client, it completes successfully on the server
[ATTR_RPC_GRPC_STATUS_CODE]: Status.OK, // Although the call is aborted after returning early from for-await on the client, it completes successfully on the server
});
});
});
Expand Down Expand Up @@ -405,39 +405,38 @@ describe("CerbosInstrumentation", () => {
kind: SpanKindProto.SPAN_KIND_SERVER,
attributes: expect.arrayContaining([
{
key: SEMATTRS_RPC_SYSTEM,
key: ATTR_RPC_SYSTEM,
value: {
value: {
$case: "stringValue",
stringValue: attributes[SEMATTRS_RPC_SYSTEM],
stringValue: attributes[ATTR_RPC_SYSTEM],
},
},
},
{
key: SEMATTRS_RPC_SERVICE,
key: ATTR_RPC_SERVICE,
value: {
value: {
$case: "stringValue",
stringValue: attributes[SEMATTRS_RPC_SERVICE],
stringValue: attributes[ATTR_RPC_SERVICE],
},
},
},
{
key: SEMATTRS_RPC_METHOD,
key: ATTR_RPC_METHOD,
value: {
value: {
$case: "stringValue",
stringValue: attributes[SEMATTRS_RPC_METHOD],
stringValue: attributes[ATTR_RPC_METHOD],
},
},
},
{
key: SEMATTRS_RPC_GRPC_STATUS_CODE,
key: ATTR_RPC_GRPC_STATUS_CODE,
value: {
value: {
$case: "intValue",
intValue:
attributes[SEMATTRS_RPC_GRPC_STATUS_CODE].toString(),
intValue: attributes[ATTR_RPC_GRPC_STATUS_CODE].toString(),
},
},
},
Expand Down

0 comments on commit 60b51fc

Please sign in to comment.