@@ -19,7 +19,6 @@ import {AxiosError} from 'axios';
1919import * as gcpMetadata from 'gcp-metadata' ;
2020import { OutgoingHttpHeaders } from 'http' ;
2121import * as os from 'os' ;
22- import * as util from 'util' ;
2322
2423import { Constants } from './constants' ;
2524import { SpanKind , Trace } from './trace' ;
@@ -28,6 +27,9 @@ import {Singleton} from './util';
2827
2928const pjson = require ( '../../package.json' ) ;
3029
30+ // TODO(kjin): This value should be exported from @g-c/c.
31+ const NO_PROJECT_ID_TOKEN = '{{projectId}}' ;
32+
3133const onUncaughtExceptionValues = [ 'ignore' , 'flush' , 'flushAndExit' ] ;
3234
3335const headers : OutgoingHttpHeaders = { } ;
@@ -52,9 +54,6 @@ export interface LabelObject { [key: string]: string; }
5254 * A class representing a service that publishes traces in the background.
5355 */
5456export class TraceWriter extends common . Service {
55- // TODO(kjin): Make public members private (they're public for testing)
56- private logger : common . Logger ;
57- private config : TraceWriterConfig ;
5857 /** Stringified traces to be published */
5958 buffer : string [ ] ;
6059 /** Default labels to be attached to written spans */
@@ -71,7 +70,9 @@ export class TraceWriter extends common.Service {
7170 * @param logger The Trace Agent's logger object.
7271 * @constructor
7372 */
74- constructor ( config : TraceWriterConfig , logger : common . Logger ) {
73+ constructor (
74+ private readonly config : TraceWriterConfig ,
75+ private readonly logger : common . Logger ) {
7576 super (
7677 {
7778 packageJson : pjson ,
@@ -82,9 +83,6 @@ export class TraceWriter extends common.Service {
8283 config ) ;
8384
8485 this . logger = logger ;
85- // Clone the config object
86- this . config = { ...config } ;
87- this . config . serviceContext = { ...this . config . serviceContext } ;
8886 this . buffer = [ ] ;
8987 this . defaultLabels = { } ;
9088
@@ -214,13 +212,13 @@ export class TraceWriter extends common.Service {
214212 }
215213
216214 getProjectId ( ) {
217- if ( this . config . projectId ) {
218- return Promise . resolve ( this . config . projectId ) ;
215+ // super.getProjectId writes to projectId, but doesn't check it first
216+ // before going through the flow of obtaining it. So we add that logic
217+ // first.
218+ if ( this . projectId !== NO_PROJECT_ID_TOKEN ) {
219+ return Promise . resolve ( this . projectId ) ;
219220 }
220- return super . getProjectId ( ) . then ( ( projectId ) => {
221- this . config . projectId = projectId ;
222- return projectId ;
223- } ) ;
221+ return super . getProjectId ( ) ;
224222 }
225223
226224 /**
@@ -269,8 +267,8 @@ export class TraceWriter extends common.Service {
269267 // Any test that doesn't mock the Trace Writer will assume that traces get
270268 // buffered synchronously. We need to refactor those tests to remove that
271269 // assumption before we can make this fix.
272- if ( this . config . projectId ) {
273- afterProjectId ( this . config . projectId ) ;
270+ if ( this . projectId !== NO_PROJECT_ID_TOKEN ) {
271+ afterProjectId ( this . projectId ) ;
274272 } else {
275273 this . getProjectId ( ) . then ( afterProjectId , ( err : Error ) => {
276274 // Because failing to get a project ID means that the trace agent will
@@ -329,7 +327,7 @@ export class TraceWriter extends common.Service {
329327 */
330328 publish ( json : string ) {
331329 const uri = `https://cloudtrace.googleapis.com/v1/projects/${
332- this . config . projectId } /traces`;
330+ this . projectId } /traces`;
333331 const options = { method : 'PATCH' , uri, body : json , headers} ;
334332 this . logger . info ( 'TraceWriter#publish: Publishing to ' + uri ) ;
335333 this . request ( options , ( err , body ?, response ?) => {
0 commit comments