ysde/grafana-backup-tool

UnicodeEncodeError while saving

Closed this issue · 3 comments

Hi everyone !

Encountering some problems with the solution :

First of all, the command 'grafana-backup save' would not work (401 Unauthorized at the API check).
Double Checked the configuration file, everything was okay.
Eventually figured it out with this issue Backup of Docker #116 by using ENV Variables. <= If anyone can explain why this is not working with the conf file, i would be interested !

Now i can pass the API Check without any issue but the script is throwing another error :

query dashboard uri: http://GRAFANA-IP:3000/api/dashboards/uid/_G0nsXRgk
[DEBUG] resp status: 200
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/commons.py", line 14, in log_response
    print("[DEBUG] resp body: {0}".format(resp.json()))
UnicodeEncodeError: 'latin-1' codec can't encode character '\u20ac' in position 3945: ordinal not in range(256)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/grafana-backup", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/cli.py", line 52, in main
    save(args, settings)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/save.py", line 51, in main
    backup_functions[backup_function](args, settings)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/save_dashboards.py", line 26, in main
    save_dashboards_above_Ver6_2(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/save_dashboards.py", line 86, in save_dashboards_above_Ver6_2
    get_individual_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/save_dashboards.py", line 64, in get_individual_dashboard_setting_and_save
    (status, content) = get_dashboard(board_uri, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/dashboardApi.py", line 83, in get_dashboard
    (status_code, content) = send_grafana_get(url, http_get_headers, verify_ssl, client_cert, debug)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/dashboardApi.py", line 329, in send_grafana_get
    log_response(r)
  File "/usr/local/lib/python3.7/dist-packages/grafana_backup/commons.py", line 16, in log_response
    print("[DEBUG] resp body: {0}".format(resp.text))
UnicodeEncodeError: 'latin-1' codec can't encode character '\u20ac' in position 3606: ordinal not in range(256)

Any solution is appreciated !

Have a nice day :)

@Nazerr
UnicodeEncodeError: 'latin-1' codec can't encode character '\u20ac' in position 3945: ordinal not in range(256)

This message tells me you have a character in your config file that isn't being read correctly which according to this site it looks like a character might be the culprit https://www.fileformat.info/info/unicode/char/20ac/index.htm

@Nazerr also if you want you could do some unicode encode/decode troubleshooting to see if you can figure out where the is coming from. Perhaps something like this in commons.py would help (try encoding the response string to utf8)

def log_response(resp):
    status_code = resp.status_code
    print("[DEBUG] resp status: {0}".format(status_code))
    try:
        print("[DEBUG] resp body: {0}".format(resp.json().encode("utf-8")))
        #print("[DEBUG] resp body: {0}".format(resp.json()))
    except ValueError:
        print("[DEBUG] resp body: {0}".format(resp.text))
    return resp

@acjohnson Thanks, will try this !