22import sys
33from deprecated import deprecated
44import requests
5+ from pathlib import Path
56
67from typing import Optional
78from colorama import Fore
2425
2526
2627class Traceloop :
28+ AUTO_CREATED_KEY_PATH = str (
29+ Path .home () / ".cache" / "traceloop" / "auto_created_key"
30+ )
31+ AUTO_CREATED_URL = str (Path .home () / ".cache" / "traceloop" / "auto_created_url" )
32+
2733 __tracer_wrapper : TracerWrapper
2834
2935 @staticmethod
@@ -77,12 +83,12 @@ def init(
7783 and not api_key
7884 ):
7985 headers = None # disable headers if we're auto-creating a dashboard
80- if os .path .exists ("/tmp/traceloop_key.txt" ) and os . path . exists (
81- "/tmp/traceloop_url.txt"
82- ):
83- api_key = open ( "/tmp/traceloop_key.txt" ). read ( )
84- access_url = open ( "/tmp/traceloop_url.txt" ). read ( )
85- else :
86+ if not os .path .exists (Traceloop . AUTO_CREATED_KEY_PATH ):
87+ os . makedirs (
88+ os . path . dirname ( Traceloop . AUTO_CREATED_KEY_PATH ), exist_ok = True
89+ )
90+ os . makedirs ( os . path . dirname ( Traceloop . AUTO_CREATED_URL ), exist_ok = True )
91+
8692 print (
8793 Fore .YELLOW
8894 + "No Traceloop API key provided, auto-creating a dashboard on Traceloop" ,
@@ -93,8 +99,13 @@ def init(
9399 access_url = f"https://app.traceloop.com/trace?skt={ res ['uiAccessKey' ]} "
94100 api_key = res ["apiKey" ]
95101 print (Fore .YELLOW + "TRACELOOP_API_KEY=" , api_key )
96- open ("/tmp/traceloop_key.txt" , "w" ).write (api_key )
97- open ("/tmp/traceloop_url.txt" , "w" ).write (access_url )
102+
103+ open (Traceloop .AUTO_CREATED_KEY_PATH , "w" ).write (api_key )
104+ open (Traceloop .AUTO_CREATED_URL , "w" ).write (access_url )
105+ else :
106+ api_key = open ("/tmp/traceloop_key.txt" ).read ()
107+ access_url = open ("/tmp/traceloop_url.txt" ).read ()
108+
98109 print (
99110 Fore .GREEN + f"\n Go to { access_url } to see a live dashboard\n " ,
100111 )
0 commit comments