@@ -25,10 +25,17 @@ import {
2525} from '@opentelemetry/api' ;
2626
2727import { Span } from './span' ;
28- import { Attributes , TraceUtil } from './trace-util' ;
28+ import { ATTRIBUTE_SETTINGS_PREFIX , Attributes , TraceUtil } from './trace-util' ;
29+
30+ import { interfaces } from '../v1/firestore_client_config.json' ;
31+ import { FirestoreClient } from '../v1' ;
32+ import { DEFAULT_DATABASE_ID } from '../path' ;
33+ import { DEFAULT_MAX_IDLE_CHANNELS } from '../index' ;
34+ const serviceConfig = interfaces [ 'google.firestore.v1.Firestore' ] ;
2935
3036export class EnabledTraceUtil implements TraceUtil {
3137 private tracer : Tracer ;
38+ private settingsAttributes : Attributes ;
3239
3340 constructor ( settings : Settings ) {
3441 let tracerProvider = settings . openTelemetryOptions ?. tracerProvider ;
@@ -42,6 +49,85 @@ export class EnabledTraceUtil implements TraceUtil {
4249 const libVersion = require ( '../../../package.json' ) . version ;
4350 const libName = require ( '../../../package.json' ) . name ;
4451 this . tracer = tracerProvider . getTracer ( libName , libVersion ) ;
52+
53+ this . settingsAttributes = { } ;
54+ this . settingsAttributes [ 'otel.scope.name' ] = libName ;
55+ this . settingsAttributes [ 'otel.scope.version' ] = libVersion ;
56+
57+ if ( settings . projectId ) {
58+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .project_id` ] =
59+ settings . projectId ;
60+ }
61+
62+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .database_id` ] =
63+ settings . databaseId || DEFAULT_DATABASE_ID ;
64+
65+ const host =
66+ settings . servicePath ?? settings . host ?? 'firestore.googleapis.com' ;
67+ const port = settings . port ?? FirestoreClient . port ;
68+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .host` ] =
69+ `${ host } :${ port } ` ;
70+
71+ if ( settings . preferRest !== undefined ) {
72+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .prefer_REST` ] =
73+ settings . preferRest ;
74+ }
75+
76+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .max_idle_channels` ] =
77+ settings . maxIdleChannels ?? DEFAULT_MAX_IDLE_CHANNELS ;
78+
79+ const defaultRetrySettings = serviceConfig . retry_params . default ;
80+ const customRetrySettings =
81+ settings . clientConfig ?. interfaces ?. [ 'google.firestore.v1.Firestore' ] ?. [
82+ 'retry_params'
83+ ] ?. [ 'default' ] ;
84+ this . settingsAttributes [
85+ `${ ATTRIBUTE_SETTINGS_PREFIX } .initial_retry_delay`
86+ ] = this . millisToSecondString (
87+ customRetrySettings ?. initial_retry_delay_millis ??
88+ defaultRetrySettings . initial_retry_delay_millis
89+ ) ;
90+ this . settingsAttributes [
91+ `${ ATTRIBUTE_SETTINGS_PREFIX } .initial_rpc_timeout`
92+ ] = this . millisToSecondString (
93+ customRetrySettings ?. initial_rpc_timeout_millis ??
94+ defaultRetrySettings . initial_rpc_timeout_millis
95+ ) ;
96+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .total_timeout` ] =
97+ this . millisToSecondString (
98+ customRetrySettings ?. total_timeout_millis ??
99+ defaultRetrySettings . total_timeout_millis
100+ ) ;
101+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .max_retry_delay` ] =
102+ this . millisToSecondString (
103+ customRetrySettings ?. max_retry_delay_millis ??
104+ defaultRetrySettings . max_retry_delay_millis
105+ ) ;
106+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .max_rpc_timeout` ] =
107+ this . millisToSecondString (
108+ customRetrySettings ?. max_rpc_timeout_millis ??
109+ defaultRetrySettings . max_rpc_timeout_millis
110+ ) ;
111+ this . settingsAttributes [
112+ `${ ATTRIBUTE_SETTINGS_PREFIX } .retry_delay_multiplier`
113+ ] =
114+ customRetrySettings ?. retry_delay_multiplier . toString ( ) ??
115+ defaultRetrySettings . retry_delay_multiplier . toString ( ) ;
116+ this . settingsAttributes [
117+ `${ ATTRIBUTE_SETTINGS_PREFIX } .rpc_timeout_multiplier`
118+ ] =
119+ customRetrySettings ?. rpc_timeout_multiplier . toString ( ) ??
120+ defaultRetrySettings . rpc_timeout_multiplier . toString ( ) ;
121+ }
122+
123+ recordProjectId ( projectId : string ) : void {
124+ this . settingsAttributes [ `${ ATTRIBUTE_SETTINGS_PREFIX } .project_id` ] =
125+ projectId ;
126+ this . currentSpan ( ) . setAttributes ( this . settingsAttributes ) ;
127+ }
128+
129+ private millisToSecondString ( millis : number ) : string {
130+ return `${ millis / 1000 } s` ;
45131 }
46132
47133 private endSpan ( otelSpan : OpenTelemetrySpan , error : Error ) : void {
@@ -64,6 +150,8 @@ export class EnabledTraceUtil implements TraceUtil {
64150 attributes : attributes ,
65151 } ,
66152 ( otelSpan : OpenTelemetrySpan ) => {
153+ this . addCommonAttributes ( otelSpan ) ;
154+
67155 // Note that if `fn` returns a `Promise`, we want the otelSpan to end
68156 // after the `Promise` has resolved, NOT after the `fn` has returned.
69157 // Therefore, we should not use a `finally` clause to end the otelSpan.
@@ -94,10 +182,16 @@ export class EnabledTraceUtil implements TraceUtil {
94182 }
95183
96184 startSpan ( name : string ) : Span {
97- return new Span ( this . tracer . startSpan ( name , undefined , context . active ( ) ) ) ;
185+ const otelSpan = this . tracer . startSpan ( name , undefined , context . active ( ) ) ;
186+ this . addCommonAttributes ( otelSpan ) ;
187+ return new Span ( otelSpan ) ;
98188 }
99189
100190 currentSpan ( ) : Span {
101191 return new Span ( trace . getActiveSpan ( ) ) ;
102192 }
193+
194+ addCommonAttributes ( otelSpan : OpenTelemetrySpan ) : void {
195+ otelSpan . setAttributes ( this . settingsAttributes ) ;
196+ }
103197}
0 commit comments