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

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 894aeac7b feat: opentelemetry plugin config collector.address support 
specify https scheme (#8823)
894aeac7b is described below

commit 894aeac7b651e90c4a8b321e83aa9805ce963c21
Author: roketyyang <[email protected]>
AuthorDate: Wed Feb 15 09:51:01 2023 +0800

    feat: opentelemetry plugin config collector.address support specify https 
scheme (#8823)
    
    Co-authored-by: 罗泽轩 <[email protected]>
    Co-authored-by: roketyyang <[email protected]>
---
 apisix/plugins/opentelemetry.lua        | 33 ++++++++++++++-------------------
 docs/en/latest/plugins/opentelemetry.md |  2 +-
 docs/zh/latest/plugins/opentelemetry.md |  2 +-
 rockspec/apisix-master-0.rockspec       |  2 +-
 t/plugin/opentelemetry.t                |  1 +
 t/plugin/opentelemetry2.t               |  2 +-
 6 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/apisix/plugins/opentelemetry.lua b/apisix/plugins/opentelemetry.lua
index c0e3a74e4..f8013e6f7 100644
--- a/apisix/plugins/opentelemetry.lua
+++ b/apisix/plugins/opentelemetry.lua
@@ -36,10 +36,9 @@ local span_status = 
require("opentelemetry.trace.span_status")
 local resource_new = require("opentelemetry.resource").new
 local attr = require("opentelemetry.attribute")
 
-local context_storage = require("opentelemetry.context_storage")
-local context = require("opentelemetry.context").new(context_storage)
-local carrier_new = require("opentelemetry.trace.propagation.carrier").new
-local trace_context = require("opentelemetry.trace.propagation.trace_context")
+local context = require("opentelemetry.context").new()
+local trace_context_propagator =
+                
require("opentelemetry.trace.propagation.text_map.trace_context_propagator").new()
 
 local ngx     = ngx
 local ngx_var = ngx.var
@@ -310,7 +309,7 @@ function _M.rewrite(conf, api_ctx)
     end
 
     -- extract trace context from the headers of downstream HTTP request
-    local upstream_context = trace_context.extract(context, carrier_new())
+    local upstream_context = trace_context_propagator:extract(context, ngx.req)
     local attributes = {
         attr.string("service", api_ctx.service_name),
         attr.string("route", api_ctx.route_name),
@@ -333,27 +332,24 @@ function _M.rewrite(conf, api_ctx)
         kind = span_kind.server,
         attributes = attributes,
     })
-    ctx:attach()
+    api_ctx.otel_context_token = ctx:attach()
 
     -- inject trace context into the headers of upstream HTTP request
-    trace_context.inject(ctx, carrier_new())
+    trace_context_propagator:inject(ctx, ngx.req)
 end
 
 
 function _M.delayed_body_filter(conf, api_ctx)
-    if ngx.arg[2] then
+    if api_ctx.otel_context_token and ngx.arg[2] then
         local ctx = context:current()
-        if not ctx then
-            return
-        end
-
-        local upstream_status = core.response.get_upstream_status(api_ctx)
-        ctx:detach()
+        ctx:detach(api_ctx.otel_context_token)
+        api_ctx.otel_context_token = nil
 
         -- get span from current context
         local span = ctx:span()
+        local upstream_status = core.response.get_upstream_status(api_ctx)
         if upstream_status and upstream_status >= 500 then
-            span:set_status(span_status.error,
+            span:set_status(span_status.ERROR,
                             "upstream response status: " .. upstream_status)
         end
 
@@ -365,15 +361,14 @@ end
 -- body_filter maybe not called because of empty http body response
 -- so we need to check if the span has finished in log phase
 function _M.log(conf, api_ctx)
-    local ctx = context:current()
-    if ctx then
+    if api_ctx.otel_context_token then
         -- ctx:detach() is not necessary, because of ctx is stored in ngx.ctx
         local upstream_status = core.response.get_upstream_status(api_ctx)
 
         -- get span from current context
-        local span = ctx:span()
+        local span = context:current():span()
         if upstream_status and upstream_status >= 500 then
-            span:set_status(span_status.error,
+            span:set_status(span_status.ERROR,
                     "upstream response status: " .. upstream_status)
         end
 
diff --git a/docs/en/latest/plugins/opentelemetry.md 
b/docs/en/latest/plugins/opentelemetry.md
index 10d6826b2..d8ba30234 100644
--- a/docs/en/latest/plugins/opentelemetry.md
+++ b/docs/en/latest/plugins/opentelemetry.md
@@ -58,7 +58,7 @@ You can set up the collector by configuring it in you 
configuration file (`conf/
 | trace_id_source                            | enum    | random                
                            | Source of the trace ID. Valid values are `random` 
or `x-request-id`. When set to `x-request-id`, the value of the `x-request-id` 
header will be used as trace ID. Make sure that is matches the regex pattern 
`[0-9a-f]{32}`. |
 | resource                                   | object  |                       
                            | Additional 
[resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md)
 appended to the trace.                                                         
                  |
 | collector                                  | object  | {address = 
"127.0.0.1:4318", request_timeout = 3} | OpenTelemetry Collector configuration. 
                                                                                
                                                                                
                       |
-| collector.address                          | string  | 127.0.0.1:4318        
                            | Collector address.                                
                                                                                
                                                                                
            |
+| collector.address                          | string  | 127.0.0.1:4318        
                            | Collector address. If the collector serves on 
https, use https://127.0.0.1:4318 as the address.                               
                                                                                
                     |
 | collector.request_timeout                  | integer | 3                     
                            | Report request timeout in seconds.                
                                                                                
                                                                                
            |
 | collector.request_headers                  | object  |                       
                            | Report request HTTP headers.                      
                                                                                
                                                                                
            |
 | batch_span_processor                       | object  |                       
                            | Trace span processor.                             
                                                                                
                                                                                
            |
diff --git a/docs/zh/latest/plugins/opentelemetry.md 
b/docs/zh/latest/plugins/opentelemetry.md
index 9bc835ac7..d8509eb19 100644
--- a/docs/zh/latest/plugins/opentelemetry.md
+++ b/docs/zh/latest/plugins/opentelemetry.md
@@ -51,7 +51,7 @@ title: opentelemetry
 | trace_id_source                            | enum    | random                
                            | trace ID 的来源。有效值为:`random` 或 `x-request-id`。当设置为 
`x-request-id` 时,`x-request-id` 头的值将用作跟踪 ID。请确保当前请求 ID 是符合 TraceID 
规范的:`[0-9a-f]{32}`。 |
 | resource                                   | object  |                       
                            | 追加到 trace 的额外 
[resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md)。
 |
 | collector                                  | object  | {address = 
"127.0.0.1:4318", request_timeout = 3} | OpenTelemetry Collector 配置。 |
-| collector.address                          | string  | 127.0.0.1:4318        
                            | 数据采集服务的地址。             |
+| collector.address                          | string  | 127.0.0.1:4318        
                            | 数据采集服务的地址。如果数据采集服务使用的是 HTTPS 协议,可以将 address 设置为 
https://127.0.0.1:4318。 |
 | collector.request_timeout                  | integer | 3                     
                            | 数据采集服务上报请求超时时长,单位为秒。 |
 | collector.request_headers                  | object  |                       
                            | 数据采集服务上报请求附加的 HTTP 请求头。 |
 | batch_span_processor                       | object  |                       
                            | trace span 处理器参数配置。 |
diff --git a/rockspec/apisix-master-0.rockspec 
b/rockspec/apisix-master-0.rockspec
index eac63bbd6..a45cfbc77 100644
--- a/rockspec/apisix-master-0.rockspec
+++ b/rockspec/apisix-master-0.rockspec
@@ -73,7 +73,7 @@ dependencies = {
     "inspect == 3.1.1",
     "lualdap = 1.2.6-1",
     "lua-resty-rocketmq = 0.3.0-0",
-    "opentelemetry-lua = 0.1-3",
+    "opentelemetry-lua = 0.2-3",
     "net-url = 0.9-1",
     "xml2lua = 1.5-2",
     "nanoid = 0.1-1",
diff --git a/t/plugin/opentelemetry.t b/t/plugin/opentelemetry.t
index 0a1b87271..5adc158e9 100644
--- a/t/plugin/opentelemetry.t
+++ b/t/plugin/opentelemetry.t
@@ -40,6 +40,7 @@ _EOC_
 local client = require("opentelemetry.trace.exporter.http_client")
 client.do_request = function()
     ngx.log(ngx.INFO, "opentelemetry export span")
+    return "ok"
 end
 _EOC_
 
diff --git a/t/plugin/opentelemetry2.t b/t/plugin/opentelemetry2.t
index 984042c26..052cdbc12 100644
--- a/t/plugin/opentelemetry2.t
+++ b/t/plugin/opentelemetry2.t
@@ -42,6 +42,7 @@ _EOC_
 local client = require("opentelemetry.trace.exporter.http_client")
 client.do_request = function()
     ngx.log(ngx.INFO, "opentelemetry export span")
+    return "ok"
 end
 local ctx_new = require("opentelemetry.context").new
 require("opentelemetry.context").new = function (...)
@@ -136,7 +137,6 @@ qr/(opentelemetry export span|opentelemetry context 
current|plugin body_filter p
 plugin body_filter phase
 plugin body_filter phase
 opentelemetry context current
-opentelemetry context current
 opentelemetry export span
 
 

Reply via email to