Skip to content

refactor(tracer): replace class-based env access with functional helpers #4146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 50 additions & 52 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import type { Handler } from 'aws-lambda';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
import xraySdk from 'aws-xray-sdk-core';
import {
type EnvironmentVariablesService,
environmentVariablesService,
} from './config/EnvironmentVariablesService.js';
getStringFromEnv,
getServiceName,
getXRayTraceIdFromEnv,
isRequestXRaySampled,
} from '@aws-lambda-powertools/commons/utils/env';
import { ProviderService } from './provider/ProviderService.js';
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
import type { ProviderServiceInterface } from './types/ProviderService.js';
Expand Down Expand Up @@ -171,11 +173,6 @@ class Tracer extends Utility implements TracerInterface {
*/
private customConfigService?: ConfigServiceInterface;

/**
* The environment variables service used by the Tracer, is always initialized in the constructor in setOptions().
*/
private envVarsService!: EnvironmentVariablesService;

// serviceName is always initialized in the constructor in setOptions()
/**
* The name of the service, is always initialized in the constructor in setOptions().
Expand All @@ -187,9 +184,31 @@ class Tracer extends Utility implements TracerInterface {
*/
private tracingEnabled = true;

// Cache environment variables once for performance and clarity
readonly #envConfig: {
awsExecutionEnv: string;
samLocal: string;
captureError: string;
captureHTTPsRequests: string;
captureResponse: string;
tracingEnabled: string;
serviceName: string;
xrayTraceId: string;
} = {
awsExecutionEnv: '',
samLocal: '',
captureError: '',
captureHTTPsRequests: '',
captureResponse: '',
tracingEnabled: '',
serviceName: '',
xrayTraceId: '',
};

