From 002b61a022e0b2229641c3eac1017fda6c6bdf45 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Wed, 14 Dec 2022 11:10:38 -0500 Subject: [PATCH] feat: implement /detectResource e2e scenario (#468) --- e2e-test-server/package.json | 1 + e2e-test-server/src/scenarios.ts | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/e2e-test-server/package.json b/e2e-test-server/package.json index 1fa18852..cc5dd286 100644 --- a/e2e-test-server/package.json +++ b/e2e-test-server/package.json @@ -32,6 +32,7 @@ }, "dependencies": { "@google-cloud/opentelemetry-cloud-trace-exporter": "^1.2.0", + "@google-cloud/opentelemetry-resource-util": "^1.2.0", "@google-cloud/pubsub": "^3.0.0", "@grpc/grpc-js": "^1.3.2", "@opentelemetry/api": "^1.0.0", diff --git a/e2e-test-server/src/scenarios.ts b/e2e-test-server/src/scenarios.ts index 655e73af..f68548a5 100644 --- a/e2e-test-server/src/scenarios.ts +++ b/e2e-test-server/src/scenarios.ts @@ -17,10 +17,12 @@ import { Tracer, BasicTracerProvider, BatchSpanProcessor, + TracerConfig, } from '@opentelemetry/sdk-trace-base'; -import {AlwaysOnSampler} from '@opentelemetry/core'; -import {Resource} from '@opentelemetry/resources'; +import {AlwaysOnSampler} from '@opentelemetry/sdk-trace-base'; +import {Resource, envDetector, detectResources} from '@opentelemetry/resources'; import {TraceExporter} from '@google-cloud/opentelemetry-cloud-trace-exporter'; +import {GcpDetector} from '@google-cloud/opentelemetry-resource-util'; import * as constants from './constants'; import {context, SpanKind} from '@opentelemetry/api'; import {AsyncHooksContextManager} from '@opentelemetry/context-async-hooks'; @@ -39,10 +41,14 @@ export interface Response { context.setGlobalContextManager(new AsyncHooksContextManager()); -async function withTracer(f: (tracer: Tracer) => R): Promise { +async function withTracer( + f: (tracer: Tracer) => R, + tracerConfig?: TracerConfig +): Promise { const tracerProvider = new BasicTracerProvider({ sampler: new AlwaysOnSampler(), resource: Resource.EMPTY, + ...tracerConfig, }); tracerProvider.addSpanProcessor( new BatchSpanProcessor(new TraceExporter({projectId: constants.PROJECT_ID})) @@ -99,6 +105,24 @@ async function complexTrace(request: Request): Promise { }); } +async function detectResource(request: Request): Promise { + return await withTracer( + async (tracer: Tracer): Promise => { + const span = tracer.startSpan('resourceDetectionTrace', { + attributes: {[constants.TEST_ID]: request.testId}, + }); + const traceId = span.spanContext().traceId; + span.end(); + return {statusCode: Status.OK, headers: {[constants.TRACE_ID]: traceId}}; + }, + { + resource: await detectResources({ + detectors: [new GcpDetector(), envDetector], + }), + } + ); +} + async function notImplementedHandler(): Promise { return {statusCode: Status.UNIMPLEMENTED}; } @@ -109,6 +133,7 @@ const SCENARIO_TO_HANDLER: {[scenario: string]: ScenarioHandler} = { '/health': health, '/basicTrace': basicTrace, '/complexTrace': complexTrace, + '/detectResource': detectResource, } as const; export function getScenarioHandler(scenario: string): ScenarioHandler {