|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +use t::APISIX 'no_plan'; |
| 19 | + |
| 20 | +repeat_each(1); |
| 21 | +no_long_string(); |
| 22 | +no_root_location(); |
| 23 | +log_level("info"); |
| 24 | + |
| 25 | +add_block_preprocessor(sub { |
| 26 | + my ($block) = @_; |
| 27 | + |
| 28 | + if (!$block->extra_yaml_config) { |
| 29 | + my $extra_yaml_config = <<_EOC_; |
| 30 | +plugins: |
| 31 | + - http-logger |
| 32 | + - opentelemetry |
| 33 | +plugin_attr: |
| 34 | + opentelemetry: |
| 35 | + set_ngx_var: true |
| 36 | + batch_span_processor: |
| 37 | + max_export_batch_size: 1 |
| 38 | + inactive_timeout: 0.5 |
| 39 | +_EOC_ |
| 40 | + $block->set_value("extra_yaml_config", $extra_yaml_config); |
| 41 | + } |
| 42 | + |
| 43 | + my $upstream_server_config = $block->upstream_server_config // <<_EOC_; |
| 44 | + set \$opentelemetry_context_traceparent ""; |
| 45 | + set \$opentelemetry_trace_id ""; |
| 46 | + set \$opentelemetry_span_id ""; |
| 47 | + access_log logs/error.log opentelemetry_log; |
| 48 | +_EOC_ |
| 49 | + |
| 50 | + $block->set_value("upstream_server_config", $upstream_server_config); |
| 51 | + |
| 52 | + my $http_config = $block->http_config // <<_EOC_; |
| 53 | + log_format opentelemetry_log '{"time": "\$time_iso8601","opentelemetry_context_traceparent": "\$opentelemetry_context_traceparent","opentelemetry_trace_id": "\$opentelemetry_trace_id","opentelemetry_span_id": "\$opentelemetry_span_id","remote_addr": "\$remote_addr","uri": "\$uri"}'; |
| 54 | +_EOC_ |
| 55 | + |
| 56 | + $block->set_value("http_config", $http_config); |
| 57 | + |
| 58 | + if (!$block->extra_init_by_lua) { |
| 59 | + my $extra_init_by_lua = <<_EOC_; |
| 60 | +-- mock exporter http client |
| 61 | +local client = require("opentelemetry.trace.exporter.http_client") |
| 62 | +client.do_request = function() |
| 63 | + ngx.log(ngx.INFO, "opentelemetry export span") |
| 64 | + return "ok" |
| 65 | +end |
| 66 | +_EOC_ |
| 67 | + |
| 68 | + $block->set_value("extra_init_by_lua", $extra_init_by_lua); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + if (!$block->request) { |
| 73 | + $block->set_value("request", "GET /t"); |
| 74 | + } |
| 75 | + |
| 76 | + $block; |
| 77 | +}); |
| 78 | + |
| 79 | +run_tests; |
| 80 | + |
| 81 | +__DATA__ |
| 82 | +
|
| 83 | +=== TEST 1: add plugin metadata |
| 84 | +--- config |
| 85 | + location /t { |
| 86 | + content_by_lua_block { |
| 87 | + local t = require("lib.test_admin").test |
| 88 | + local code, body = t('/apisix/admin/plugin_metadata/http-logger', |
| 89 | + ngx.HTTP_PUT, |
| 90 | + [[{ |
| 91 | + "log_format": { |
| 92 | + "opentelemetry_context_traceparent": "$opentelemetry_context_traceparent", |
| 93 | + "opentelemetry_trace_id": "$opentelemetry_trace_id", |
| 94 | + "opentelemetry_span_id": "$opentelemetry_span_id" |
| 95 | + } |
| 96 | + }]] |
| 97 | + ) |
| 98 | + if code >= 300 then |
| 99 | + ngx.status = code |
| 100 | + return body |
| 101 | + end |
| 102 | +
|
| 103 | + local code, body = t('/apisix/admin/routes/1', |
| 104 | + ngx.HTTP_PUT, |
| 105 | + [[{ |
| 106 | + "plugins": { |
| 107 | + "http-logger": { |
| 108 | + "uri": "http://127.0.0.1:1980/log", |
| 109 | + "batch_max_size": 1, |
| 110 | + "max_retry_count": 1, |
| 111 | + "retry_delay": 2, |
| 112 | + "buffer_duration": 2, |
| 113 | + "inactive_timeout": 2, |
| 114 | + "concat_method": "new_line" |
| 115 | + }, |
| 116 | + "opentelemetry": { |
| 117 | + "sampler": { |
| 118 | + "name": "always_on" |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + "upstream": { |
| 123 | + "nodes": { |
| 124 | + "127.0.0.1:1982": 1 |
| 125 | + }, |
| 126 | + "type": "roundrobin" |
| 127 | + }, |
| 128 | + "uri": "/hello" |
| 129 | + }]] |
| 130 | + ) |
| 131 | +
|
| 132 | + if code >=300 then |
| 133 | + ngx.status = code |
| 134 | + end |
| 135 | + ngx.say(body) |
| 136 | + } |
| 137 | + } |
| 138 | +--- request |
| 139 | +GET /t |
| 140 | +--- response_body |
| 141 | +passed |
| 142 | +
|
| 143 | +
|
| 144 | +
|
| 145 | +=== TEST 2: trigger opentelemetry with open set variables |
| 146 | +--- request |
| 147 | +GET /hello |
| 148 | +--- response_body |
| 149 | +hello world |
| 150 | +--- wait: 1 |
| 151 | +--- grep_error_log eval |
| 152 | +qr/opentelemetry export span/ |
| 153 | +--- grep_error_log_out |
| 154 | +opentelemetry export span |
| 155 | +--- error_log eval |
| 156 | +qr/request log: \{.*"opentelemetry_context_traceparent":"00-\w{32}-\w{16}-01".*\}/ |
| 157 | +
|
| 158 | +
|
| 159 | +
|
| 160 | +=== TEST 3: trigger opentelemetry with disable set variables |
| 161 | +--- yaml_config |
| 162 | +plugin_attr: |
| 163 | + opentelemetry: |
| 164 | + set_ngx_var: false |
| 165 | +--- request |
| 166 | +GET /hello |
| 167 | +--- response_body |
| 168 | +hello world |
| 169 | +--- error_log eval |
| 170 | +qr/request log: \{.*"opentelemetry_context_traceparent":"".*\}/ |
0 commit comments