22# SPDX-License-Identifier: Apache-2.0
33
44import gzip
5- import itertools
65import logging
76from io import BytesIO
87from time import sleep
9- from typing import Dict , Iterator , Mapping , Optional , Sequence
8+ from typing import Dict , Mapping , Optional , Sequence
109
1110import requests
1211
2625
2726
2827class OTLPAwsLogExporter (OTLPLogExporter ):
28+ COUNT = 0
2929 _LARGE_LOG_HEADER = {"x-aws-log-semantics" : "otel" }
3030 _RETRY_AFTER_HEADER = "Retry-After" # https://opentelemetry.io/docs/specs/otlp/#otlphttp-throttling
3131
@@ -62,7 +62,8 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
6262
6363 print (f"Exporting batch of { len (batch )} logs" )
6464 print ("TOTAL DATA SIZE " + str (sum (self ._get_size_of_log (logz ) for logz in batch )))
65- print ("GEN_AI_FLAG " + str (self ._gen_ai_flag ))
65+ self .COUNT += len (batch )
66+ print ("COUNT " + str (self .COUNT ))
6667
6768 """
6869 Exports the given batch of OTLP log data.
@@ -74,8 +75,8 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
7475 - inject the 'x-aws-log-semantics' flag into the header.
7576
7677 3. Retry behavior is now the following:
77- - if the response contains a status code that is retryable and the response contains Retry-After in its headers,
78- the serialized data will be exported after that set delay
78+ - if the response contains a status code that is retryable and the response contains Retry-After in its
79+ headers, the serialized data will be exported after that set delay
7980
8081 - if the response does not contain that Retry-After header, default back to the current iteration of the
8182 exponential backoff delay
@@ -93,11 +94,18 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
9394
9495 data = gzip_data .getvalue ()
9596
96- backoff = _create_exp_backoff_generator (self ._MAX_RETRY_TIMEOUT )
97+ backoff = _create_exp_backoff_generator (max_value = self ._MAX_RETRY_TIMEOUT )
9798
9899 while True :
99100 resp = self ._send (data )
100101
102+ print (f"Response status: { resp .status_code } " )
103+ print (f"Response headers: { resp .headers } " )
104+ try :
105+ print (f"Response body: { resp .text } " )
106+ except :
107+ print ("Could not print response body" )
108+
101109 if resp .ok :
102110 return LogExportResult .SUCCESS
103111
@@ -111,13 +119,18 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
111119 return LogExportResult .FAILURE
112120
113121 # https://opentelemetry.io/docs/specs/otlp/#otlphttp-throttling
114- retry_after = resp .headers .get (self ._RETRY_AFTER_HEADER , None )
122+ maybe_retry_after = resp .headers .get (self ._RETRY_AFTER_HEADER , None )
115123
116124 # Set the next retry delay to the value of the Retry-After response in the headers.
117- # If Retry-After is not present in the headers, default to the next iteration of the exponential backoff strategy.
118- delay = next (backoff , - 1 ) if retry_after == None else self ._parse_retryable_header (retry_after )
125+ # If Retry-After is not present in the headers, default to the next iteration of the
126+ # exponential backoff strategy.
127+
128+ delay = self ._parse_retryable_header (maybe_retry_after )
119129
120130 if delay == - 1 :
131+ delay = next (backoff , self ._MAX_RETRY_TIMEOUT )
132+
133+ if delay == self ._MAX_RETRY_TIMEOUT :
121134 _logger .error (
122135 "Transient error %s encountered while exporting logs batch. "
123136 "No Retry-After header found and all backoff retries exhausted. "
@@ -134,7 +147,7 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
134147 )
135148
136149 sleep (delay )
137-
150+
138151 def set_gen_ai_flag (self ):
139152 """
140153 Sets the gen_ai flag to true to signal injecting the LLO flag to the headers of the export request.
@@ -171,10 +184,15 @@ def _retryable(resp: requests.Response) -> bool:
171184
172185 return OTLPLogExporter ._retryable (resp )
173186
174- def _parse_retryable_header (self , retry_header : str ) -> float :
187+ def _parse_retryable_header (self , retry_header : Optional [ str ] ) -> float :
175188 """
176- Converts the given retryable header into a delay in seconds, returns -1 if there's an error with the parsing
189+ Converts the given retryable header into a delay in seconds, returns -1 if there's no header
190+ or error with the parsing
177191 """
192+
193+ if not retry_header :
194+ return - 1
195+
178196 try :
179197 return float (retry_header )
180198 except ValueError :
0 commit comments