Skip to content

Commit 99cce63

Browse files
committed
refactor: set default service name in utility class
Previously the Metric and Tracer classes did not set a service name if one was not provided. There was no default service name. This commit sets a default service name in the Utility class, and updates the Tracer, Metric and Logger classes to use it.
1 parent 72c8e35 commit 99cce63

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

packages/commons/src/Utility.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
* ```
5757
*/
5858
export class Utility {
59-
6059
private coldStart: boolean = true;
60+
private readonly defaultServiceName: string = 'service_undefined';
6161

6262
public getColdStart(): boolean {
6363
if (this.coldStart) {
@@ -69,6 +69,10 @@ export class Utility {
6969
return false;
7070
}
7171

72+
public getDefaultServiceName(): string {
73+
return this.defaultServiceName;
74+
}
75+
7276
public isColdStart(): boolean {
7377
return this.getColdStart();
7478
}

packages/logger/src/Logger.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ class Logger extends Utility implements ClassThatLogs {
118118

119119
private static readonly defaultLogLevel: LogLevel = 'INFO';
120120

121-
private static readonly defaultServiceName: string = 'service_undefined';
122-
123121
// envVarsService is always initialized in the constructor in setOptions()
124122
private envVarsService!: EnvironmentVariablesService;
125123

@@ -786,7 +784,7 @@ class Logger extends Utility implements ClassThatLogs {
786784
this.getEnvVarsService().getCurrentEnvironment(),
787785
sampleRateValue: this.getSampleRateValue(),
788786
serviceName:
789-
serviceName || this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName() || Logger.defaultServiceName,
787+
serviceName || this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName() || this.getDefaultServiceName(),
790788
},
791789
persistentLogAttributes,
792790
);

packages/metrics/src/Metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class Metrics extends Utility implements MetricsInterface {
473473
private setService(service: string | undefined): void {
474474
const targetService = (service ||
475475
this.getCustomConfigService()?.getServiceName() ||
476-
this.getEnvVarsService().getServiceName()) as string;
476+
this.getEnvVarsService().getServiceName()) as string || this.getDefaultServiceName();
477477
if (targetService.length > 0) {
478478
this.setDefaultDimensions({ service: targetService });
479479
}

packages/tracer/src/Tracer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Tracer extends Utility implements TracerInterface {
128128
// envVarsService is always initialized in the constructor in setOptions()
129129
private envVarsService!: EnvironmentVariablesService;
130130

131-
private serviceName?: string;
131+
private serviceName!: string;
132132

133133
private tracingEnabled: boolean = true;
134134

@@ -189,7 +189,7 @@ class Tracer extends Utility implements TracerInterface {
189189
*
190190
*/
191191
public addServiceNameAnnotation(): void {
192-
if (!this.isTracingEnabled() || this.serviceName === undefined) {
192+
if (!this.isTracingEnabled()) {
193193
return;
194194
}
195195
this.putAnnotation('Service', this.serviceName);
@@ -836,6 +836,7 @@ class Tracer extends Utility implements TracerInterface {
836836

837837
return;
838838
}
839+
this.serviceName = this.getDefaultServiceName();
839840
}
840841

841842
/**

0 commit comments

Comments
 (0)