Repository: elastic/apm-agent-python

Issue:
Does not send data to elastic
**Events are not arriving in elastic, some messages appear when executing elasticapm test.**

**To Reproduce**

1. Check this repository: https://github.com/mjr/test-django-elasticapm/
2. Run `python manage.py elasticapm test`
3. Nothing appears in the elastic panel

**Environment (please complete the following information)**
- OS: [e.g. Linux]
- Python version: 3.10.4
- Framework and version: Django 4.0.4 
- APM Server version: 7+
- Agent version: elastic-apm 6.9.1


**Additional context**

```shell
$ python manage.py elasticapm test
 does not match pattern ^[a-zA-Z0-9 _-]+$
Could not register elasticapm.metrics.sets.cpu.CPUMetricSet metricset: psutil not found. Install it to get system and process metrics
 does not match pattern ^[a-zA-Z0-9 _-]+$
Could not register elasticapm.metrics.sets.cpu.CPUMetricSet metricset: psutil not found. Install it to get system and process metrics
Client object is being set more than once
Stack (most recent call last):
  File "/Users/mjr/Workspace/test-elasticapm/.venv/../manage.py", line 22, in <module>
    main()
  File "/Users/mjr/Workspace/test-elasticapm/.venv/../manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/elasticapm/contrib/django/management/commands/elasticapm.py", line 131, in handle
    self.dispatch.get(subcommand, self.handle_command_not_found)(self, subcommand, **options)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/elasticapm/contrib/django/management/commands/elasticapm.py", line 153, in handle_test
    client = DjangoClient(**config)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/elasticapm/contrib/django/client.py", line 88, in __init__
    super(DjangoClient, self).__init__(config, **inline)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/elasticapm/base.py", line 219, in __init__
    set_client(self)
  File "/Users/mjr/Workspace/test-elasticapm/.venv/lib/python3.10/site-packages/elasticapm/base.py", line 682, in set_client
    logger.warning("Client object is being set more than once", stack_info=True)
Trying to send a test error to APM Server using these settings:

SERVICE_NAME:   unknown-python-service
SECRET_TOKEN:   DYVuJGfJTX8brbTKaY
SERVER:         https://28b44e7b03744591a63f086fe3058600.apm.us-east-1.aws.cloud.es.io:443

Success! We tracked the error successfully! 
You should see it in the APM app in Kibana momentarily. 
Look for "TestException: Hi there!" in the Errors tab of the unknown-python-service app
```


Discussion:
Hey @mjr! Thanks for the report. I'm not sure that our `manage.py elasticapm test` has received a lot of love recently, which we should fix. In the meantime, are you seeing data in Elastic from your app's normal execution?

🚨  Also, you should probably rotate your secret token, as it's present in your log above.
Hello @basepi! Thanks for taking a look at this.

No, data is not showing in elastic panel.

Don't worry about the secret token being from a test account.
I checked out your test app. Currently you [have the service name set to an empty string](https://github.com/mjr/test-django-elasticapm/blob/7b4d12494dba7da2bb0fcfdb978759c9582c5077/testelasticapm/settings.py#L37). Can you try setting the service name? Everything we show in Kibana is based on the service name, and we should probably not allow it to be set to an empty string as I think it can cause problems.
It worked, thank you very much for your time.

I think the documentation could give an example with the service name, I think this can cause confusion.
@basepi in another project I'm getting errors that I can't identify, the settings are the same as in the test project, but the data doesn't arrive in the elastic panel.

```
No version key found in server response: b'{"ok":false,"message":"Unknown resource."}\n'
```

```
dropping flushed data due to transport failure back-off
```

```
TransportException
HTTP 404: {"ok":false,"message":"Unknown resource."}
```
@mjr We should probably make this a new issue.

That said, when you say the settings are the same, do you mean you're sending to the same elastic cloud endpoint? The 404 is very odd, it's like you're sending to a non-standard endpoint or port or something. Are you perhaps sending to the kibana endpoint instead of the APM endpoint?
> I think the documentation could give an example with the service name, I think this can cause confusion.

Did you find a place where the service name is set to an empty string? I mostly am seeing `'<SERVICE_NAME>'` which I think is OK. But I definitely need to add a log to make sure it's not an empty string.

Resolving patch:
diff --git a/elasticapm/base.py b/elasticapm/base.py
--- a/elasticapm/base.py
+++ b/elasticapm/base.py
@@ -116,8 +116,12 @@ def __init__(self, config=None, **inline):
             for msg in config.errors.values():
                 self.error_logger.error(msg)
             config.disable_send = True
-        if config.service_name == "python_service":
-            self.logger.warning("No custom SERVICE_NAME was set -- using non-descript default 'python_service'")
+        if config.service_name == "unknown-python-service":
+            self.logger.warning("No custom SERVICE_NAME was set -- using non-descript default 'unknown-python-service'")
+        if config.service_name.strip() == "":
+            self.logger.error(
+                "SERVICE_NAME cannot be whitespace-only. Please set the SERVICE_NAME to a useful identifier."
+            )
         self.config = VersionedConfig(config, version=None)
 
         # Insert the log_record_factory into the logging library


Test patch: