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);
}
