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

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

Golang Elastic APM - save transactions of cronjob

问题

Golang Elastic APM - 保存 cronjob 的事务

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

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

  • main.go
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "math/rand"
  6. "time"
  7. "go.elastic.co/apm/v2"
  8. )
  9. func main() {
  10. // export ELASTIC_APM_SERVICE_NAME=test
  11. t := apm.DefaultTracer().StartTransaction("test-name", "test-group")
  12. c := apm.ContextWithTransaction(context.Background(), t)
  13. worker(c)
  14. t.End()
  15. time.Sleep(time.Second * 5)
  16. }
  17. func worker(c context.Context) {
  18. span, c := apm.StartSpan(c, "one", "test-type")
  19. e := apm.DefaultTracer().Recovered(errors.New("test-error"))
  20. e.SetSpan(span)
  21. e.Send()
  22. // do some work
  23. time.Sleep(time.Duration(rand.Intn(300-100)+300) * time.Millisecond)
  24. span.End()
  25. two(c)
  26. }
  27. func two(c context.Context) {
  28. span, _ := apm.StartSpan(c, "two", "test-type")
  29. // do some other work
  30. time.Sleep(time.Duration(rand.Intn(100-50)+100) * time.Millisecond)
  31. span.End()
  32. }
  • docker-compose.yaml 用于在本地运行 APM
  1. version: '3.8'
  2. services:
  3. apm-server:
  4. image: docker.elastic.co/apm/apm-server:7.15.0
  5. cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
  6. cap_drop: ["ALL"]
  7. ports:
  8. - 8200:8200
  9. command: >
  10. apm-server -e
  11. -E apm-server.rum.enabled=true
  12. -E setup.kibana.host=kibana:5601
  13. -E setup.template.settings.index.number_of_replicas=0
  14. -E apm-server.kibana.enabled=true
  15. -E apm-server.kibana.host=kibana:5601
  16. -E output.elasticsearch.hosts=["elasticsearch:9200"]
  17. healthcheck:
  18. interval: 10s
  19. retries: 12
  20. test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/
  21. networks:
  22. - elastic
  23. elasticsearch:
  24. container_name: elasticsearch
  25. image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
  26. ports:
  27. - 9200:9200
  28. environment:
  29. - xpack.monitoring.enabled=true
  30. - xpack.watcher.enabled=false
  31. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  32. - discovery.type=single-node
  33. networks:
  34. - elastic
  35. kibana:
  36. container_name: kibana
  37. image: docker.elastic.co/kibana/kibana:7.15.0
  38. ports:
  39. - 5601:5601
  40. depends_on:
  41. - elasticsearch
  42. environment:
  43. - ELASTICSEARCH_URL=http://localhost:9200
  44. - xpack.apm.enabled=false
  45. networks:
  46. - elastic
  47. networks:
  48. elastic:
  49. 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
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "math/rand"
  6. "time"
  7. "go.elastic.co/apm/v2"
  8. )
  9. func main() {
  10. // export ELASTIC_APM_SERVICE_NAME=test
  11. t := apm.DefaultTracer().StartTransaction("test-name", "test-group")
  12. c := apm.ContextWithTransaction(context.Background(), t)
  13. worker(c)
  14. t.End()
  15. time.Sleep(time.Second * 5)
  16. }
  17. func worker(c context.Context) {
  18. span, c := apm.StartSpan(c, "one", "test-type")
  19. e := apm.DefaultTracer().Recovered(errors.New("test-error"))
  20. e.SetSpan(span)
  21. e.Send()
  22. // do some work
  23. time.Sleep(time.Duration(rand.Intn(300-100)+300) * time.Millisecond)
  24. span.End()
  25. two(c)
  26. }
  27. func two(c context.Context) {
  28. span, _ := apm.StartSpan(c, "two", "test-type")
  29. // do some other work
  30. time.Sleep(time.Duration(rand.Intn(100-50)+100) * time.Millisecond)
  31. span.End()
  32. }
  • docker-compose.yaml to run APM locally
  1. version: '3.8'
  2. services:
  3. apm-server:
  4. image: docker.elastic.co/apm/apm-server:7.15.0
  5. cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
  6. cap_drop: ["ALL"]
  7. ports:
  8. - 8200:8200
  9. command: >
  10. apm-server -e
  11. -E apm-server.rum.enabled=true
  12. -E setup.kibana.host=kibana:5601
  13. -E setup.template.settings.index.number_of_replicas=0
  14. -E apm-server.kibana.enabled=true
  15. -E apm-server.kibana.host=kibana:5601
  16. -E output.elasticsearch.hosts=["elasticsearch:9200"]
  17. healthcheck:
  18. interval: 10s
  19. retries: 12
  20. test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/
  21. networks:
  22. - elastic
  23. elasticsearch:
  24. container_name: elasticsearch
  25. image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
  26. ports:
  27. - 9200:9200
  28. environment:
  29. - xpack.monitoring.enabled=true
  30. - xpack.watcher.enabled=false
  31. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  32. - discovery.type=single-node
  33. networks:
  34. - elastic
  35. kibana:
  36. container_name: kibana
  37. image: docker.elastic.co/kibana/kibana:7.15.0
  38. ports:
  39. - 5601:5601
  40. depends_on:
  41. - elasticsearch
  42. environment:
  43. - ELASTICSEARCH_URL=http://localhost:9200
  44. - xpack.apm.enabled=false
  45. networks:
  46. - elastic
  47. networks:
  48. elastic:
  49. driver: bridge

答案1

得分: 1

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

  1. 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:

  1. 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:

确定