Worker works under "Preview" and doesn't in Live

I have a worker that sends data to Elastic:
indent preformatted text by 4 spaces

addEventListener('fetch', event => {
  event.respondWith(fetchAndLog(event));
})



async function logToElk(request, response) {
  let ray  = request.headers.get('cf-ray') || '';
  let id   = ray.slice(0, -4);
  var data = {
        'timestamp':  (new Date(Date.now())).toISOString(),
        'ua':         request.headers.get('user-agent') || '-',
        'url':        request.url

  };
  console.log("data: " +JSON.stringify(data))
  const url1 = 'http://server_host:9200/cloudflare.test.shift/_doc/'+id+'?pretty'
  console.log("url: " +url1)
  await fetch(url1, {
    method: 'PUT',
    body: JSON.stringify(data),
    headers: new Headers({
      'Content-Type':'application/json',
      'Authorization':'Basic pass'
        })
      }).then((response) => {
        return response.json();
      }).then((data) => {
        console.log(data.error);
      });
    }

async function fetchAndLog(event) {

  const response = await fetch(event.request);

  event.waitUntil(logToElk(event.request, response));
  return response;
}

indent preformatted text by 4 spaces

Under Preview mode it works fine and i see new events in Elastic and kibana, but when i go live - i got nothing.

Worker route configured as:
*domain10.com/*

i go to: domain10 .com and automatically reach https://domain10 .com and receive the content

In your logToElk function change await fetch for return fetch

Hello!
Thank you for your advice

chenged
await fetch(url1, { to return fetch(url1, {
and got no result (

the main function is:

async function fetchAndLog(event) {
  const response = await fetch(event.request);
  event.waitUntil(logToElk(event.request, response));
  return response;
}

Try reading userAgent, ray, and URL upfront.

addEventListener('fetch', event => {
  event.respondWith(fetchAndLog(event));
})

async function logToElk({userAgent, ray, url}) {
	
	const id   = ray.slice(0, -4);
	const data = {
		'timestamp':  (new Date(Date.now())).toISOString(),
		'ua': userAgent,
		'url': url
	};

	const url1 = 'http://server_host:9200/cloudflare.test.shift/_doc/'+id+'?pretty'
  
	return fetch(url1, {
		method: 'PUT',
		body: JSON.stringify(data),
		headers:  {
			'Content-Type': 'application/json',
			'Authorization': 'Basic pass'
        }
	})
}

async function fetchAndLog(event) {
	const { request } = event
	const {headers, url} = request
	const ray  = headers.get('cf-ray') || ''
	const userAgent = headers.get('user-agent') || '-'
	event.waitUntil(logToElk({userAgent, ray, url}))
	return await fetch(request)
}

Hello, thanks
I’ve tried such a way, and still no result.

Lately i’ve tried such a script:

addEventListener('fetch', event => {
  event.respondWith(fetchAndLog(event));
})
async function fetchAndLog(event) {
  let response = await fetch(event.request);
//  event.waitUntil(logToElk(event.request, response));
  const ray  = event.request.headers.get('cf-ray') || '';
  const id   = ray.slice(0, -4);
  const data = {
        'timestamp':  (new Date(Date.now())).toISOString(),
        'ua':         event.request.headers.get('user-agent') || '-',
        'url':        event.request.url
  };
    console.log("data: " +JSON.stringify(data))
  const url1 = 'http://server:9200/cloudflare.test/_doc/'+id+'?pretty'
  console.log("url: " +url1)
  return fetch(url1, {
    method: 'PUT',
    body: JSON.stringify(data, null, 0),
    headers: new Headers({
      'Content-Type':'application/json',
      'Authorization':'Basic authpass'
        })
      })

  return response;
}

so i am trying to return fetch result to client - and got in CF preview:

{
  "_index" : "cloudflare.test",
  "_type" : "_doc",
  "_id" : "623fe7729fe80c01",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 237,
  "_primary_term" : 1
}

But in browser i got:

# 522 Origin Connection Time-out

cloudflare-nginx

also want to mention that ELK server is totally opened for requests on 9200 port