调用opentelemetry收集器时出现CORS错误

问题描述 投票:0回答:1

我可以使用以下配置从本地 javascript 项目 (http://localhost:3000) 正常将日志发送到 OpenTelemetry:

receivers:
  otlp:
    protocols:
      http:
        cors:
          allowed_origins:
            - http://localhost:3000
          max_age: 7200
        endpoint: 0.0.0.0:4318
exporters:
  debug:
    verbosity: detailed
service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [debug]
    metrics:
      receivers: [otlp]
      exporters: [debug]
    logs:
      receivers: [otlp]
      exporters: [debug]

请注意,我使用的是原始收集器 => https://github.com/open-telemetry/opentelemetry-collector

但是使用 https://github.com/open-telemetry/opentelemetry-collector-contrib(opentelemetry 收集器的超集)时我无法再将日志发送到收集器

我得到了

Access to resource at 'http://localhost:4318/v1/traces' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

有什么想法吗?

open-telemetry open-telemetry-collector
1个回答
0
投票

无法为

opentelemetry-collector-contrib
图像配置CORS策略,因此前端应用程序(在浏览器上运行)似乎无法直接连接到它,尽管我在opentelemetry-collector-contrib中没有找到任何文档这证实了这一点。

(注意:收集器可以通过邮递员或卷曲调用,因为那里没有 CORS 策略,也可以从后端服务调用)

因此 CORS 策略应该配置在其他地方。

我尝试的是使用像nginx这样的反向代理,它将在浏览器和收集器之间发挥中介者的作用

以下是该代理的配置:

server {
        server_name localhost;
        listen 80;
        
        location /v1/choose_an_api-endpoint {
            proxy_pass http://localhost:4318/v1/traces; # the collector url

            if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin "http://localhost:3000"; # consider that the frontend application is running on the port 3000 locally
                add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
                add_header Access-Control-Allow-Headers "Authorization, Content-Type, Origin, Accept";
                add_header Access-Control-Allow-Credentials true;
                return 204;
            }
        }
}

通过添加所需的标头,调用将从代理重定向到收集器

© www.soinside.com 2019 - 2024. All rights reserved.