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

Merged
Merged
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
103 changes: 63 additions & 40 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import type {
HandlerMethodDecorator,
SyncHandler,
} from '@aws-lambda-powertools/commons/types';
import {
getServiceName,
getStringFromEnv,
getXRayTraceIdFromEnv,
isRequestXRaySampled,
} from '@aws-lambda-powertools/commons/utils/env';
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';
import { ProviderService } from './provider/ProviderService.js';
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
import type { ProviderServiceInterface } from './types/ProviderService.js';
Expand Down Expand Up @@ -174,11 +176,18 @@ 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().
* Cache environment variables once at init time.
*/
private envVarsService!: EnvironmentVariablesService;
readonly #envConfig = {
awsExecutionEnv: '',
samLocal: '',
captureError: '',
captureHTTPsRequests: '',
captureResponse: '',
tracingEnabled: '',
serviceName: '',
};

// serviceName is always initialized in the constructor in setOptions()
/**
* The name of the service, is always initialized in the constructor in setOptions().
*/
Expand All @@ -192,6 +201,7 @@ class Tracer extends Utility implements TracerInterface {
public constructor(options: TracerOptions = {}) {
super();

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

/**
Expand Down Expand Up @@ -628,7 +638,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 @@ -733,39 +743,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 @@ -784,10 +783,8 @@ class Tracer extends Utility implements TracerInterface {
return;
}

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

return;
}
}
Expand Down Expand Up @@ -820,10 +817,8 @@ class Tracer extends Utility implements TracerInterface {
return;
}

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

return;
}
}
Expand All @@ -844,10 +839,8 @@ class Tracer extends Utility implements TracerInterface {
return;
}

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

return;
}
}
Expand Down Expand Up @@ -876,7 +869,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 Down Expand Up @@ -910,10 +902,11 @@ class Tracer extends Utility implements TracerInterface {
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 Down Expand Up @@ -943,10 +936,8 @@ class Tracer extends Utility implements TracerInterface {
return;
}

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

return;
}

Expand All @@ -958,6 +949,38 @@ class Tracer extends Utility implements TracerInterface {
this.tracingEnabled = false;
}
}

/**
* Set environment variables for the tracer.
* This method is called during initialization to ensure environment variables are available.
*/
#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();
}
}

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 @@ -28,7 +28,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 @@ -134,7 +134,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