@@ -932,137 +932,39 @@ def capture_event(
932932
933933 return return_value
934934
935- def _capture_log (self , log ):
936- # type: (Optional[Log]) -> None
937- if not has_logs_enabled (self .options ) or log is None :
935+ def _capture_telemetry (self , telemetry , type_ , scope ):
936+ # type: (Telemetry, str, Scope) -> None
937+ # Capture attributes-based telemetry (logs, metrics, spansV2)
938+ before_send_getter = {
939+ "log" : lambda : get_before_send_log (self .options ),
940+ "metric" : lambda : get_before_send_metric (self .options ),
941+ }.get (type_ )
942+
943+ if before_send_getter is not None :
944+ before_send = before_send_getter ()
945+ if before_send is not None :
946+ telemetry = before_send (telemetry , {})
947+
948+ if telemetry is None :
938949 return
939950
940- current_scope = sentry_sdk .get_current_scope ()
941- isolation_scope = sentry_sdk .get_isolation_scope ()
942-
943- log ["attributes" ]["sentry.sdk.name" ] = SDK_INFO ["name" ]
944- log ["attributes" ]["sentry.sdk.version" ] = SDK_INFO ["version" ]
945-
946- server_name = self .options .get ("server_name" )
947- if server_name is not None and SPANDATA .SERVER_ADDRESS not in log ["attributes" ]:
948- log ["attributes" ][SPANDATA .SERVER_ADDRESS ] = server_name
949-
950- environment = self .options .get ("environment" )
951- if environment is not None and "sentry.environment" not in log ["attributes" ]:
952- log ["attributes" ]["sentry.environment" ] = environment
953-
954- release = self .options .get ("release" )
955- if release is not None and "sentry.release" not in log ["attributes" ]:
956- log ["attributes" ]["sentry.release" ] = release
957-
958- trace_context = current_scope .get_trace_context ()
959- trace_id = trace_context .get ("trace_id" )
960- span_id = trace_context .get ("span_id" )
961-
962- if trace_id is not None and log .get ("trace_id" ) is None :
963- log ["trace_id" ] = trace_id
964-
965- if (
966- span_id is not None
967- and "sentry.trace.parent_span_id" not in log ["attributes" ]
968- ):
969- log ["attributes" ]["sentry.trace.parent_span_id" ] = span_id
970-
971- # The user, if present, is always set on the isolation scope.
972- if isolation_scope ._user is not None :
973- for log_attribute , user_attribute in (
974- ("user.id" , "id" ),
975- ("user.name" , "username" ),
976- ("user.email" , "email" ),
977- ):
978- if (
979- user_attribute in isolation_scope ._user
980- and log_attribute not in log ["attributes" ]
981- ):
982- log ["attributes" ][log_attribute ] = isolation_scope ._user [
983- user_attribute
984- ]
985-
986- # If debug is enabled, log the log to the console
987- debug = self .options .get ("debug" , False )
988- if debug :
989- logger .debug (
990- f"[Sentry Logs] [{ log .get ('severity_text' )} ] { log .get ('body' )} "
991- )
951+ scope .apply_to_telemetry (telemetry )
992952
993- before_send_log = get_before_send_log (self .options )
994- if before_send_log is not None :
995- log = before_send_log (log , {})
953+ batcher = {
954+ "log" : self .log_batcher ,
955+ "metric" : self .metrics_batcher ,
956+ }.get (type_ ) # type: Optional[LogBatcher, MetricsBatcher]
996957
997- if log is None :
998- return
958+ if batcher :
959+ batcher . add ( telemetry )
999960
1000- if self .log_batcher :
1001- self .log_batcher .add (log )
961+ def _capture_log (self , log , scope ):
962+ # type: (Optional[Log], Scope) -> None
963+ self ._capture_telemetry (log , "log" , scope )
1002964
1003- def _capture_metric (self , metric ):
965+ def _capture_metric (self , metric , scope ):
1004966 # type: (Optional[Metric]) -> None
1005- if not has_metrics_enabled (self .options ) or metric is None :
1006- return
1007-
1008- current_scope = sentry_sdk .get_current_scope ()
1009- isolation_scope = sentry_sdk .get_isolation_scope ()
1010-
1011- metric ["attributes" ]["sentry.sdk.name" ] = SDK_INFO ["name" ]
1012- metric ["attributes" ]["sentry.sdk.version" ] = SDK_INFO ["version" ]
1013-
1014- server_name = self .options .get ("server_name" )
1015- if (
1016- server_name is not None
1017- and SPANDATA .SERVER_ADDRESS not in metric ["attributes" ]
1018- ):
1019- metric ["attributes" ][SPANDATA .SERVER_ADDRESS ] = server_name
1020-
1021- environment = self .options .get ("environment" )
1022- if environment is not None and "sentry.environment" not in metric ["attributes" ]:
1023- metric ["attributes" ]["sentry.environment" ] = environment
1024-
1025- release = self .options .get ("release" )
1026- if release is not None and "sentry.release" not in metric ["attributes" ]:
1027- metric ["attributes" ]["sentry.release" ] = release
1028-
1029- trace_context = current_scope .get_trace_context ()
1030- trace_id = trace_context .get ("trace_id" )
1031- span_id = trace_context .get ("span_id" )
1032-
1033- metric ["trace_id" ] = trace_id or "00000000-0000-0000-0000-000000000000"
1034- if span_id is not None :
1035- metric ["span_id" ] = span_id
1036-
1037- if isolation_scope ._user is not None :
1038- for metric_attribute , user_attribute in (
1039- ("user.id" , "id" ),
1040- ("user.name" , "username" ),
1041- ("user.email" , "email" ),
1042- ):
1043- if (
1044- user_attribute in isolation_scope ._user
1045- and metric_attribute not in metric ["attributes" ]
1046- ):
1047- metric ["attributes" ][metric_attribute ] = isolation_scope ._user [
1048- user_attribute
1049- ]
1050-
1051- debug = self .options .get ("debug" , False )
1052- if debug :
1053- logger .debug (
1054- f"[Sentry Metrics] [{ metric .get ('type' )} ] { metric .get ('name' )} : { metric .get ('value' )} "
1055- )
1056-
1057- before_send_metric = get_before_send_metric (self .options )
1058- if before_send_metric is not None :
1059- metric = before_send_metric (metric , {})
1060-
1061- if metric is None :
1062- return
1063-
1064- if self .metrics_batcher :
1065- self .metrics_batcher .add (metric )
967+ self ._capture_telemetry (metric , "metric" , scope )
1066968
1067969 def capture_session (
1068970 self ,
0 commit comments