feat(core-shared): OTel resource builder
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { buildResource } from "./resource";
|
||||
import {
|
||||
ATTR_SERVICE_NAME,
|
||||
ATTR_SERVICE_VERSION,
|
||||
ATTR_DEPLOYMENT_ENVIRONMENT_NAME,
|
||||
} from "@opentelemetry/semantic-conventions/incubating";
|
||||
|
||||
describe("buildResource", () => {
|
||||
it("populates service name, version, and environment", () => {
|
||||
const r = buildResource({
|
||||
serviceName: "web-next",
|
||||
serviceVersion: "1.0.0",
|
||||
environment: "production",
|
||||
});
|
||||
expect(r.attributes[ATTR_SERVICE_NAME]).toBe("web-next");
|
||||
expect(r.attributes[ATTR_SERVICE_VERSION]).toBe("1.0.0");
|
||||
expect(r.attributes[ATTR_DEPLOYMENT_ENVIRONMENT_NAME]).toBe("production");
|
||||
});
|
||||
|
||||
it("populates namespace when provided", () => {
|
||||
const r = buildResource({
|
||||
serviceName: "web-next",
|
||||
environment: "production",
|
||||
namespace: "template-vertical",
|
||||
});
|
||||
expect(r.attributes["service.namespace"]).toBe("template-vertical");
|
||||
});
|
||||
|
||||
it("omits version and namespace when not provided", () => {
|
||||
const r = buildResource({
|
||||
serviceName: "web-next",
|
||||
environment: "production",
|
||||
});
|
||||
expect(r.attributes[ATTR_SERVICE_VERSION]).toBeUndefined();
|
||||
expect(r.attributes["service.namespace"]).toBeUndefined();
|
||||
});
|
||||
});
|
||||
22
packages/core-shared/src/instrumentation/otel/resource.ts
Normal file
22
packages/core-shared/src/instrumentation/otel/resource.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Resource } from "@opentelemetry/resources";
|
||||
|
||||
export type BuildResourceOpts = {
|
||||
serviceName: string;
|
||||
serviceVersion?: string;
|
||||
environment: string;
|
||||
namespace?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds an OpenTelemetry Resource with semantic-convention attributes.
|
||||
* Each app constructs its own resource at startup (per-app service name).
|
||||
*/
|
||||
export function buildResource(opts: BuildResourceOpts): Resource {
|
||||
const attrs: Record<string, string> = {
|
||||
"service.name": opts.serviceName,
|
||||
"deployment.environment.name": opts.environment,
|
||||
};
|
||||
if (opts.serviceVersion) attrs["service.version"] = opts.serviceVersion;
|
||||
if (opts.namespace) attrs["service.namespace"] = opts.namespace;
|
||||
return new Resource(attrs);
|
||||
}
|
||||
Reference in New Issue
Block a user