From f56c46659c8fcd985761919a5168b3d2593a4f5d Mon Sep 17 00:00:00 2001 From: LeiZhang-Hunter <357733652@qq.com> Date: Wed, 26 Jun 2024 01:04:45 +0800 Subject: [PATCH 1/4] update token --- src/lib.rs | 8 ++++++++ src/module.rs | 8 ++++++++ src/request.rs | 2 ++ 3 files changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a65f4ef..e6f818e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,8 @@ const SKYWALKING_AGENT_KAFKA_PRODUCER_CONFIG: &str = "skywalking_agent.kafka_pro /// `$request->server` variable. const SKYWALKING_AGENT_INJECT_CONTEXT: &str = "skywalking_agent.inject_context"; +const SKYWALKING_AGENT_TOKEN: &str = "skywalking_agent.token"; + #[php_get_module] pub fn get_module() -> Module { let mut module = Module::new( @@ -196,6 +198,12 @@ pub fn get_module() -> Module { ); module.add_ini(SKYWALKING_AGENT_INJECT_CONTEXT, false, Policy::System); + module.add_ini( + SKYWALKING_AGENT_TOKEN, + "{}".to_string(), + Policy::System, + ); + // Hooks. module.on_module_init(module::init); module.on_module_shutdown(module::shutdown); diff --git a/src/module.rs b/src/module.rs index edd55d7..be20927 100644 --- a/src/module.rs +++ b/src/module.rs @@ -77,6 +77,14 @@ pub static SERVICE_INSTANCE: Lazy = Lazy::new(|| { service_instance.to_string() }); +pub static TOKEN_NAME: Lazy = Lazy::new(|| { + let defined_token_name = ini_get::>(SKYWALKING_AGENT_TOKEN) + .and_then(|s| s.to_str().ok()) + .unwrap_or_default(); + + defined_token_name.to_string() +}); + pub static SKYWALKING_VERSION: Lazy = Lazy::new(|| ini_get::(SKYWALKING_AGENT_SKYWALKING_VERSION)); diff --git a/src/request.rs b/src/request.rs index 0bddbc9..f0d40a6 100644 --- a/src/request.rs +++ b/src/request.rs @@ -31,6 +31,7 @@ use std::{ }; use tracing::{error, instrument, trace, warn}; use url::Url; +use crate::module::TOKEN_NAME; const INJECT_CONTEXT_SERVICE_NAME: &str = "SW_SERVICE_NAME"; const INJECT_CONTEXT_INSTANCE_NAME: &str = "SW_INSTANCE_NAME"; @@ -345,6 +346,7 @@ fn create_request_context( let span_object = span.span_object_mut(); span_object.component_id = COMPONENT_PHP_ID; + span_object.add_tag("token", &*TOKEN_NAME); span_object.add_tag("url", url.to_string()); span_object.add_tag("http.method", method); From 6e5b1d55bc545991754ec4b6b091873c9e759983 Mon Sep 17 00:00:00 2001 From: LeiZhang-Hunter <357733652@qq.com> Date: Wed, 26 Jun 2024 01:06:15 +0800 Subject: [PATCH 2/4] add token --- src/module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.rs b/src/module.rs index be20927..40d306e 100644 --- a/src/module.rs +++ b/src/module.rs @@ -82,7 +82,7 @@ pub static TOKEN_NAME: Lazy = Lazy::new(|| { .and_then(|s| s.to_str().ok()) .unwrap_or_default(); - defined_token_name.to_string() + defined_token_name.to_string()k }); pub static SKYWALKING_VERSION: Lazy = From 1baadbdf3be4eb504e34d6c8cbf56d1f398e348c Mon Sep 17 00:00:00 2001 From: LeiZhang-Hunter <357733652@qq.com> Date: Wed, 26 Jun 2024 01:06:26 +0800 Subject: [PATCH 3/4] add token --- src/module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.rs b/src/module.rs index 40d306e..be20927 100644 --- a/src/module.rs +++ b/src/module.rs @@ -82,7 +82,7 @@ pub static TOKEN_NAME: Lazy = Lazy::new(|| { .and_then(|s| s.to_str().ok()) .unwrap_or_default(); - defined_token_name.to_string()k + defined_token_name.to_string() }); pub static SKYWALKING_VERSION: Lazy = From 74abe89f4da72bacca5e1e33ef0ca9570a921a60 Mon Sep 17 00:00:00 2001 From: LeiZhang-Hunter <357733652@qq.com> Date: Sun, 30 Jun 2024 01:18:49 +0800 Subject: [PATCH 4/4] add token --- src/lib.rs | 2 +- src/plugin/mod.rs | 3 ++- src/plugin/plugin_amqplib.rs | 2 ++ src/plugin/plugin_curl.rs | 7 ++++++- src/plugin/plugin_memcache.rs | 2 ++ src/plugin/plugin_memcached.rs | 2 ++ src/plugin/plugin_mongodb.rs | 2 ++ src/plugin/plugin_mysqli.rs | 4 ++++ src/plugin/plugin_pdo.rs | 7 +++++++ src/plugin/plugin_predis.rs | 2 ++ src/plugin/plugin_redis.rs | 3 +++ src/request.rs | 1 + 12 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e6f818e..6f0533a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,7 +200,7 @@ pub fn get_module() -> Module { module.add_ini( SKYWALKING_AGENT_TOKEN, - "{}".to_string(), + "".to_string(), Policy::System, ); diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index dbbcdd6..43dd7d6 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -31,6 +31,7 @@ use phper::{eg, objects::ZObj}; use skywalking::trace::span::HandleSpanObject; use std::{collections::HashMap, ops::Deref, sync::Mutex}; use tracing::error; +use crate::module::TOKEN_NAME; // Register plugins here. static PLUGINS: Lazy>> = Lazy::new(|| { @@ -117,7 +118,7 @@ fn log_exception(span: &mut impl HandleSpanObject) -> Option<&mut ZObj> { if let Some(ex) = ex.as_mut() { let span_object = span.span_object_mut(); span_object.is_error = true; - + span_object.add_tag("token", &*TOKEN_NAME); let mut logs = Vec::new(); if let Ok(class_name) = ex.get_class().get_name().to_str() { logs.push(("error.kind", class_name.to_owned())); diff --git a/src/plugin/plugin_amqplib.rs b/src/plugin/plugin_amqplib.rs index aa86aa6..5147d6e 100644 --- a/src/plugin/plugin_amqplib.rs +++ b/src/plugin/plugin_amqplib.rs @@ -32,6 +32,7 @@ use skywalking::{ proto::v3::SpanLayer, trace::span::{HandleSpanObject, Span}, }; +use crate::module::TOKEN_NAME; #[derive(Default, Clone)] pub struct AmqplibPlugin; @@ -136,6 +137,7 @@ impl AmqplibPlugin { })?; let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.set_span_layer(SpanLayer::Mq); span_object.component_id = COMPONENT_AMQP_PRODUCER_ID; span_object.add_tag(TAG_MQ_BROKER, peer); diff --git a/src/plugin/plugin_curl.rs b/src/plugin/plugin_curl.rs index ab31220..30d4f58 100644 --- a/src/plugin/plugin_curl.rs +++ b/src/plugin/plugin_curl.rs @@ -32,6 +32,7 @@ use skywalking::{ use std::{cell::RefCell, collections::HashMap, os::raw::c_long}; use tracing::{debug, warn}; use url::Url; +use crate::module::TOKEN_NAME; const CURLM_OK: i64 = 0; @@ -425,6 +426,7 @@ impl CurlPlugin { })?; let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.set_span_layer(SpanLayer::Http); span_object.component_id = COMPONENT_PHP_CURL_ID; span_object.add_tag("url", &info.raw_url); @@ -448,10 +450,13 @@ impl CurlPlugin { .context("curl_error is not string")? .to_str()?; let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.is_error = true; span_object.add_log(vec![("CURL_ERROR", curl_error)]); } else { - span.span_object_mut().is_error = http_code >= 400; + let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); + span_object.is_error = http_code >= 400; } log_exception(span); diff --git a/src/plugin/plugin_memcache.rs b/src/plugin/plugin_memcache.rs index cba62bc..6370922 100644 --- a/src/plugin/plugin_memcache.rs +++ b/src/plugin/plugin_memcache.rs @@ -33,6 +33,7 @@ use skywalking::{ }; use std::{any::Any, collections::HashMap}; use tracing::{debug, error, instrument, warn}; +use crate::module::TOKEN_NAME; static PEER_MAP: Lazy> = Lazy::new(Default::default); @@ -254,6 +255,7 @@ fn create_exit_span( ); let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.set_span_layer(SpanLayer::Cache); span_object.component_id = COMPONENT_PHP_MEMCACHED_ID; span_object.add_tag(TAG_CACHE_TYPE, "memcache"); diff --git a/src/plugin/plugin_memcached.rs b/src/plugin/plugin_memcached.rs index 1e480f3..fe4235a 100644 --- a/src/plugin/plugin_memcached.rs +++ b/src/plugin/plugin_memcached.rs @@ -33,6 +33,7 @@ use skywalking::{ trace::span::{HandleSpanObject, Span}, }; use tracing::{debug, instrument, warn}; +use crate::module::TOKEN_NAME; /// The method parameters is empty. static MEMCACHE_EMPTY_METHOD_MAPPING: Lazy>> = Lazy::new(|| { @@ -350,6 +351,7 @@ fn create_exit_span( let span_object = span.span_object_mut(); span_object.set_span_layer(SpanLayer::Cache); + span_object.add_tag("token", &*TOKEN_NAME); span_object.component_id = COMPONENT_PHP_MEMCACHED_ID; span_object.add_tag(TAG_CACHE_TYPE, "memcache"); if let Some(cmd) = tag_info.cmd { diff --git a/src/plugin/plugin_mongodb.rs b/src/plugin/plugin_mongodb.rs index 0ee2ade..f06a75a 100644 --- a/src/plugin/plugin_mongodb.rs +++ b/src/plugin/plugin_mongodb.rs @@ -30,6 +30,7 @@ use skywalking::{ }; use std::any::Any; use tracing::{debug, error}; +use crate::module::TOKEN_NAME; const MANAGER_CLASS_NAME: &str = r"MongoDB\Driver\Manager"; @@ -123,6 +124,7 @@ fn before_manager_crud_hook( let span_object = span.span_object_mut(); span_object.set_span_layer(SpanLayer::Database); + span_object.add_tag("token", &*TOKEN_NAME); span_object.component_id = COMPONENT_MONGODB_ID; span_object.add_tag(TAG_DB_TYPE, "MongoDB"); diff --git a/src/plugin/plugin_mysqli.rs b/src/plugin/plugin_mysqli.rs index cc2ad0f..a5222ba 100644 --- a/src/plugin/plugin_mysqli.rs +++ b/src/plugin/plugin_mysqli.rs @@ -30,6 +30,7 @@ use skywalking::{ trace::span::{HandleSpanObject, Span}, }; use tracing::{debug, error}; +use crate::module::TOKEN_NAME; #[derive(Default, Clone)] pub struct MySQLImprovedPlugin; @@ -159,6 +160,8 @@ impl MySQLImprovedPlugin { style, )?; + span.add_tag("token", &*TOKEN_NAME); + if execute_data.num_args() >= 1 { if let Some(statement) = execute_data.get_parameter(0).as_z_str() { span.add_tag("db.statement", statement.to_str()?); @@ -193,6 +196,7 @@ fn create_mysqli_exit_span( let span_object = span.span_object_mut(); span_object.set_span_layer(SpanLayer::Database); + span_object.add_tag("token", &*TOKEN_NAME); span_object.component_id = COMPONENT_PHP_MYSQLI_ID; span_object.add_tag("db.type", "mysql"); diff --git a/src/plugin/plugin_pdo.rs b/src/plugin/plugin_pdo.rs index 00d6ff9..572b33e 100644 --- a/src/plugin/plugin_pdo.rs +++ b/src/plugin/plugin_pdo.rs @@ -36,6 +36,7 @@ use skywalking::{ }; use std::{any::Any, str::FromStr}; use tracing::{debug, warn}; +use crate::module::TOKEN_NAME; static DSN_MAP: Lazy> = Lazy::new(Default::default); static DTOR_MAP: Lazy> = Lazy::new(Default::default); @@ -128,6 +129,8 @@ impl PdoPlugin { create_exit_span_with_dsn(request_id, "PDO", &function_name, dsn) })?; + span.add_tag("token", &*TOKEN_NAME); + if execute_data.num_args() >= 1 { if let Some(statement) = execute_data.get_parameter(0).as_z_str() { span.add_tag(TAG_DB_STATEMENT, statement.to_str()?); @@ -155,6 +158,8 @@ impl PdoPlugin { create_exit_span_with_dsn(request_id, "PDOStatement", &function_name, dsn) })?; + span.add_tag("token", &*TOKEN_NAME); + if let Some(query) = this.get_property("queryString").as_z_str() { span.add_tag(TAG_DB_STATEMENT, query.to_str()?); } else { @@ -242,6 +247,7 @@ fn after_hook_when_false(this: &mut ZObj, span: &mut Span) -> crate::Result<()> let error = get_error_info_item(info, 2)?.expect_z_str()?.to_str()?; let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.is_error = true; span_object.add_log([("SQLSTATE", state), ("Error Code", code), ("Error", error)]); @@ -273,6 +279,7 @@ fn create_exit_span_with_dsn( ctx.create_exit_span(&format!("{}->{}", class_name, function_name), &dsn.peer); let span_object = span.span_object_mut(); + span_object.add_tag("token", &*TOKEN_NAME); span_object.set_span_layer(SpanLayer::Database); span_object.component_id = COMPONENT_PHP_PDO_ID; span_object.add_tag(TAG_DB_TYPE, &dsn.db_type); diff --git a/src/plugin/plugin_predis.rs b/src/plugin/plugin_predis.rs index 6ed0ac8..ca1fa02 100644 --- a/src/plugin/plugin_predis.rs +++ b/src/plugin/plugin_predis.rs @@ -29,6 +29,7 @@ use skywalking::{ }; use std::collections::HashSet; use tracing::debug; +use crate::module::TOKEN_NAME; pub static REDIS_READ_COMMANDS: Lazy> = Lazy::new(|| { [ @@ -233,6 +234,7 @@ impl PredisPlugin { span_object.set_span_layer(SpanLayer::Cache); span_object.component_id = COMPONENT_PHP_PREDIS_ID; span_object.add_tag(TAG_CACHE_TYPE, "redis"); + span_object.add_tag("token", &*TOKEN_NAME); span_object.add_tag(TAG_CACHE_CMD, cmd); if let Some(op) = op { span_object.add_tag(TAG_CACHE_OP, op); diff --git a/src/plugin/plugin_redis.rs b/src/plugin/plugin_redis.rs index 680602d..0fe739c 100644 --- a/src/plugin/plugin_redis.rs +++ b/src/plugin/plugin_redis.rs @@ -34,6 +34,7 @@ use skywalking::{ }; use std::{any::Any, collections::HashMap}; use tracing::{debug, warn}; +use crate::module::TOKEN_NAME; static PEER_MAP: Lazy> = Lazy::new(Default::default); @@ -261,6 +262,7 @@ impl RedisPlugin { let span_object = span.span_object_mut(); span_object.set_span_layer(SpanLayer::Cache); + span_object.add_tag("token", &*TOKEN_NAME); span_object.component_id = COMPONENT_PHP_REDIS_ID; span_object.add_tag(TAG_CACHE_TYPE, "redis"); @@ -307,6 +309,7 @@ impl RedisPlugin { let span_object = span.span_object_mut(); span_object.set_span_layer(SpanLayer::Cache); span_object.component_id = COMPONENT_PHP_REDIS_ID; + span_object.add_tag("token", &*TOKEN_NAME); span_object.add_tag(TAG_CACHE_TYPE, "redis"); span_object.add_tag( TAG_CACHE_CMD, diff --git a/src/request.rs b/src/request.rs index f0d40a6..0aa3469 100644 --- a/src/request.rs +++ b/src/request.rs @@ -370,6 +370,7 @@ fn finish_request_context(request_id: Option, status_code: i32) -> crate::R entry_span.add_tag("http.status_code", &status_code.to_string()); if status_code >= 400 { entry_span.span_object_mut().is_error = true; + entry_span.add_tag("token", &*TOKEN_NAME); } drop(entry_span);