@@ -76,7 +76,29 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
7676 Returns:
7777 The result of the export SUCCESS or FAILURE
7878 """
79- if not self .api_key and not self .disable_logging :
79+ headers = {
80+ "Content-Type" : "application/json" ,
81+ "x-api-key" : self .api_key ,
82+ "User-Agent" : "LangtraceExporter" ,
83+ }
84+
85+ # Check if the OTEL_EXPORTER_OTLP_HEADERS environment variable is set
86+ otel_headers = os .getenv ("OTEL_EXPORTER_OTLP_HEADERS" , None )
87+ if otel_headers :
88+ # Parse and add to headers
89+ for header in otel_headers .split ("," ):
90+ key , value = header .split ("=" )
91+ headers [key .strip ()] = value .strip ()
92+
93+ # Check if the OTEL_EXPORTER_OTLP_TRACES_HEADERS environment variable is set
94+ otel_traces_headers = os .getenv ("OTEL_EXPORTER_OTLP_TRACES_HEADERS" , None )
95+ if otel_traces_headers :
96+ # Parse and add to headers
97+ for header in otel_traces_headers .split ("," ):
98+ key , value = header .split ("=" )
99+ headers [key .strip ()] = value .strip ()
100+
101+ if not headers ["x-api-key" ] and not self .disable_logging :
80102 print (Fore .RED )
81103 print (
82104 "Missing Langtrace API key, proceed to https://langtrace.ai to create one"
@@ -101,26 +123,42 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
101123 response = requests .post (
102124 url = f"{ self .api_host } " ,
103125 data = json .dumps (data ),
104- headers = {
105- "Content-Type" : "application/json" ,
106- "x-api-key" : self .api_key ,
107- "User-Agent" : "LangtraceExporter" ,
108- },
126+ headers = headers ,
109127 timeout = 20 ,
110128 )
111129
112130 if not response .ok :
113131 raise RequestException (response .text )
114132 if not self .disable_logging :
115133 print (
116- Fore .GREEN + f"Exported { len (spans )} spans successfully." + Fore .RESET
134+ Fore .GREEN
135+ + f"Exported { len (spans )} spans successfully."
136+ + Fore .RESET
117137 )
118138 return SpanExportResult .SUCCESS
119139 except RequestException as err :
120140 if not self .disable_logging :
121141 print (Fore .RED + "Failed to export spans." )
122- print (Fore .RED + f"Error: { err } " + Fore .RESET )
142+ print (Fore .RED + f"Error: { err } \r \n " + Fore .RESET )
143+ if (
144+ "invalid api key" in str (err ).lower ()
145+ and self .api_host == f"{ LANGTRACE_REMOTE_URL } /api/trace"
146+ ):
147+ print (
148+ Fore .YELLOW
149+ + "If you're self-hosting Langtrace, make sure to do one of the following to configure your trace endpoint (e.g., http://localhost:3000/api/trace):"
150+ + Fore .YELLOW
151+ )
152+ print (
153+ Fore .YELLOW
154+ + "1. Set the `LANGTRACE_API_HOST` environment variable, or\r \n 2. Pass the `api_host` parameter to the `langtrace.init()` method.\r \n "
155+ + Fore .YELLOW
156+ )
123157 return SpanExportResult .FAILURE
124158
125159 def shutdown (self ) -> None :
126- print (Fore .WHITE + "⭐ Leave our github a star to stay on top of our updates - https://github.com/Scale3-Labs/langtrace" + Fore .RESET )
160+ print (
161+ Fore .WHITE
162+ + "⭐ Leave our github a star to stay on top of our updates - https://github.com/Scale3-Labs/langtrace"
163+ + Fore .RESET
164+ )
0 commit comments