File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change 36
36
import logging
37
37
import os
38
38
import platform
39
+ import re
39
40
import sys
40
41
import threading
41
42
import time
@@ -425,11 +426,10 @@ def get_user_agent(self) -> str:
425
426
to the APM Server
426
427
"""
427
428
if self .config .service_version :
428
- return "apm-agent-python/{} ({} {})" .format (
429
- elasticapm .VERSION , self .config .service_name , self .config .service_version
430
- ).encode ("utf-8" )
429
+ service_version = re .sub (r"[^\t _\x21-\x27\x2a-\x5b\x5d-\x7e\x80-\xff]" , "_" , self .config .service_version )
430
+ return "apm-agent-python/{} ({} {})" .format (elasticapm .VERSION , self .config .service_name , service_version )
431
431
else :
432
- return "apm-agent-python/{} ({})" .format (elasticapm .VERSION , self .config .service_name ). encode ( "utf-8" )
432
+ return "apm-agent-python/{} ({})" .format (elasticapm .VERSION , self .config .service_name )
433
433
434
434
def build_metadata (self ):
435
435
data = {
Original file line number Diff line number Diff line change @@ -867,3 +867,15 @@ def test_backdating_transaction(elasticapm_client):
867
867
elasticapm_client .end_transaction ()
868
868
transaction = elasticapm_client .events [TRANSACTION ][0 ]
869
869
assert 1000 < transaction ["duration" ] < 2000
870
+
871
+
872
+ @pytest .mark .parametrize (
873
+ "elasticapm_client,expected" ,
874
+ [
875
+ ({"service_version" : "v2" }, "v2" ),
876
+ ({"service_version" : "v2 \x00 " }, "v2 _" ),
877
+ ],
878
+ indirect = ["elasticapm_client" ],
879
+ )
880
+ def test_user_agent (elasticapm_client , expected ):
881
+ assert elasticapm_client .get_user_agent () == "apm-agent-python/unknown (myapp {})" .format (expected )
You can’t perform that action at this time.
0 commit comments