You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2024/05/23 08:30:09 UTC

(skywalking-php) branch master updated: Defined instance_name (#111)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-php.git


The following commit(s) were added to refs/heads/master by this push:
     new dc1efbd  Defined instance_name (#111)
dc1efbd is described below

commit dc1efbd9d83066b3fca8fb16c6a6650fe3450452
Author: Alex Abaev <88...@users.noreply.github.com>
AuthorDate: Thu May 23 11:30:03 2024 +0300

    Defined instance_name (#111)
---
 docs/en/configuration/ini-settings.md | 46 ++++++++++++++++++-----------------
 src/lib.rs                            |  8 ++++++
 src/module.rs                         | 16 ++++++++++--
 3 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/docs/en/configuration/ini-settings.md b/docs/en/configuration/ini-settings.md
index f437b6e..dd883ba 100644
--- a/docs/en/configuration/ini-settings.md
+++ b/docs/en/configuration/ini-settings.md
@@ -2,25 +2,27 @@
 
 This is the configuration list supported in `php.ini`.
 
-| Configuration Item                               | Description                                                                                                                           | Default Value             |
-| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
-| skywalking_agent.enable                          | Enable skywalking_agent extension or not.                                                                                             | Off                       |
-| skywalking_agent.log_file                        | Log file path.                                                                                                                        | /tmp/skywalking-agent.log |
-| skywalking_agent.log_level                       | Log level: one of `OFF`, `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`.                                                                   | INFO                      |
-| skywalking_agent.runtime_dir                     | Skywalking agent runtime directory.                                                                                                   | /tmp/skywalking-agent     |
-| skywalking_agent.server_addr                     | Address of skywalking oap server. Only available when `reporter_type` is `grpc`.                                                      | 127.0.0.1:11800           |
-| skywalking_agent.service_name                    | Application service name.                                                                                                             | hello-skywalking          |
-| skywalking_agent.skywalking_version              | Skywalking version, 8 or 9.                                                                                                           | 8                         |
-| skywalking_agent.authentication                  | Skywalking authentication token, let it empty if the backend isn't enabled. Only available when `reporter_type` is `grpc`.            |                           |
-| skywalking_agent.worker_threads                  | Skywalking worker threads, 0 will auto set as the cpu core size.                                                                      | 0                         |
-| skywalking_agent.enable_tls                      | Wether to enable tls for gPRC, default is false. Only available when `reporter_type` is `grpc`.                                       | Off                       |
-| skywalking_agent.ssl_trusted_ca_path             | The gRPC SSL trusted ca file. Only available when `reporter_type` is `grpc`.                                                          |                           |
-| skywalking_agent.ssl_key_path                    | The private key file. Enable mTLS when `ssl_key_path` and `ssl_cert_chain_path` exist. Only available when `reporter_type` is `grpc`. |                           |
-| skywalking_agent.ssl_cert_chain_path             | The certificate file. Enable mTLS when `ssl_key_path` and `ssl_cert_chain_path` exist. Only available when `reporter_type` is `grpc`. |                           |
-| skywalking_agent.heartbeat_period                | Agent heartbeat report period. Unit, second.                                                                                          | 30                        |
-| skywalking_agent.properties_report_period_factor | The agent sends the instance properties to the backend every heartbeat_period * properties_report_period_factor seconds.              | 10                        |
-| skywalking_agent.enable_zend_observer            | Whether to use `zend observer` instead of `zend_execute_ex` to hook the functions, this feature is only available for PHP8+.          | Off                       |
-| skywalking_agent.reporter_type                   | Reporter type, optional values are `grpc` and `kafka`.                                                                                | grpc                      |
-| skywalking_agent.kafka_bootstrap_servers         | A list of host/port pairs to use for connect to the Kafka cluster. Only available when `reporter_type` is `kafka`.                    |                           |
-| skywalking_agent.kafka_producer_config           | Configure Kafka Producer configuration in JSON format `{"key": "value}`. Only available when `reporter_type` is `kafka`.              | {}                        |
-| skywalking_agent.inject_context                  | Whether to enable automatic injection of skywalking context variables (such as `SW_TRACE_ID`). For `php-fpm` mode, it will be injected into the `$_SERVER` variable. For `swoole` mode, it will be injected into the `$request->server` variable.              | Off                    |
+| Configuration Item                               | Description                                                                                                                                                                                                                                       | Default Value             |
+| ------------------------------------------------ |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------- |
+| skywalking_agent.enable                          | Enable skywalking_agent extension or not.                                                                                                                                                                                                         | Off                       |
+| skywalking_agent.log_file                        | Log file path.                                                                                                                                                                                                                                    | /tmp/skywalking-agent.log |
+| skywalking_agent.log_level                       | Log level: one of `OFF`, `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`.                                                                                                                                                                               | INFO                      |
+| skywalking_agent.runtime_dir                     | Skywalking agent runtime directory.                                                                                                                                                                                                               | /tmp/skywalking-agent     |
+| skywalking_agent.server_addr                     | Address of skywalking oap server. Only available when `reporter_type` is `grpc`.                                                                                                                                                                  | 127.0.0.1:11800           |
+| skywalking_agent.service_name                    | Application service name.                                                                                                                                                                                                                         | hello-skywalking          |
+| skywalking_agent.skywalking_version              | Skywalking version, 8 or 9.                                                                                                                                                                                                                       | 8                         |
+| skywalking_agent.authentication                  | Skywalking authentication token, let it empty if the backend isn't enabled. Only available when `reporter_type` is `grpc`.                                                                                                                        |                           |
+| skywalking_agent.worker_threads                  | Skywalking worker threads, 0 will auto set as the cpu core size.                                                                                                                                                                                  | 0                         |
+| skywalking_agent.enable_tls                      | Wether to enable tls for gPRC, default is false. Only available when `reporter_type` is `grpc`.                                                                                                                                                   | Off                       |
+| skywalking_agent.ssl_trusted_ca_path             | The gRPC SSL trusted ca file. Only available when `reporter_type` is `grpc`.                                                                                                                                                                      |                           |
+| skywalking_agent.ssl_key_path                    | The private key file. Enable mTLS when `ssl_key_path` and `ssl_cert_chain_path` exist. Only available when `reporter_type` is `grpc`.                                                                                                             |                           |
+| skywalking_agent.ssl_cert_chain_path             | The certificate file. Enable mTLS when `ssl_key_path` and `ssl_cert_chain_path` exist. Only available when `reporter_type` is `grpc`.                                                                                                             |                           |
+| skywalking_agent.heartbeat_period                | Agent heartbeat report period. Unit, second.                                                                                                                                                                                                      | 30                        |
+| skywalking_agent.properties_report_period_factor | The agent sends the instance properties to the backend every heartbeat_period * properties_report_period_factor seconds.                                                                                                                          | 10                        |
+| skywalking_agent.enable_zend_observer            | Whether to use `zend observer` instead of `zend_execute_ex` to hook the functions, this feature is only available for PHP8+.                                                                                                                      | Off                       |
+| skywalking_agent.reporter_type                   | Reporter type, optional values are `grpc` and `kafka`.                                                                                                                                                                                            | grpc                      |
+| skywalking_agent.kafka_bootstrap_servers         | A list of host/port pairs to use for connect to the Kafka cluster. Only available when `reporter_type` is `kafka`.                                                                                                                                |                           |
+| skywalking_agent.kafka_producer_config           | Configure Kafka Producer configuration in JSON format `{"key": "value}`. Only available when `reporter_type` is `kafka`.                                                                                                                          | {}                        |
+| skywalking_agent.inject_context                  | Whether to enable automatic injection of skywalking context variables (such as `SW_TRACE_ID`). For `php-fpm` mode, it will be injected into the `$_SERVER` variable. For `swoole` mode, it will be injected into the `$request->server` variable. | Off                    |
+| skywalking_agent.instance_name                   | Instance name. You can set ${HOSTNAME}, refer to [Example #1]( https://www.php.net/manual/en/install.fpm.configuration.php)                                                                                                                           |                     |
+
diff --git a/src/lib.rs b/src/lib.rs
index 28ba16f..a65f4ef 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -44,6 +44,9 @@ const SKYWALKING_AGENT_SKYWALKING_VERSION: &str = "skywalking_agent.skywalking_v
 /// skywalking server address.
 const SKYWALKING_AGENT_SERVER_ADDR: &str = "skywalking_agent.server_addr";
 
+/// skywalking instance name.
+const SKYWALKING_AGENT_INSTANCE_NAME: &str = "skywalking_agent.instance_name";
+
 /// skywalking app service name.
 const SKYWALKING_AGENT_SERVICE_NAME: &str = "skywalking_agent.service_name";
 
@@ -122,6 +125,11 @@ pub fn get_module() -> Module {
         "127.0.0.1:11800".to_string(),
         Policy::System,
     );
+    module.add_ini(
+        SKYWALKING_AGENT_INSTANCE_NAME,
+        "".to_string(),
+        Policy::System,
+    );
     module.add_ini(
         SKYWALKING_AGENT_SERVICE_NAME,
         "hello-skywalking".to_string(),
diff --git a/src/module.rs b/src/module.rs
index 6d5d865..a527a48 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -62,8 +62,20 @@ pub static SERVER_ADDR: Lazy<String> =
 pub static SERVICE_NAME: Lazy<String> =
     Lazy::new(|| get_str_ini_with_default(SKYWALKING_AGENT_SERVICE_NAME));
 
-pub static SERVICE_INSTANCE: Lazy<String> =
-    Lazy::new(|| RandomGenerator::generate() + "@" + &IPS[0]);
+pub static SERVICE_INSTANCE: Lazy<String> = Lazy::new(|| {
+    let rnd_hostname = RandomGenerator::generate() + "@" + &IPS[0];
+    let mut service_instance = rnd_hostname.as_str();
+
+    let defined_instance_name = ini_get::<Option<&CStr>>(SKYWALKING_AGENT_INSTANCE_NAME)
+        .and_then(|s| s.to_str().ok())
+        .unwrap_or_default();
+    let defined_instance_name = defined_instance_name.trim();
+
+    if !defined_instance_name.is_empty() {
+        service_instance = defined_instance_name;
+    }
+    service_instance.to_string()
+});
 
 pub static SKYWALKING_VERSION: Lazy<i64> =
     Lazy::new(|| ini_get::<i64>(SKYWALKING_AGENT_SKYWALKING_VERSION));