langfuse.Sampler
1import hashlib 2import logging 3 4 5log = logging.getLogger("langfuse") 6 7 8class Sampler: 9 sample_rate: float 10 11 def __init__(self, sample_rate: float): 12 self.sample_rate = sample_rate 13 14 def sample_event(self, event: dict): 15 # need to get trace_id from a given event 16 # returns true if 17 18 if "type" in event and "body" in event: 19 event_type = event["type"] 20 21 if event_type == "sdk-log": 22 return True 23 24 trace_id = None 25 26 if event_type == "trace-create" and "id" in event["body"]: 27 trace_id = event["body"]["id"] 28 elif "trace_id" in event["body"]: 29 trace_id = event["body"]["trace_id"] 30 elif "traceId" in event["body"]: 31 trace_id = event["body"]["traceId"] 32 else: 33 log.error("Unexpected event format: No trace id found in event") 34 return True 35 36 return self.deterministic_sample(trace_id, self.sample_rate) 37 38 else: 39 log.error("Unexpected event format: No trace id found in event") 40 return True 41 42 def deterministic_sample(self, trace_id: str, sample_rate: float): 43 """Determins if an event should be sampled based on the trace_id and sample_rate. Event will be sent to server if True""" 44 log.debug( 45 f"Applying deterministic sampling to trace_id: {trace_id} with rate {sample_rate}" 46 ) 47 48 # Use SHA-256 to hash the trace_id 49 hash_object = hashlib.sha256(trace_id.encode()) 50 # Get the hexadecimal representation of the hash 51 hash_hex = hash_object.hexdigest() 52 53 # Take the first 8 characters of the hex digest and convert to integer 54 hash_int = int(hash_hex[:8], 16) 55 56 # Normalize the integer to a float in the range [0, 1) 57 normalized_hash = hash_int / 0xFFFFFFFF 58 59 result = normalized_hash < sample_rate 60 61 if not result: 62 log.debug( 63 f"event with trace_id: {trace_id} and rate {sample_rate} was sampled and not sent to the server" 64 ) 65 66 return result
log =
<Logger langfuse (WARNING)>
class
Sampler:
9class Sampler: 10 sample_rate: float 11 12 def __init__(self, sample_rate: float): 13 self.sample_rate = sample_rate 14 15 def sample_event(self, event: dict): 16 # need to get trace_id from a given event 17 # returns true if 18 19 if "type" in event and "body" in event: 20 event_type = event["type"] 21 22 if event_type == "sdk-log": 23 return True 24 25 trace_id = None 26 27 if event_type == "trace-create" and "id" in event["body"]: 28 trace_id = event["body"]["id"] 29 elif "trace_id" in event["body"]: 30 trace_id = event["body"]["trace_id"] 31 elif "traceId" in event["body"]: 32 trace_id = event["body"]["traceId"] 33 else: 34 log.error("Unexpected event format: No trace id found in event") 35 return True 36 37 return self.deterministic_sample(trace_id, self.sample_rate) 38 39 else: 40 log.error("Unexpected event format: No trace id found in event") 41 return True 42 43 def deterministic_sample(self, trace_id: str, sample_rate: float): 44 """Determins if an event should be sampled based on the trace_id and sample_rate. Event will be sent to server if True""" 45 log.debug( 46 f"Applying deterministic sampling to trace_id: {trace_id} with rate {sample_rate}" 47 ) 48 49 # Use SHA-256 to hash the trace_id 50 hash_object = hashlib.sha256(trace_id.encode()) 51 # Get the hexadecimal representation of the hash 52 hash_hex = hash_object.hexdigest() 53 54 # Take the first 8 characters of the hex digest and convert to integer 55 hash_int = int(hash_hex[:8], 16) 56 57 # Normalize the integer to a float in the range [0, 1) 58 normalized_hash = hash_int / 0xFFFFFFFF 59 60 result = normalized_hash < sample_rate 61 62 if not result: 63 log.debug( 64 f"event with trace_id: {trace_id} and rate {sample_rate} was sampled and not sent to the server" 65 ) 66 67 return result
def
sample_event(self, event: dict):
15 def sample_event(self, event: dict): 16 # need to get trace_id from a given event 17 # returns true if 18 19 if "type" in event and "body" in event: 20 event_type = event["type"] 21 22 if event_type == "sdk-log": 23 return True 24 25 trace_id = None 26 27 if event_type == "trace-create" and "id" in event["body"]: 28 trace_id = event["body"]["id"] 29 elif "trace_id" in event["body"]: 30 trace_id = event["body"]["trace_id"] 31 elif "traceId" in event["body"]: 32 trace_id = event["body"]["traceId"] 33 else: 34 log.error("Unexpected event format: No trace id found in event") 35 return True 36 37 return self.deterministic_sample(trace_id, self.sample_rate) 38 39 else: 40 log.error("Unexpected event format: No trace id found in event") 41 return True
def
deterministic_sample(self, trace_id: str, sample_rate: float):
43 def deterministic_sample(self, trace_id: str, sample_rate: float): 44 """Determins if an event should be sampled based on the trace_id and sample_rate. Event will be sent to server if True""" 45 log.debug( 46 f"Applying deterministic sampling to trace_id: {trace_id} with rate {sample_rate}" 47 ) 48 49 # Use SHA-256 to hash the trace_id 50 hash_object = hashlib.sha256(trace_id.encode()) 51 # Get the hexadecimal representation of the hash 52 hash_hex = hash_object.hexdigest() 53 54 # Take the first 8 characters of the hex digest and convert to integer 55 hash_int = int(hash_hex[:8], 16) 56 57 # Normalize the integer to a float in the range [0, 1) 58 normalized_hash = hash_int / 0xFFFFFFFF 59 60 result = normalized_hash < sample_rate 61 62 if not result: 63 log.debug( 64 f"event with trace_id: {trace_id} and rate {sample_rate} was sampled and not sent to the server" 65 ) 66 67 return result
Determins if an event should be sampled based on the trace_id and sample_rate. Event will be sent to server if True