//引入opentelemetry
import opentelemetry from "@opentelemetry/api";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor} from "@opentelemetry/sdk-trace-base";
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';


import promise from 'es6-promise';
promise.polyfill();

 
// Optionally register automatic instrumentation libraries
registerInstrumentations({
  instrumentations: [],
});
 
const resource =
  Resource.default().merge(
    new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: "service-name-here",
      [SemanticResourceAttributes.SERVICE_VERSION]: "0.1.0",
      [SemanticResourceAttributes.PROCESS_COMMAND_LINE]:"-DAPP_NAME=szmh_front"
    })
  );
 
const provider = new WebTracerProvider({
    resource: resource,
});
const exporter = new ConsoleSpanExporter();
const processor = new BatchSpanProcessor(exporter);
provider.addSpanProcessor(processor);

provider.addSpanProcessor(new SimpleSpanProcessor(
  new OTLPTraceExporter({
    url: 'http://otel-collector.cecf.base:8580/v1/traces',//url配置成指定后端collector服务
    //url: 'http://otel-collector.cecf.base:31596/v1/traces',//url配置成指定后端collector服务
    // url:'http://collector.test.obs.js.sgcc.com.cn/v1/traces'
    // url:'http://123.56.83.105:30080/v1/traces',
    headers: {

    }
  })
 
));
 
provider.register();



 
//使用getTracer方法
const tracer = opentelemetry.trace.getTracer(
    'my-service-tracer'
  );

// //   // 创建Span,必须有end()结束
// tracer.startActiveSpan('main', (span) => {
//     for (let i = 0; i < 10; i += 1) {
//       console.log(i);
//     }
//    
//     //一定要写end()方法结束
//     span.end();
//   });

  // const mainWork = () => {
  //   tracer.startActiveSpan('box', (parentSpan) => {
  //     for (let i = 0; i < 3; i += 1) {
  //       doWork(i);
  //     }
  //     // Be sure to end the parent span!
  //     parentSpan.end();
  //   });
  // };
  
  // const doWork = (i) => {
  //   tracer.startActiveSpan(`doWork:${i}`, (span) => {
  //     // simulate some random work.
  //     for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
  //       // empty
  //     }
  
  //     // Make sure to end this child span! If you don't,
  //     // it will continue to track work beyond 'doWork'!
  //     span.end();
  //   });
  // };
