Skip to content

Commit 4f60d99

Browse files
authored
Merge pull request #322 from Scale3-Labs/release-2.3.2
Release 2.3.2
2 parents ab9d7b1 + c99d4d8 commit 4f60d99

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

src/langtrace_python_sdk/extensions/langtrace_exporter.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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\n2. 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+
)

src/langtrace_python_sdk/langtrace.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ def init(
8080
sys.stdout = open(os.devnull, "w")
8181

8282
host = (
83-
os.environ.get("LANGTRACE_API_HOST", None) or api_host or LANGTRACE_REMOTE_URL
83+
os.environ.get("LANGTRACE_API_HOST", None)
84+
or os.environ.get("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", None)
85+
or os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", None)
86+
or api_host
87+
or LANGTRACE_REMOTE_URL
8488
)
8589
check_if_sdk_is_outdated()
8690
print(Fore.GREEN + "Initializing Langtrace SDK.." + Fore.RESET)
87-
print(Fore.WHITE + "⭐ Leave our github a star to stay on top of our updates - https://github.com/Scale3-Labs/langtrace" + Fore.RESET)
91+
print(
92+
Fore.WHITE
93+
+ "⭐ Leave our github a star to stay on top of our updates - https://github.com/Scale3-Labs/langtrace"
94+
+ Fore.RESET
95+
)
8896
sampler = LangtraceSampler(disabled_methods=disable_tracing_for_functions)
8997
resource = Resource.create(
9098
attributes={
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.3.1"
1+
__version__ = "2.3.2"

0 commit comments

Comments
 (0)