ããã«ã¡ã¯ãSRE/ãã¼ã¿ã¹ãã¢ãã¼ã ã®é£¯å¡ã§ãã
ç§ãã¡ã®ãã¼ã ã§ã¯ãã¼ã¿ãã¼ã¹ã代çã§æä½ãããæ å ±ãåå¾ããããããµã¼ãã¹ãããã¤ãä½ããããããããã¯ããã¼ã ãå©ç¨ã§ããããã« gRPC çµç±ã§æä¾ãã¦ãã¾ããã¨ããã§ãããæ¥çªç¶ãåæ£ãã¬ã¼ã·ã³ã°ãæ´»ç¨ãã¦ãããã¨ã«ãªã£ãã®ã§ãããªãã®ãã¼ã ã®ãµã¼ãã¹ã対å¿ããã¦ãã ãããã¨ãé¡ãããããã©ãããã°ããã§ããããï¼ç§ã¯ããã¾ã§ã«ãããããªã«ã³ãã¡ã¬ã³ã¹ã§åæ£ãã¬ã¼ã·ã³ã°ã OpenTelemetry ã«ã¤ãã¦ã®è¬æ¼ãèãã¦ããã®ã§ãç念ã¯ç解ããã便å©ããã ãå°å ¥ãã¦ã¿ãããã¨æã£ããã¨ã¯ä½åº¦ãããã¾ããããããå®éã«å°å ¥ãããã¨ã㦠SDK ã®ããã¥ã¡ã³ããéãã¦ã¿ãã¨ãç解ããªããã°ãããªãï¼ããã«è¦ããï¼æ¦å¿µãã使ãæ¹ããã¹ã¿ã¼ããªãã¨ãããªãï¼ããã«è¦ããï¼API ã®æ°ã«å§åããã¦ãã¾ããå¾åãã«ãã¦ãã¾ã£ã¦ãã¾ããã
ä»åã¯ä¸å¿µçºèµ·ãã¦ããã¾ã§ç§ã«å°å ¥ããããããã¦ããé¨åã調ã¹ãå ·ä½çã«å¿ è¦ãªã¢ã¯ã·ã§ã³ãèªåãªãã«æ´çãããã¨ãã§ãã¾ããããã®è¨äºã§ã¯ãèªè ã®çæ§ãåãããã«ãããªãã®ãã¼ã ãä½ã£ã¦ããã¢ããªã±ã¼ã·ã§ã³ãåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ããã¦ãã ãããã¨è¨ãããã¨ãã®ããã« opentelemetry-go ãæä½éå©ç¨å¯è½ã«ãªãç¥èãæä¾ãã¾ãã
ä½ãå¦ã¶å¿ è¦ããããï¼
OpenTelemetry ãã¢ããªã±ã¼ã·ã§ã³ã«å°å ¥ãããã¨ããã¨ãã«ã¯å¤§ããåãã¦2ã¤ã®å¦çãæ¸ããã¨ã«ãªãã¾ãã
- Span ã®ä½ãè¾¼ã¿
- Trace ã®éä¿¡ã«é¢ããè¨å®
Span ã¨ããã®ã¯åæ£ãã¬ã¼ã·ã³ã°ã®èª¬æã§ããåºã¦ãã以ä¸ã®ãããªå³ã®ã²ã¨ã¤ã®å¸¯ã®ãã¨ã§ããããããã® Span ã¯å¦çã®ååãå¦çã®éå§ãçµäºæéããã®ä»ã®è£å©æ å ± (attributes) ãå«ã¿ã¾ããè¤æ°ã® Span ãé¢é£ä»ãããã¨ã«ãã Trace ãæ§æããã¾ããã¢ããªã±ã¼ã·ã§ã³ãåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ãããã¨ããã®ã¯é¢å¿ãããé¨åã« Span ãä½ããSpan ã®é¢é£ä»ããè¨å®ããå¦çãã³ã¼ãã«åãè¾¼ããã¨ã ã¨ããã¾ãã
ãã㧠2. ã®ãTrace ã®éä¿¡ã«é¢ããè¨å®ãã«ã¤ãã¦ã¯ãã¼ã ã®ä¸ã§å°æ°ã®è©³ãã人ã ããç¥ã£ã¦ããã ãã§ãªãã¨ããªãã¨ãããããããã¾ãããä¸æ¹ã§ 1. ã®ãSpan ã®ä½ãè¾¼ã¿ãã¯ã¢ããªã±ã¼ã·ã§ã³ãåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ããããã¨ããã¨ãã«ã¯ãã®ã¢ããªã±ã¼ã·ã§ã³ã®éçºè ã®å¤§åãããç¨åº¦ç解ãã¦ãããã¨ãæ±ãããã¾ããããã§ãªããã°ä¸é¨ã®é¢æ°ã®ã¿ããåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ãã¦ããªãã¨ãã£ããã¨ã«ãªãããã¾ããã
ãããããã㧠1. ã® Span ã®ä½ãè¾¼ã¿ããå ã«èª¬æãã¾ãã
Span ã®ä½ãè¾¼ã¿
å®ã¯ Trace ã®éä¿¡ã«é¢ããè¨å®ãä½ãæºåãã¦ããªãã¦ã Span ã®ä½ãè¾¼ã¿ãããããã®ã³ã¼ããæ¸ãå§ãããã¨ãã§ããããã«ãªã£ã¦ãã¾ããéä¿¡å ãè¨å®ããã¦ããªãç¶æ 㧠Trace ãåéããã³ã¼ããã¢ããªã±ã¼ã·ã§ã³ã«åãè¾¼ãã§ãå£ããããã§ã¯ãªãã®ã§å®å¿ãã¦ãã ããï¼ãã Trace ãè¨é²ãããªãã ãã§ãï¼ãã¢ããªã±ã¼ã·ã§ã³ãåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ããããã¨ã決ã¾ã£ããããã£ãã Span ã®ä½ãè¾¼ã¿ãå§ãã¾ãããã
ç´ããããç¨èª: context
Go è¨èªã§éçºããã¦ãã人ã«ã¨ã£ã¦ç´ããããç¨èªã®ãããå ã«èª¬æããã¦ããã¾ãã
OpenTelemetry ã«ããã context 㯠Go è¨èªã® context.Context ã¨ã¯éããã®ã§ãtrace ID ã parent span ID ãªã© trace å ã§ã®å¼ã³åºãã§ä¼æãããå¿ è¦ãããæ å ±ã®ãã¨ã§ãããããããã¸ãç´ãããããã¨ã«ãGo è¨èªã® context.Context ã¯ãã®æ å ±ãæ ¼ç´ããã®ã«ã¨ã¦ã便å©ãªãããcontext (otel) ã¯çµå±ã®ã¨ãã context.Context (Go) ã«ä¿åãããã¡ã§ãã
Span ãä½ãè¾¼ãã³ã¼ã㧠context.Context ãå¼æ°ã«æ¸¡ãã¦ããç®æ㯠context (otel) ã渡ãã¦ããã®ã ã¨èãã¦ãã ããã
tracer ãå®ç¾©ãã
æåã«ãããã¨ã¯ã°ãã¼ãã«å¤æ°ã§ tracer
ãå®ç¾©ãããã¨ã§ããããã±ã¼ã¸ãã¨ã« tracer.go
ã«å®ç¾©ããã¨ããããããã¾ããã
import ( "go.opentelemetry.io/otel" ) var tracer = otel.Tracer("github.com/cybozu-go/deadbeef/foo/bar/baz")
Tracer ã®å½¹å²ã¯ä»¥ä¸ã®3ã¤ã§ãã
- ã¢ã¸ã¥ã¼ã«åãä¸ããï¼ç¬¬ä¸å¼æ°ï¼
- ãã® Tracer ããä½ããã Span ã«å ±éãã¦ä¸ããã attributes ãä»ä¸ããï¼ãã®ä¾ã§ã¯å©ç¨ãã¦ããªãï¼
- TracerProvider ã¨ã®é¢é£ä»ãï¼å¾è¿°ï¼
第ä¸å¼æ°ã«æ¸¡ãããæåå㯠instrumentation name ã¨å¼ã°ã instrumentation ãå®è¡ããã¦ããã¢ã¸ã¥ã¼ã«ï¼ã©ã¤ãã©ãªï¼ã®ååãå ¥ããã¨ãæå¾ ããã¦ãã¾ãããã¨ãã Jaeger ãªã©ã§ Trace ãåç §ããã¨ãã«ãç¹å®ã®ã¢ã¸ã¥ã¼ã«ï¼ã©ã¤ãã©ãªï¼ã«é¢ä¿ãã¦ãã Trace ãçµãè¾¼ããã¨ããæ¤ç´¢ãããã®ã«ä½¿ãã¾ããGo ã§æ¸ãããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ ããã±ã¼ã¸åã渡ããã¨ãæ¨å¥¨ããã¦ãã¾ãããã使ããã¦ããã©ã¤ãã©ãªã§ãããã¨ãã°ä»¥ä¸ã®ãã㪠instrumentation name ã渡ããã¦ãã¾ãã
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
OpenTelemetry ã«å ¥éãã人ã instrumentation name ã«ã¤ãã¦ãããããã¡ãªééãã«ã¯ä»¥ä¸ã®ãããªãã®ãããã¾ãã
- Span ã®ååãã¤ãã¦ãã¾ã
- Trace ãè¨é²ãã¦ãããµã¼ãã¹ãã¢ããªã±ã¼ã·ã§ã³ã®ååãä»ãã¦ãã¾ã
Span ã®ååã¯ãã®ã㨠tracer.Start()
ããã¨ãã«æ¸¡ãã¾ããã¾ããTrace ãè¨é²ãã¦ãããµã¼ãã¹ãã¢ããªã±ã¼ã·ã§ã³ã®ååã¯ãã¨ã§ TracerProvider ãè¨å®ããã¨ãã« resource ã¨ããä»çµã¿ã§ã¾ã¨ãã¦è¨å®ãã¾ãã
Tracer ã«ä»ä¸ãã attributes ã¯ãã¨ãã°ã¢ã¸ã¥ã¼ã«ï¼ã©ã¤ãã©ãªï¼ã®ãã¼ã¸ã§ã³ãä»ä¸ãããããã®ã«ä½¿ãã¾ãããã¡ããåæ§ã«å¾è¿°ãã TracerProvider ã«å¯¾ã㦠resource ã¨ãã¦ä¸ããã®ãé©åãªå ´åãå¤ããããããã§ä»ä¸ããå¿ è¦ããããã®ã¯å°ãªãããããã¾ããã
Span ãçæãã
Trace ã® Span ãä½ãããå¦çã«è¡ãã¤ããããããã»ã©ã°ãã¼ãã«å¤æ°ã«å®ç¾©ãã tracer ã使ã£ã¦ tracer.Start()
ãå¼ã³åºããã¨ã§ Span ãçæã§ãã¾ãã
import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) func DoSomething(ctx context.Context, foo string, bar string) error { ctx, span := tracer.Start(ctx, "DoSomething", trace.WithAttributes( attribute.String("foo", foo), attribute.String("bar", bar), )) defer span.End() ... }
å ã»ã©ã® context ã«ã¤ãã¦èª¬æã§ãè¿°ã¹ãããã«ãå¼æ°ã«æ¸¡ãã¦ãã context.Context ã«ã¯ trace ID, parent Span ID ãªã©ã® context æ å ±ãåãè¾¼ã¾ãã¦ãã¾ããåãè¾¼ã¾ãã¦ããªãã£ãå ´åã¯ä½ããã Span ã root Span ã«ãªãã¾ããæ»ãå¤ã® context.Context ã«ã¯ parent Span ID ãæ°ããè¨å®ããã context æ å ±ãåãè¾¼ã¾ãã¦ãã¾ãã
attributes ã¨ãã¦æ¸¡ãã追å æ å ±ã¯ Jaeger ãªã©ã§ãåç §ãããã¨ãã§ãããã®å¦çãã©ã®ãããªãã©ã¡ã¼ã¿ã§å®è¡ãããã®ãç¥ãã®ã«å½¹ç«ã¡ã¾ãã
span.End()
㯠Span ãçµãã£ãæéãè¨é²ãã¾ããå¼ã³åºãå¿ããã¨ã¡ã¢ãªãªã©ã®ãªã½ã¼ã¹ããªã¼ã¯ãã¾ãã
Event ã Status ã®è¨å®
Span ã®ä½ãè¾¼ã¿ã®åºæ¬ã¯ä¸è¨ã®2ã¤ï¼tracer ã®å®ç¾©ã¨ tracer.Start()
ã®å¼ã³åºãï¼ã ãã§ããããã ãã§ãååæç¨ã§ãããããã«ä½ãè¾¼ã¿ããå ´åã¯æ¬¡ã®ãããªæ©è½ãããã¾ãã
ã¾ã span.AddEvent ã§ã¤ãã³ããè¨å®ãããã¨ãã§ãã¾ããSpan ã¯éå§æå»ã¨çµäºæå»ã®2ã¤ã§è¡¨ããã¾ããããEvent ã¯è¨é²ããæå»ã®ã¿ã§è¡¨ããã¾ããJaeger ã§ã¯ Span ã¯å¸¯ã¨ãã¦è¡¨ç¤ºãããã®ã«å¯¾ããEvent ã¯ç¹ã¨ãã¦è¡¨ç¤ºããã¾ããå®éã« OpenTelemetry ãçµã¿è¾¼ãã§ããã¨éå§æå»ãçµäºæå»ã®æ¦å¿µããªããSpan ã¨ãããã㯠Event ã ãªï½ãã¨æããç¬éããããã¨æãã®ã§ããã®ãããªã¨ãã«ã¯ Event ã使ã£ã¦ãããããããã¾ããã
ã»ãã«ã Span status ãè¨å® ããã error ãè¨é² ããããããã¨ãã§ãã¾ããJaeger ã§ã¯ status ãã¨ã©ã¼ã® Span ã¯ãã¼ã¯ãä»ãããã«ãªã£ã¦ãããããé©åã«è¨å®ãã¦ããã¨ã¨ã©ã¼ã®åå ã追ãããããããã®ã«æç¨ããããã¾ããã
Trace ã®éä¿¡ã«é¢ããè¨å®
Span ã®ä½ãè¾¼ã¿ã§ã¯ä½¿ãããªãå¿ è¦ããã API ã¯ããã»ã©å¤ãããã¾ããã§ãããä¸æ¹ãTrace ã®éä¿¡ã«é¢ããè¨å®ã§ã¯ä»¥ä¸ã®ãããªãã¨ã«åãçµãå¿ è¦ãããã¾ãã
- exporter ã®è¨å®
- resource ã®è¨å®
- sampler ã®è¨å®
- TracerProvider ã®è¨å®
- context propagator ã®è¨å®
è¨å®é ç®ãç¡æ°ã«ããå§åããã¦ãã¾ããã¡ã§ãããå¿ è¦ã«ãªãã¾ã§ç´°ãã調æ´ãå¾åãã«ãã¦æ¨æºçãªè¨å®ãããã ããªãããã»ã©å¤§å¤ã§ã¯ããã¾ããã
exporter ã®è¨å®
OpenTelemetry ã® exporter 㯠Prometheus exporter ã¨ã¯éããã®ã§ããOpenTelemetry ã§è¨æ¸¬ãã Signalsï¼Trace, Metric, Log ã®ãã¨ï¼ã¯ãã¾ãã¾ãªãããã³ã«ï¼ãã¨ãã° trace ã ãã§ã otlpgrpc, otlphttp, jaeger, zipkin ãªã©ï¼ã«å¯¾å¿ããããã¯ã¨ã³ãã«éä¿¡ãããã¨ãã§ããããã«ãªã£ã¦ãã¾ããããã®éä¿¡å ããããã¯éä¿¡å¦çãè¡ãã¢ã¸ã¥ã¼ã«ã®ãã¨ã exporter ã¨å¼ã³ã¾ãã
opentelemetry-go ã§ã¯ ãã¾ãã¾ãª exporter ãå®è£ ãã¦ãããããå©ç¨ããããã¯ã¨ã³ãã«å¿ã㦠exporter ãè¨å®ããå¿ è¦ãããã¾ããããã§ã¯ otlpgrpc ãããã³ã«ã使ãå ´åã®èª¬æããã¾ãã
otlptracegrpc ã®ããã¥ã¡ã³ããèªã㨠gRPC ã®ã¨ã³ããã¤ã³ãã証ææ¸ã®å©ç¨ã®æç¡ããã®ä»æ¥ç¶ã«é¢ãããªãã·ã§ã³ã®è¨å®é
ç®ãããæ°ãéããªãã¾ãããããã»ã¨ãã©ã®ã¦ã¼ã¹ã±ã¼ã¹ã§ã¯ä½ãè¨å®ããå¿
è¦ã¯ãªã otlptracegrpc.New()
ã¨ã ãæ¸ãã°ããã§ããã»ã¨ãã©ã®è¨å®ã¯ç°å¢å¤æ°ã§å¤æ´å¯è½ã§ãããã¨ãã°ããã¯ã¨ã³ãã®ã¨ã³ããã¤ã³ã㯠OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
ã§ä¸ãããã¨ãã§ãã¾ãã
import ( "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" ) exporter, err := otlptracegrpc.New(ctx) if err != nil { return nil, err }
resource ã®è¨å®
resource 㯠Trace ã®çæè ã®å±æ§ï¼ãã¨ãã°ãã¹ãåãã¢ããªã±ã¼ã·ã§ã³ã®ååããã¼ã¸ã§ã³ãªã©ï¼ã表æããããã®ãã®ã§ããä¼¼ãè¦ç´ ã¨ã㦠Span ã«å¯¾ãã¦ä»ä¸ãã¦ãã attributes ãããã¾ãããattributes ã¯ããããã® Span åºæã®æ å ±ãå ¥ãããã®ã§ããã®ã«å¯¾ããresource ã¯ã°ãã¼ãã«ãªæ å ±ãå ¥ããããã®ãã®ã¨ããéããããã¾ãã
è¨å®ãããããããã¦ãã resource ã¯ããããããã¾ã ããservice.name
ã ã㯠Required ã¨ãããã¨ã«ãªã£ã¦ãã¾ããservice.name
ãè¨å®ãã㨠Jaeger ã§ã Search ã¿ãã®ä¸çªä¸ã«ãã Service ã¨ãããã«ãã¦ã³ã§çµãè¾¼ããããã«ãªã£ã¦ãã¦ä¾¿å©ãªã®ã§ãå¿
ãæå®ãããã¨ããããããã¾ãã
import ( "go.opentelemetry.io/otel/sdk/resource" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" ) res := resource.NewWithAttributes( semconv.SchemaURL, semconv.ServiceName(serviceName), )
sampler ã®è¨å®
sampler ã¯ããã¯ã¨ã³ãã«éä¿¡ãã Trace ããã£ã«ã¿ãªã³ã°ããæ©è½ã§ããå ¨ã¦ã® Trace ãéä¿¡ããã¨ã³ã¹ããããã©ã¼ãã³ã¹ã«æªå½±é¿ãããããé¢å¿ããã Trace ã ããéä¿¡ããããã¨ããç¨éã§ä½¿ããã¾ãã
åæ£ãã¬ã¼ã·ã³ã°ãæ´»ç¨ããåã«é©åãªè¨å®ãäºæ³ããã®ã¯é£ãããããå®éã«ã©ã®ãã㪠Trace ãä¸è¦ãåãã£ã¦ãã調æ´ããã¨ããé²ãæ¹ã«ãªããã¨ãå¤ãã®ã§ã¯ãªãã§ãããããGo ã®ã³ã¼ãã§ä½ãè¨å®ããªãã£ãå ´åã«ã¯ç°å¢å¤æ° OTEL_TRACES_SAMPLER 㨠OTEL_TRACES_SAMPLER_ARG ã®å¤ã«åºã¥ãã¦è¨å®ããã¾ããæå㯠Go ã®ã³ã¼ãã§ã¯ä½ãè¨å®ãããç°å¢å¤æ°ã§èª¿æ´å¯è½ã«ãã¦ããã®ãããã®ããããã¾ããããã¨ãã° sdktrace.WithSampler(sdktrace.AlwaysSample())
ã¨ã³ã¼ãã§è¨å®ããããªã£ãã¨ãã«ã¯ç°å¢å¤æ°ã§ OTEL_TRACES_SAMPLER=always_on
ãä¸ãããã¨ã«ãªãã¾ãã
TracerProvider ã®è¨å®
TracerProvider 㯠var tracer = otel.Tracer()
ã§ä½ã tracer ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã factory ã®ãããªãã®ã§ããããã¾ã§ã«è¨å®ãã¦ãã exporter, resource, sampler 㯠TracerProvider ãä½ãã¨ãã®ãªãã·ã§ã³ã¨ãã¦æ¸¡ããã¨ã§æå¹åããã¾ãã
import ( "go.opentelemetry.io/otel" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) tp := sdktrace.NewTracerProvider( // ä»å㯠sampler ãè¨å®ããªããã¨ã«ãã sdktrace.WithResource(res), // resource sdktrace.WithBatcher(exporter), // exporter ) otel.SetTracerProvider(tp)
å®ã¯ Span ãä½ãè¾¼ãã¨ãã«ä½¿ã£ã¦ãã otel.Tracer()
㯠otel.GetTracerProvider().Tracer()
ã®ç¥ã§ãotel.SetTracerProvider()
ã§ã°ãã¼ãã«å¤æ°ã«è¨å®ããã TracerProvider ãã tracer ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ãã¾ããã°ãã¼ãã«å¤æ°ã使ãã®ã¯å°ãå¿ãçã¿ã¾ãããã»ã¨ãã©ã®ã¦ã¼ã¹ã±ã¼ã¹ã§ã¯ããã§å¯¾å¿ã§ããã¯ãã§ããããããã®ã¢ã¸ã¥ã¼ã«ã® Trace 㯠Jaeger ã«éãããã®ã¢ã¸ã¥ã¼ã«ã® Trace 㯠Grafana Tempo ã«éããã®ããã«éä¿¡å
ã使ãåãããå ´åã¯ã°ãã¼ãã«ã® TracerProvider ã使ãæ¹æ³ã§ã¯å¯¾å¿ãããã¾ãããããã®ãããªå¿
è¦ãçããã¾ã§ã¯æ°ã«ããã« otel.Tracer()
ã使ãæ¹æ³ã§å¯¾å¿ã§ããã¯ãã§ãã
WithBatcher()
㯠trace ãããç¨åº¦æºã¾ã£ã¦ããããã㧠exporter ã«éä¿¡ããã¨ãããªãã·ã§ã³ã§ããWithBatcher()
以å¤ã®é¸æè¢ã¨ãã¦ã¯ WithSyncer()
ã¨ãããã®ãããã¾ãããnot recommended for production use ã¨ãããã¨ãªã®ã§æ°ã«ãã WithBatcher()
ã使ãã°ããã§ããããWithBatcher()
ã«ã¯ãããããªãã·ã§ã³ãããã®ã§ï¼ãã¨ãã°ä½ä»¶æºã¾ã£ããéããã件æ°ãå°ãªãã¨ãã§ãä½ç§çµã£ããéããããªã©ï¼ä¸éãç解ããªããã°ãããªãã®ã§ã¯ã¨ä¸å®ã«ãªãã¾ãããããã«ã¤ãã¦ã ç°å¢å¤æ°ã§ãã¨ãããããã§ãè¨å®ã§ãã ã®ã§å¿é
ããå¿
è¦ã¯ããã¾ããã
context propagator ã®è¨å®
Span ã®ä½ãè¾¼ã¿ã®èª¬æãããã¨ãã«ãOpenTelemetry ã«ããã context ã¨ã¯ trace ID ã parent span ID ãªã© Trace å ã§ã®å¼ã³åºãã§ä¼æãããå¿ è¦ãããæ å ±ã ã¨èª¬æãã¾ãããcontext (otel) ã¯ãã Go è¨èªã® context.Context ã«åãè¾¼ã¾ãã¾ãããããã¯åä¸ããã»ã¹å ã§ã®é¢æ°å¼ã³åºãã§ã®è©±ã§ããããã¯ã¼ã¯è¶ãã«å¥ã®ã³ã³ãã¼ãã³ããå¼ã³åºãåæ£ãã¬ã¼ã·ã³ã°ã§ã¯ context ã HTTP ããããªã©ï¼ãã®ä¼éåªä½ã®ãã¨ã Carrier ã¨å¼ã¶ï¼ã«ä¹ãããã¨ã«ãªãã¾ãã
ãã® context ã®åã渡ãã®ä»çµã¿ãæ½è±¡åãããã®ã context propagator ã¨å¼ãã§ãããopentelemetry-go ã§ã¯ããã¤ãã®ä»æ§ã«å¯¾å¿ãã context propagator ãå®è£ ããã¦ãã¾ããopentelemetry-go ã®ã³ã¢ã®ããã±ã¼ã¸ã§ã¯ W3C Trace Context, W3C Baggageãcontrib ã®ããã±ã¼ã¸ã§ã¯ B3, AWS X-Ray ãªã©ã®å®è£ ãå«ã¾ãã¦ãã¾ããå©ç¨è ã¯ãããã®æä¾ãããå®è£ ãå©ç¨ãããããããã¯èªä½ã® propagator ãä½ããã¨ã«ãªãã¾ãã
context ã Carrier ã«åãè¾¼ãå¦çã Inject, context ã Carrier ããåãåºãå¦çã Extract ã¨å¼ã³ã¾ããpropagator 㯠HTTP ã gRPC ã§å¤é¨ã¨éä¿¡ããã¨ãã«æå㧠Inject, Extract ãããã¨ã§ã使ããã®ã§ãããããã ã¨å¤§å¤ãããã®ã§æ®é㯠otelhttp ã otelgrpc ãªã©ã®ã¤ã³ã¿ã¼ã»ãã¿ã« Extract, Inject ããä»»ããã¾ãããã®ã¨ãå©ç¨ãã propagator ãã¤ã³ã¿ã¼ã»ãã¿ã«æ示çã«è¨å®ããªãã£ãå ´å㯠otel.SetTextMapPropagator
ã«ããã°ãã¼ãã«å¤æ°ã«è¨å®ããã propagator ãå©ç¨ãããããã«ãªã£ã¦ãã¾ãããã W3C Trace Context ãããã©ã«ãã® propagator ã¨ãã¦å©ç¨ãããªã以ä¸ã®ããã«è¨å®ãããã¨ã«ãªãã¾ãã
import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" ) otel.SetTextMapPropagator(propagation.TraceContext{})
gRPC ã®ã¤ã³ã¿ã¼ã»ãã¿ãè¨å®ãã
context ã® Inject, Extract ã¯å¤§å¤ãªã®ã§ã¤ã³ã¿ã¼ã»ãã¿ãå©ç¨ããã¨ãã説æããã¾ãããHTTP ãµã¼ãã¼åãã«ã¯ otelhttp, gRPC ãµã¼ãã¼åãã«ã¯ otelgrpc ã¨ããã¤ã³ã¿ã¼ã»ãã¿ãæä¾ããã¦ãã¾ããotelgrpc 㯠gRPC ã® metadata ãã context ãåãåºã㦠context.Context ã«åãè¾¼ãã§ããã¾ããã¾ã gRPC ã®ãªã¯ã¨ã¹ãã®å¦çã«å¯¾å¿ãã¦èªåçã« Span ãéå§ãçµäºãã¦ããã¾ãï¼èªåè¨è£
ï¼ããã®ã¨ãã«ä½¿ããã propagator ã¯åè¿°ã®éã otel.SetTextMapPropagator()
ã§è¨å®ãã¦ãããã®ãããã©ã«ãã§ä½¿ããã¾ãã
ãã«ã¹ãã§ãã¯ã®ãªã¯ã¨ã¹ãã§ã Trace ãè¨é²ãã¦ããã¨éãå¤ãã¦å¤§å¤ãªã®ã§ ãã£ã«ã¿ã¼ãè¨å®ãã ã¨ããã§ãããã
var traceFilter = filters.None( // ããã« Trace ãè¨é²ããããªã RPC ã®æ¡ä»¶ãåæãã filters.FullMethodName("/cybozu.deadbeef.v1.Probe/Ready"), ) server := grpc.NewServer( grpc.ChainUnaryInterceptor( otelgrpc.UnaryServerInterceptor(otelgrpc.WithInterceptorFilter(traceFilter)), ... // ã»ãã«ä½¿ãããã¤ã³ã¿ã¼ã»ãã¿ãããã° ), )
åä½ç¢ºèªãã
åæ£ãã¬ã¼ã·ã³ã°ã®ããã¯ã¨ã³ãã«ã¯æåç¡åãã¾ãã¾ãªé¸æè¢ãããã¾ãããã¢ããªã±ã¼ã·ã§ã³ãåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ããããã¨ãã«ã¯ãã¼ã«ã«éçºç°å¢ã§åä½ç¢ºèªãã§ããã»ããæããã¨ãå¤ãã¨æãã¾ããJaeger 㯠jaegertracing/all-in-one ã¨ããã¤ã¡ã¼ã¸ãæä¾ãã¦ãããåä½ç¢ºèªã«ä½¿ããããã¯ã¨ã³ããç°¡åã«ç«ã¡ä¸ãããã¨ãã§ãã¾ãã
Docker Compose ãªãã³ã³ãããã²ã¨ã¤ç«ã¦ãã ããKubernetes ãªã Pod ãã²ã¨ã¤ç«ã¦ãã ãã§ããåæ£ãã¬ã¼ã·ã³ã°ã«å¯¾å¿ããã¢ããªã±ã¼ã·ã§ã³ãéçºãããã¨ãã«ã¯ãã®ããã¯ã¨ã³ããç«ã¡ä¸ããããããã«æºåãã¦ããããã¤ã§ãåä½ç¢ºèªã§ããããã«ãã¦ããã®ããå§ããã¾ãã
ãããã«
ãã®è¨äºã§ã¯ opentelemetry-go ã®å©ç¨ãå§ããã®ã«å¿ è¦ãªç¥èãæä¾ãã¾ãããç§ã®ãã¼ã ã§ã® OpenTelemetry ã®æ´»ç¨ã¯ã¾ã ã¾ã å§ã¾ã£ãã°ãããªã®ã§ãããããã¬ã·ã¬ã·å®è£ ãã¦åæ£ãã¬ã¼ã·ã³ã°ã«ãã£ã¦å¾ãããæ©æµã享åãã¦ããããã§ãã