public constructor(options: TracerOptions = {}) {
super();

this.#setEnvConfig();
this.setOptions(options);
this.provider = new ProviderService();
if (this.isTracingEnabled() && this.captureHTTPsRequests) {
Expand Down Expand Up @@ -576,10 +595,9 @@ class Tracer extends Utility implements TracerInterface {
* };
* }
* }
* ```
*/
public getRootXrayTraceId(): string | undefined {
return this.envVarsService.getXrayTraceId();
return this.#envConfig.xrayTraceId;
}

/**
Expand All @@ -600,7 +618,6 @@ class Tracer extends Utility implements TracerInterface {
* const currentSegment = tracer.getSegment();
* ... // Do something with segment
* }
* ```
*/
public getSegment(): Segment | Subsegment | undefined {
if (!this.isTracingEnabled()) {
Expand All @@ -626,7 +643,7 @@ class Tracer extends Utility implements TracerInterface {
public isTraceSampled(): boolean {
if (!this.isTracingEnabled()) return false;

return this.envVarsService.getXrayTraceSampled();
return isRequestXRaySampled();
}

/**
Expand Down Expand Up @@ -731,39 +748,28 @@ class Tracer extends Utility implements TracerInterface {
return this.customConfigService;
}

/**
* Get for `envVarsService`.
* Used internally during initialization.
*/
private getEnvVarsService(): EnvironmentVariablesService {
return this.envVarsService;
}

/**
* Determine if we are running inside an Amplify CLI process.
* Used internally during initialization.
*/
private isAmplifyCli(): boolean {
return (
this.getEnvVarsService().getAwsExecutionEnv() ===
'AWS_Lambda_amplify-mock'
);
return this.#envConfig.awsExecutionEnv === 'AWS_Lambda_amplify-mock';
}

/**
* Determine if we are running in a Lambda execution environment.
* Used internally during initialization.
*/
private isLambdaExecutionEnv(): boolean {
return this.getEnvVarsService().getAwsExecutionEnv() !== '';
return this.#envConfig.awsExecutionEnv !== '';
}

/**
* Determine if we are running inside a SAM CLI process.
* Used internally during initialization.
*/
private isLambdaSamCli(): boolean {
return this.getEnvVarsService().getSamLocal() !== '';
return this.#envConfig.samLocal !== '';
}

/**
Expand All @@ -778,14 +784,11 @@ class Tracer extends Utility implements TracerInterface {
customConfigValue.toLowerCase() === 'false'
) {
this.captureError = false;

return;
}

const envVarsValue = this.getEnvVarsService().getTracingCaptureError();
if (envVarsValue.toLowerCase() === 'false') {
if (this.#envConfig.captureError.toLowerCase() === 'false') {
this.captureError = false;

return;
}
}
Expand All @@ -803,7 +806,6 @@ class Tracer extends Utility implements TracerInterface {
private setCaptureHTTPsRequests(enabled?: boolean): void {
if (enabled !== undefined && !enabled) {
this.captureHTTPsRequests = false;

return;
}

Expand All @@ -814,14 +816,11 @@ class Tracer extends Utility implements TracerInterface {
customConfigValue.toLowerCase() === 'false'
) {
this.captureHTTPsRequests = false;

return;
}

const envVarsValue = this.getEnvVarsService().getCaptureHTTPsRequests();
if (envVarsValue.toLowerCase() === 'false') {
if (this.#envConfig.captureHTTPsRequests.toLowerCase() === 'false') {
this.captureHTTPsRequests = false;

return;
}
}
Expand All @@ -838,14 +837,11 @@ class Tracer extends Utility implements TracerInterface {
customConfigValue.toLowerCase() === 'false'
) {
this.captureResponse = false;

return;
}

const envVarsValue = this.getEnvVarsService().getTracingCaptureResponse();
if (envVarsValue.toLowerCase() === 'false') {
if (this.#envConfig.captureResponse.toLowerCase() === 'false') {
this.captureResponse = false;

return;
}
}
Expand Down Expand Up @@ -874,7 +870,6 @@ class Tracer extends Utility implements TracerInterface {
const { enabled, serviceName, captureHTTPsRequests, customConfigService } =
options;

this.envVarsService = environmentVariablesService;
this.setCustomConfigService(customConfigService);
this.setTracingEnabled(enabled);
this.setCaptureResponse();
Expand All @@ -894,7 +889,6 @@ class Tracer extends Utility implements TracerInterface {
private setServiceName(serviceName?: string): void {
if (serviceName !== undefined && this.isValidServiceName(serviceName)) {
this.serviceName = serviceName;

return;
}

Expand All @@ -904,14 +898,11 @@ class Tracer extends Utility implements TracerInterface {
this.isValidServiceName(customConfigValue)
) {
this.serviceName = customConfigValue;

return;
}

const envVarsValue = this.getEnvVarsService().getServiceName();
if (envVarsValue !== undefined && this.isValidServiceName(envVarsValue)) {
this.serviceName = envVarsValue;

if (this.#envConfig.serviceName !== undefined && this.isValidServiceName(this.#envConfig.serviceName)) {
this.serviceName = this.#envConfig.serviceName;
return;
}
this.serviceName = this.defaultServiceName;
Expand All @@ -926,7 +917,6 @@ class Tracer extends Utility implements TracerInterface {
private setTracingEnabled(enabled?: boolean): void {
if (enabled !== undefined && !enabled) {
this.tracingEnabled = enabled;

return;
}

Expand All @@ -937,17 +927,13 @@ class Tracer extends Utility implements TracerInterface {
customConfigValue.toLowerCase() === 'false'
) {
this.tracingEnabled = false;

return;
}

const envVarsValue = this.getEnvVarsService().getTracingEnabled();
if (envVarsValue.toLowerCase() === 'false') {
if (this.#envConfig.tracingEnabled.toLowerCase() === 'false') {
this.tracingEnabled = false;

return;
}

if (
this.isAmplifyCli() ||
this.isLambdaSamCli() ||
Expand All @@ -956,6 +942,18 @@ class Tracer extends Utility implements TracerInterface {
this.tracingEnabled = false;
}
}

// Populate #envConfig with all relevant environment variables
#setEnvConfig(): void {
this.#envConfig.awsExecutionEnv = getStringFromEnv({ key: 'AWS_EXECUTION_ENV', defaultValue: '' });
this.#envConfig.samLocal = getStringFromEnv({ key: 'AWS_SAM_LOCAL', defaultValue: '' });
this.#envConfig.captureError = getStringFromEnv({ key: 'POWERTOOLS_TRACER_CAPTURE_ERROR', defaultValue: '' });
this.#envConfig.captureHTTPsRequests = getStringFromEnv({ key: 'POWERTOOLS_TRACER_CAPTURE_HTTPS_REQUESTS', defaultValue: '' });
this.#envConfig.captureResponse = getStringFromEnv({ key: 'POWERTOOLS_TRACER_CAPTURE_RESPONSE', defaultValue: '' });
this.#envConfig.tracingEnabled = getStringFromEnv({ key: 'POWERTOOLS_TRACE_ENABLED', defaultValue: '' });
this.#envConfig.serviceName = getServiceName();
this.#envConfig.xrayTraceId = getXRayTraceIdFromEnv() || '';
}
}

export { Tracer };
74 changes: 0 additions & 74 deletions packages/tracer/src/config/EnvironmentVariablesService.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/tracer/src/provider/ProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import http from 'node:http';
import https from 'node:https';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
import type { DiagnosticsChannel } from 'undici-types';
import { environmentVariablesService } from '../config/EnvironmentVariablesService.js';
import { getXRayTraceIdFromEnv } from '@aws-lambda-powertools/commons/utils/env';
import {
findHeaderAndDecode,
getRequestURL,
Expand Down Expand Up @@ -132,7 +132,7 @@ class ProviderService implements ProviderServiceInterface {
// @ts-expect-error
request.addHeader(
'X-Amzn-Trace-Id',
`Root=${environmentVariablesService.getXrayTraceId()};Parent=${subsegment.id};Sampled=${subsegment.notTraced ? '0' : '1'}`
`Root=${getXRayTraceIdFromEnv()};Parent=${subsegment.id};Sampled=${subsegment.notTraced ? '0' : '1'}`
);

(subsegment as HttpSubsegment).http = {
Expand Down
Loading