dsgni26260 2018-10-22 18:50
浏览 82

如何在Go中向第二代App Engine应用程序添加自定义跟踪?

Google App Engine now supports Go 1.11 via the new second-generation standard environment. While converting an older standard environment application to the second generation, it wasn't obvious how to combine tracing information from the app engine infrastructure with custom tracing I added to the application using OpenCensus.

Even though I had created a stackdriver exporter and registered traces, I wasn't seeing custom trace information in the stackdriver console attached to inbound requests.

  • 写回答

1条回答 默认 最新

  • doutang1873 2018-10-22 18:50
    关注

    The key is I was missing is understanding how span context is communicated to the serving app. Google leverages the X-Cloud-Trace-Context header to propagate span context within requests sent to your serving instances, and the go.opencensus.io/exporter/stackdriver/propagation library provides an implementation to extract and persist this information within http requests.

    Don't forget to create a stackdriver exporter, and register traces to it. The docs for the exporter library show an example of this.

    // CreateSpanFromRequest returns a context and span based on the http.Request.
    // If no existing spancontext is found, this will start a new span.
    // Modifies existing request to contain the generated span's context.
    func CreateSpanFromRequest(name string, r *http.Request) (context.Context, *trace.Span) {
        var span *trace.Span
        ctx := r.Context()
        httpFormat := &propagation.HTTPFormat{}
        sc, ok := httpFormat.SpanContextFromRequest(r)
        if ok {
            ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc)
        } else {
            ctx, span = trace.StartSpan(ctx, name)
        }
        // Write the span context into the http.Request.  We do this to
        // to enable chaining handlers together more easily.
        httpFormat.SpanContextToRequest(span.SpanContext(), r)
        return ctx, span
    }
    

    Using this, I was able to add custom spans to my handlers that would be properly associated with the incoming request information in stackdriver:

    func indexHandler(w http.ResponseWriter, r *http.Request) {
        _, span := CreateSpanFromRequest("indexHandler", r)
        defer span.End()
        if r.URL.Path != "/" {
            http.NotFound(w, r)
            return
        }
        fmt.Fprint(w, "Hello, World!")
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