Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporter/elasticexporter: translate semantic conventions to Elastic destination fields #671

Merged
merged 2 commits into from
Aug 19, 2020

Conversation

axw
Copy link
Contributor

@axw axw commented Aug 10, 2020

Description:

Translate http.*, net.peer.*, and db.* semantic conventions to Elastic's equivalent destination.* fields. This is necessary to add external (non-instrumented) destination services to the service map.

Testing:

I've added unit tests, and performed manual testing with a Jaeger-instrumented test application:

Test code
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	"time"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
	"github.com/uber/jaeger-client-go/config"
	"golang.org/x/sync/errgroup"
)

const (
	addrServiceA = "localhost:1234"
	addrServiceB = "localhost:4321"
)

func newTracer(name string) (opentracing.Tracer, io.Closer, error) {
	cfg, err := config.FromEnv()
	if err != nil {
		return nil, nil, err
	}
	cfg.ServiceName = name
	cfg.Sampler.Type = "const"
	cfg.Sampler.Param = 1
	return cfg.NewTracer()
}

func runServiceA(ctx context.Context) error {
	tracer, closer, err := newTracer("jaeger-service-a")
	if err != nil {
		return err
	}
	defer closer.Close()

	return http.ListenAndServe(addrServiceA, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
		serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx))
		defer serverSpan.Finish()

		clientSpan := tracer.StartSpan("client", opentracing.ChildOf(serverSpan.Context()))
		defer clientSpan.Finish()
		ext.SpanKindRPCClient.Set(clientSpan)

		req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/", addrServiceB), nil)
		ext.HTTPUrl.Set(clientSpan, req.URL.String())
		ext.HTTPMethod.Set(clientSpan, req.Method)

		// Inject the client span context into the headers
		tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		defer resp.Body.Close()
		ext.HTTPStatusCode.Set(clientSpan, uint16(resp.StatusCode))

		w.WriteHeader(resp.StatusCode)
		io.Copy(w, resp.Body)
	}))
}

func runServiceB(ctx context.Context) error {
	tracer, closer, err := newTracer("jaeger-service-b")
	if err != nil {
		return err
	}
	defer closer.Close()

	return http.ListenAndServe(addrServiceB, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
		serverSpan := tracer.StartSpan("server", ext.RPCServerOption(spanCtx))
		defer serverSpan.Finish()

		dbSpan := tracer.StartSpan("querying database", opentracing.ChildOf(serverSpan.Context()))
		ext.DBStatement.Set(dbSpan, "SELECT * FROM table")
		ext.StringTagName("db.system").Set(dbSpan, "mysql")
		ext.StringTagName("net.peer.host").Set(dbSpan, "mysql-host")
		ext.Uint16TagName("net.peer.port").Set(dbSpan, 3306)
		time.Sleep(time.Millisecond)
		dbSpan.Finish()

		clientSpan := tracer.StartSpan("client", opentracing.ChildOf(serverSpan.Context()))
		defer clientSpan.Finish()
		ext.SpanKindRPCClient.Set(clientSpan)

		req, _ := http.NewRequest("GET", "https://testing.invalid/", nil)
		ext.HTTPUrl.Set(clientSpan, req.URL.String())
		ext.HTTPMethod.Set(clientSpan, req.Method)
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		defer resp.Body.Close()
		ext.HTTPStatusCode.Set(clientSpan, uint16(resp.StatusCode))
	}))
}

func main() {
	g, ctx := errgroup.WithContext(context.Background())
	g.Go(func() error { return runServiceA(ctx) })
	g.Go(func() error { return runServiceB(ctx) })
	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}
}

image

Translate http.*, peer.*, and db.* to Elastic's destination.* fields.
This is necessary to add external service nodes to the service map.
@axw axw requested a review from a team August 10, 2020 03:57
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 10, 2020

CLA Check
The committers are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Aug 10, 2020

Codecov Report

Merging #671 into master will increase coverage by 0.18%.
The diff coverage is 92.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #671      +/-   ##
==========================================
+ Coverage   86.80%   86.99%   +0.18%     
==========================================
  Files         197      197              
  Lines       10749    10794      +45     
==========================================
+ Hits         9331     9390      +59     
+ Misses       1088     1073      -15     
- Partials      330      331       +1     
Flag Coverage Δ
#integration 71.09% <ø> (ø)
#unit 86.83% <92.85%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...sticexporter/internal/translator/elastic/traces.go 93.69% <92.00%> (+2.87%) ⬆️
...icexporter/internal/translator/elastic/metadata.go 100.00% <100.00%> (+14.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e2a04c...eb95449. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants