Golang Elastic APM – 保存定时任务的事务记录

huangapple go评论75阅读模式
英文:

Golang Elastic APM - save transactions of cronjob

问题

Golang Elastic APM - 保存 cronjob 的事务

我需要将 Elastic APM 连接到我的 cronjob,但是当我按照 APM 文档 进行操作时,我发现没有事务甚至没有服务注册。

我该如何连接 APM 并为 cronjob 注册事务,而不是为 api 注册事务?

  • main.go
package main

import (
	"context"
	"errors"
	"math/rand"
	"time"

	"go.elastic.co/apm/v2"
)

func main() {
	// export ELASTIC_APM_SERVICE_NAME=test
	t := apm.DefaultTracer().StartTransaction("test-name", "test-group")
	c := apm.ContextWithTransaction(context.Background(), t)
	worker(c)
	t.End()

	time.Sleep(time.Second * 5)
}

func worker(c context.Context) {
	span, c := apm.StartSpan(c, "one", "test-type")

	e := apm.DefaultTracer().Recovered(errors.New("test-error"))
	e.SetSpan(span)
	e.Send()

	// do some work
	time.Sleep(time.Duration(rand.Intn(300-100)+300) * time.Millisecond)
	span.End()

	two(c)
}

func two(c context.Context) {
	span, _ := apm.StartSpan(c, "two", "test-type")
	// do some other work
	time.Sleep(time.Duration(rand.Intn(100-50)+100) * time.Millisecond)
	span.End()
}
  • docker-compose.yaml 用于在本地运行 APM
version: '3.8'

services:

  apm-server:
    image: docker.elastic.co/apm/apm-server:7.15.0
    cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
    cap_drop: ["ALL"]
    ports:
    - 8200:8200
    command: >
       apm-server -e
         -E apm-server.rum.enabled=true
         -E setup.kibana.host=kibana:5601
         -E setup.template.settings.index.number_of_replicas=0
         -E apm-server.kibana.enabled=true
         -E apm-server.kibana.host=kibana:5601
         -E output.elasticsearch.hosts=["elasticsearch:9200"]       
    healthcheck:
      interval: 10s
      retries: 12
      test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/
    networks:
      - elastic

  elasticsearch:
   container_name: elasticsearch
   image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
   ports:
    - 9200:9200
   environment:
    - xpack.monitoring.enabled=true
    - xpack.watcher.enabled=false
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    - discovery.type=single-node
   networks:
    - elastic

  kibana:
   container_name: kibana
   image: docker.elastic.co/kibana/kibana:7.15.0
   ports:
    - 5601:5601
   depends_on:
    - elasticsearch
   environment:
    - ELASTICSEARCH_URL=http://localhost:9200
    - xpack.apm.enabled=false
   networks:
    - elastic

  
networks:
  elastic:
    driver: bridge
英文:

Golang Elastic APM - save transactions of cronjob

I need to connect Elastic APM to my cronjob however I when I follow documentation for APM I see no transactions or even service registered.

How can I connect APM and register transactions for cronjob and not for api

  • main.go
package main

import (
	"context"
	"errors"
	"math/rand"
	"time"

	"go.elastic.co/apm/v2"
)

func main() {
	// export ELASTIC_APM_SERVICE_NAME=test
	t := apm.DefaultTracer().StartTransaction("test-name", "test-group")
	c := apm.ContextWithTransaction(context.Background(), t)
	worker(c)
	t.End()

	time.Sleep(time.Second * 5)
}

func worker(c context.Context) {
	span, c := apm.StartSpan(c, "one", "test-type")

	e := apm.DefaultTracer().Recovered(errors.New("test-error"))
	e.SetSpan(span)
	e.Send()

	// do some work
	time.Sleep(time.Duration(rand.Intn(300-100)+300) * time.Millisecond)
	span.End()

	two(c)
}

func two(c context.Context) {
	span, _ := apm.StartSpan(c, "two", "test-type")
	// do some other work
	time.Sleep(time.Duration(rand.Intn(100-50)+100) * time.Millisecond)
	span.End()
}
  • docker-compose.yaml to run APM locally
version: '3.8'

services:

  apm-server:
    image: docker.elastic.co/apm/apm-server:7.15.0
    cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
    cap_drop: ["ALL"]
    ports:
    - 8200:8200
    command: >
       apm-server -e
         -E apm-server.rum.enabled=true
         -E setup.kibana.host=kibana:5601
         -E setup.template.settings.index.number_of_replicas=0
         -E apm-server.kibana.enabled=true
         -E apm-server.kibana.host=kibana:5601
         -E output.elasticsearch.hosts=["elasticsearch:9200"]
    healthcheck:
      interval: 10s
      retries: 12
      test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/
    networks:
      - elastic

  elasticsearch:
   container_name: elasticsearch
   image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
   ports:
    - 9200:9200
   environment:
    - xpack.monitoring.enabled=true
    - xpack.watcher.enabled=false
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    - discovery.type=single-node
   networks:
    - elastic

  kibana:
   container_name: kibana
   image: docker.elastic.co/kibana/kibana:7.15.0
   ports:
    - 5601:5601
   depends_on:
    - elasticsearch
   environment:
    - ELASTICSEARCH_URL=http://localhost:9200
    - xpack.apm.enabled=false
   networks:
    - elastic

  
networks:
  elastic:
    driver: bridge

答案1

得分: 1

你的追踪器可能在作业退出之前没有发送事务。为了确保发送事务,可以在main()函数中的time.Sleep之后(或替换它)添加以下代码:

apm.DefaultTracer().Flush(nil)
英文:

Your tracer is probably not sending the transaction before the job exits. To ensure it does, add after (or replace) the time.Sleep in main() with:

apm.DefaultTracer().Flush(nil)

huangapple
  • 本文由 发表于 2022年9月26日 02:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/73846719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定