Search code examples
angularelasticsearchopentracingjaegerelastic-apm

Elastic APM Opentracing encountering CORS issue with Docker apm-server


Given this docker file to setup the backend services that includes: elasticsearch, apm-server, kibana, jaeger-collector, jaeger-agent, jaeger-query, grafana.

apm-server:
image: docker.elastic.co/apm/apm-server:6.8.1
ports:
  - 8200:8200
environment:
  - output.elasticsearch.hosts=['http://elasticsearch:9200']
  - apm-server.host="0.0.0.0:8200"
  - apm-server.rum.enabled=true
  - setup.kibana.host="kibana:5601"
  - setup.template.enabled=true
  - logging.to_files=false
networks:
  - elastic-jaeger

I am running Elastic APM with Opentracing from my Angular client:

const elasticApm = initApm({
  serviceName: `Test`,
  serviceUrl: `127.0.0.1:8200`,
  // serviceVersion: ``,
  active: true,
  environment: ``, // production, development, test, etc
  logLevel: `warn`, // Possible levels are: trace, debug, info, warn, error
  flushInterval: 500, // ms
  errorThrottleLimit: 20, // errors
  errorThrottleInterval: 30000, // ms
  transactionSampleRate: 1.0,
  distributedTracing: true,
  distributedTracingOrigins: ['http://foo.com']
});

const elasticTracer = createTracer(elasticApm);
this.opentracing.initGlobalTracer(elasticTracer);

I am encountering CORS issue:

Screenshot of CORS error

My goal is to hook up Angular the elastic APM's opentracing client to the services inside docker.

There are some additional issues and documents that covers CORS for apm-server:

Distributed Tracing Guide

Config with RUM enabled

It looks like the config should work, since Default value is set to ['*'], which allows everything.


Solution

  • Try using a configuration:

    version: "3"
    
    services:
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.8.1
        networks:
          - elastic-jaeger
        ports:
          - "127.0.0.1:9200:9200"
          - "127.0.0.1:9300:9300"
        restart: on-failure
        environment:
          - cluster.name=jaeger-cluster
          - discovery.type=single-node
          - http.host=0.0.0.0
          - transport.host=127.0.0.1
          - ES_JAVA_OPTS=-Xms512m -Xmx512m
          - xpack.security.enabled=false
        volumes:
          - esdata:/usr/share/elasticsearch/data
    
      apm-server:
        image: docker.elastic.co/apm/apm-server:6.8.1
        ports:
          - 8200:8200
    
        volumes:
           - ./apm-server/config/apm-server.yml:/usr/share/apm-server/apm-server.yml
    
        networks:
          - elastic-jaeger
    
      kibana:
        image: docker.elastic.co/kibana/kibana:6.8.1
        ports:
          - "127.0.0.1:5601:5601"
        restart: on-failure
        networks:
          - elastic-jaeger
    
      jaeger-collector:
        image: jaegertracing/jaeger-collector
        ports:
          - "14269:14269"
          - "14268:14268"
          - "14267:14267"
          - "9411:9411"
        networks:
          - elastic-jaeger
        restart: on-failure
        environment:
          - SPAN_STORAGE_TYPE=elasticsearch
        command: [
          "--es.server-urls=http://elasticsearch:9200",
          "--es.num-shards=1",
          "--es.num-replicas=0",
          "--log-level=error"
        ]
        depends_on:
          - elasticsearch
    
      jaeger-agent:
        image: jaegertracing/jaeger-agent
        hostname: jaeger-agent
        command: ["--collector.host-port=jaeger-collector:14267"]
        ports:
          - "5775:5775/udp"
          - "6831:6831/udp"
          - "6832:6832/udp"
          - "5778:5778"
        networks:
          - elastic-jaeger
        restart: on-failure
        environment:
          - SPAN_STORAGE_TYPE=elasticsearch
        depends_on:
          - jaeger-collector
    
      jaeger-query:
        image: jaegertracing/jaeger-query
        environment:
          - SPAN_STORAGE_TYPE=elasticsearch
          - no_proxy=localhost
        ports:
          - "16686:16686"
          - "16687:16687"
        networks:
          - elastic-jaeger
        restart: on-failure
        command: [
          "--es.server-urls=http://elasticsearch:9200",
          "--span-storage.type=elasticsearch",
          "--log-level=debug"
        ]
        depends_on:
          - jaeger-agent
    
      grafana:
        image: grafana/grafana
        ports:
          - 3999:3000
        volumes:
          - ./grafana-data:/var/lib/grafana
        networks:
          - elastic-jaeger
    
    volumes:
      esdata:
        driver: local
    
    networks:
      elastic-jaeger:
        driver: bridge 
    
    

    where the file apm-server/config/apm-server.yml has your config content:

    apm-server.rum.enabled: true
    apm-server.rum.event_rate.limit: 300
    apm-server.rum.event_rate.lru_size: 1000
    apm-server.rum.allow_origins: ['*']
    apm-server.rum.library_pattern: "node_modules|bower_components|~"
    apm-server.rum.exclude_from_grouping: "^/webpack"
    apm-server.rum.source_mapping.cache.expiration: 5m
    apm-server.rum.source_mapping.index_pattern: "apm-*-sourcemap*"
    output.elasticsearch.hosts: ["http://elasticsearch:9200"]
    apm-server.host: "0.0.0.0:8200"
    setup.kibana.host: "kibana:5601"
    setup.template.enabled: true
    logging.to_files: false
    
    

    Note the rum.allow_origins option that you can configure to resolve the CORS issue. https://www.elastic.co/guide/en/apm/agent/rum-js/master/configuring-cors.html