11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { Logger } from "@opentelemetry/api" ;
5- import { ConsoleLogger , LogLevel , ExportResult , ExportResultCode } from "@opentelemetry/core" ;
4+ import { diag } from "@opentelemetry/api" ;
5+ import { ExportResult , ExportResultCode } from "@opentelemetry/core" ;
66import { ReadableSpan , SpanExporter } from "@opentelemetry/tracing" ;
77import { RestError } from "@azure/core-http" ;
88import { ConnectionStringParser } from "../utils/connectionStringParser" ;
@@ -21,8 +21,6 @@ import { readableSpanToEnvelope } from "../utils/spanUtils";
2121export class AzureMonitorTraceExporter implements SpanExporter {
2222 private readonly _persister : PersistentStorage ;
2323
24- private readonly _logger : Logger ;
25-
2624 private readonly _sender : Sender ;
2725
2826 private _retryTimer : NodeJS . Timer | null ;
@@ -31,14 +29,13 @@ export class AzureMonitorTraceExporter implements SpanExporter {
3129
3230 constructor ( options : AzureExporterConfig = { } ) {
3331 const connectionString = options . connectionString || process . env [ ENV_CONNECTION_STRING ] ;
34- this . _logger = new ConsoleLogger ( LogLevel . ERROR ) ;
3532 this . _options = {
3633 ...DEFAULT_EXPORTER_CONFIG
3734 } ;
3835 this . _options . apiVersion = options . apiVersion ?? this . _options . apiVersion ;
3936
4037 if ( connectionString ) {
41- const parsedConnectionString = ConnectionStringParser . parse ( connectionString , this . _logger ) ;
38+ const parsedConnectionString = ConnectionStringParser . parse ( connectionString ) ;
4239 this . _options . instrumentationKey =
4340 parsedConnectionString . instrumentationkey ?? this . _options . instrumentationKey ;
4441 this . _options . endpointUrl =
@@ -48,14 +45,14 @@ export class AzureMonitorTraceExporter implements SpanExporter {
4845 if ( ! this . _options . instrumentationKey ) {
4946 const message =
5047 "No instrumentation key or connection string was provided to the Azure Monitor Exporter" ;
51- this . _logger . error ( message ) ;
48+ diag . error ( message ) ;
5249 throw new Error ( message ) ;
5350 }
5451
5552 this . _sender = new HttpSender ( this . _options ) ;
5653 this . _persister = new FileSystemPersist ( this . _options ) ;
5754 this . _retryTimer = null ;
58- this . _logger . debug ( "AzureMonitorTraceExporter was successfully setup" ) ;
55+ diag . debug ( "AzureMonitorTraceExporter was successfully setup" ) ;
5956 }
6057
6158 private async _persist ( envelopes : unknown [ ] ) : Promise < ExportResult > {
@@ -73,7 +70,7 @@ export class AzureMonitorTraceExporter implements SpanExporter {
7370 }
7471
7572 private async exportEnvelopes ( envelopes : Envelope [ ] ) : Promise < ExportResult > {
76- this . _logger . info ( `Exporting ${ envelopes . length } envelope(s)` ) ;
73+ diag . info ( `Exporting ${ envelopes . length } envelope(s)` ) ;
7774
7875 try {
7976 const { result, statusCode } = await this . _sender . send ( envelopes ) ;
@@ -90,7 +87,7 @@ export class AzureMonitorTraceExporter implements SpanExporter {
9087 } else if ( statusCode && isRetriable ( statusCode ) ) {
9188 // Failed -- persist failed data
9289 if ( result ) {
93- this . _logger . info ( result ) ;
90+ diag . info ( result ) ;
9491 const breezeResponse = JSON . parse ( result ) as BreezeResponse ;
9592 const filteredEnvelopes = breezeResponse . errors . reduce (
9693 ( acc , v ) => [ ...acc , envelopes [ v . index ] ] ,
@@ -110,13 +107,13 @@ export class AzureMonitorTraceExporter implements SpanExporter {
110107 }
111108 } catch ( senderErr ) {
112109 if ( this . _isNetworkError ( senderErr ) ) {
113- this . _logger . error (
110+ diag . error (
114111 "Retrying due to transient client side error. Error message:" ,
115112 senderErr . message
116113 ) ;
117114 return await this . _persist ( envelopes ) ;
118115 } else {
119- this . _logger . error (
116+ diag . error (
120117 "Envelopes could not be exported and are not retriable. Error message:" ,
121118 senderErr . message
122119 ) ;
@@ -129,15 +126,15 @@ export class AzureMonitorTraceExporter implements SpanExporter {
129126 spans : ReadableSpan [ ] ,
130127 resultCallback : ( result : ExportResult ) => void
131128 ) : Promise < void > {
132- this . _logger . info ( `Exporting ${ spans . length } span(s). Converting to envelopes...` ) ;
129+ diag . info ( `Exporting ${ spans . length } span(s). Converting to envelopes...` ) ;
133130 const envelopes = spans . map ( ( span ) =>
134- readableSpanToEnvelope ( span , this . _options . instrumentationKey , this . _logger )
131+ readableSpanToEnvelope ( span , this . _options . instrumentationKey )
135132 ) ;
136133 resultCallback ( await this . exportEnvelopes ( envelopes ) ) ;
137134 }
138135
139136 async shutdown ( ) : Promise < void > {
140- this . _logger . info ( "Azure Monitor Trace Exporter shutting down" ) ;
137+ diag . info ( "Azure Monitor Trace Exporter shutting down" ) ;
141138 return this . _sender . shutdown ( ) ;
142139 }
143140
@@ -148,7 +145,7 @@ export class AzureMonitorTraceExporter implements SpanExporter {
148145 await this . _sender . send ( envelopes ) ;
149146 }
150147 } catch ( err ) {
151- this . _logger . warn ( `Failed to fetch persisted file` , err ) ;
148+ diag . warn ( `Failed to fetch persisted file` , err ) ;
152149 }
153150 }
154151
0 commit comments