/*tracing.ts*/import*asopentelemetryfrom"@opentelemetry/sdk-node";import{getNodeAutoInstrumentations,}from"@opentelemetry/auto-instrumentations-node";import{OTLPTraceExporter,}from"@opentelemetry/exporter-trace-otlp-proto";import{OTLPMetricExporter}from"@opentelemetry/exporter-metrics-otlp-proto";import{PeriodicExportingMetricReader}from"@opentelemetry/sdk-metrics";constsdk=newopentelemetry.NodeSDK({traceExporter:newOTLPTraceExporter({// optional - default url is http://localhost:4318/v1/tracesurl:"<your-otlp-endpoint>/v1/traces",// optional - collection of custom headers to be sent with each request, empty by defaultheaders:{},}),metricReader:newPeriodicExportingMetricReader({exporter:newOTLPMetricExporter({url:'<your-otlp-endpoint>/v1/metrics',// url is optional and can be omitted - default is http://localhost:4318/v1/metricsheaders:{},// an optional object containing custom headers to be sent with each request}),}),instrumentations:[getNodeAutoInstrumentations()],});sdk.start();
/*tracing.js*/constopentelemetry=require("@opentelemetry/sdk-node");const{getNodeAutoInstrumentations,}=require("@opentelemetry/auto-instrumentations-node");const{OTLPTraceExporter,}=require("@opentelemetry/exporter-trace-otlp-proto");const{OTLPMetricExporter}=require("@opentelemetry/exporter-metrics-otlp-proto");const{PeriodicExportingMetricReader}=require('@opentelemetry/sdk-metrics');constsdk=newopentelemetry.NodeSDK({traceExporter:newOTLPTraceExporter({// optional - default url is http://localhost:4318/v1/tracesurl:"<your-otlp-endpoint>/v1/traces",// optional - collection of custom headers to be sent with each request, empty by defaultheaders:{},}),metricReader:newPeriodicExportingMetricReader({exporter:newOTLPMetricExporter({url:'<your-otlp-endpoint>/v1/metrics',// url is optional and can be omitted - default is http://localhost:4318/v1/metricsheaders:{},// an optional object containing custom headers to be sent with each requestconcurrencyLimit:1,// an optional limit on pending requests}),}),instrumentations:[getNodeAutoInstrumentations()],});sdk.start();
[OpenTelemetry Collector exporters with gRPC][]和[OpenTelemetry Collector
exporters with protobuf][]只能与 Node.js 一起工作,因此你只能使用[OpenTelemetry
Collector exporters with HTTP][]。
To receive telemetry from a web application you need to allow the browsers of
your end-users to send data to your collector. If your web application is
accessible from the public internet, you also have to make your collector
accessible for everyone.
It is recommended that you do not expose your collector directly, but that you
put a reverse proxy (NGINX, Apache HTTP Server, ...) in front of it. The reverse
proxy can take care of SSL-offloading, setting the right CORS headers, and many
other features specific to web applications.
Below you will find a configuration for the popular NGINX web server to get you
started:
server{listen80default_server;server_name_;location/{# Take care of preflight requestsif($request_method='OPTIONS'){add_header'Access-Control-Max-Age'1728000;add_header'Access-Control-Allow-Origin''name.of.your.website.example.com'always;add_header'Access-Control-Allow-Headers''Accept,Accept-Language,Content-Language,Content-Type'always;add_header'Access-Control-Allow-Credentials''true'always;add_header'Content-Type''text/plaincharset=UTF-8';add_header'Content-Length'0;return204;}add_header'Access-Control-Allow-Origin''name.of.your.website.example.com'always;add_header'Access-Control-Allow-Credentials''true'always;add_header'Access-Control-Allow-Headers''Accept,Accept-Language,Content-Language,Content-Type'always;proxy_http_version1.1;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_passhttp://collector:4318;}}