5
5
import random
6
6
import threading
7
7
import time
8
+ from functools import cached_property
8
9
from typing import Any
9
10
10
11
import httpx
@@ -50,9 +51,9 @@ def __init__(
50
51
base_delay: Base delay (in seconds) for the first backoff.
51
52
max_delay: Maximum delay (in seconds) for backoff growth.
52
53
"""
53
- self .api_key = api_key or os . environ . get ( "OPENAI_API_KEY" )
54
- self .organization = organization or os . environ . get ( "OPENAI_ORG_ID" )
55
- self .project = project or os . environ . get ( "OPENAI_PROJECT_ID" )
54
+ self ._api_key = api_key
55
+ self ._organization = organization
56
+ self ._project = project
56
57
self .endpoint = endpoint
57
58
self .max_retries = max_retries
58
59
self .base_delay = base_delay
@@ -61,14 +62,30 @@ def __init__(
61
62
# Keep a client open for connection pooling across multiple export calls
62
63
self ._client = httpx .Client (timeout = httpx .Timeout (timeout = 60 , connect = 5.0 ))
63
64
65
+ self ._lazy_read_complete = False
66
+
64
67
def set_api_key (self , api_key : str ):
65
68
"""Set the OpenAI API key for the exporter.
66
69
67
70
Args:
68
71
api_key: The OpenAI API key to use. This is the same key used by the OpenAI Python
69
72
client.
70
73
"""
71
- self .api_key = api_key
74
+ # Reset the cached property
75
+ del self .api_key
76
+ self ._api_key = api_key
77
+
78
+ @cached_property
79
+ def api_key (self ):
80
+ return self ._api_key or os .environ .get ("OPENAI_API_KEY" )
81
+
82
+ @cached_property
83
+ def organization (self ):
84
+ return self ._organization or os .environ .get ("OPENAI_ORG_ID" )
85
+
86
+ @cached_property
87
+ def project (self ):
88
+ return self ._project or os .environ .get ("OPENAI_PROJECT_ID" )
72
89
73
90
def export (self , items : list [Trace | Span [Any ]]) -> None :
74
91
if not items :
0 commit comments