Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
tracer detection shifted to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikash Kumar committed Aug 7, 2021
1 parent 6b839f4 commit 2f00f00
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
15 changes: 15 additions & 0 deletions examples/simple-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16

RUN mkdir /src/
WORKDIR /src/
COPY . .
RUN go get ./...
RUN go build -ldflags "-linkmode external -extldflags -static" -a main.go

FROM alpine:latest
RUN apk add --no-cache tzdata ca-certificates
COPY --from=0 /src/main /main
COPY --from=0 /src/configs /configs
EXPOSE 9000

CMD ["/main"]
22 changes: 8 additions & 14 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package gofr

import (
"fmt"
"github.com/vikash/gofr/pkg/gofr/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"log"
"net"
"os"
"strconv"
"sync"
"time"

"github.com/vikash/gofr/pkg/gofr/config"
"go.opentelemetry.io/otel/exporters/zipkin"
sdktrace "go.opentelemetry.io/otel/sdk/trace"

gofrHTTP "github.com/vikash/gofr/pkg/gofr/http"
)
Expand Down Expand Up @@ -130,20 +128,16 @@ func (a *App) SubCommand(pattern string, handler Handler) {
}

func (a *App) initTracer() {
// If zipkin is running on default port - start tracing
conn, err := net.DialTimeout("tcp", net.JoinHostPort("localhost", "9411"), 2*time.Second) //nolint:gomnd

if err != nil || conn == nil {
a.container.Logf("Tracer detection error: %v", err)
a.container.Log("To enable tracing locally, Run: docker run -d -p 9411:9411 openzipkin/zipkin")

tracerHost := a.Config.Get("TRACER_HOST")
tracerPort := a.Config.GetOrDefault("TRACER_PORT", "9411")
if tracerHost == "" {
return
}

a.container.Log("Exporting traces to zipkin.")

exporter, err := zipkin.New(
"http://localhost:9411/api/v2/spans",
fmt.Sprintf("http://%s:%s/api/v2/spans", tracerHost, tracerPort),
zipkin.WithSDKOptions(sdktrace.WithSampler(sdktrace.AlwaysSample())),
)

Expand Down
1 change: 0 additions & 1 deletion pkg/gofr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ type handler struct {

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := newContext(http2.NewResponder(w), http2.NewRequest(r), h.container)
defer c.Trace("gofr-handler").End()
c.responder.Respond(h.function(c))
}
3 changes: 1 addition & 2 deletions pkg/gofr/http/router.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package http

import (
"fmt"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"net/http"

Expand Down Expand Up @@ -29,6 +28,6 @@ func NewRouter() *Router {
}

func (rou *Router) Add(method, pattern string, handler http.Handler) {
h := otelhttp.NewHandler(handler, fmt.Sprintf("%s %s", method, pattern))
h := otelhttp.NewHandler(handler, "gofr-handler")
rou.Router.NewRoute().Methods(method).Path(pattern).Handler(h)
}
6 changes: 2 additions & 4 deletions pkg/gofr/service/new.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package service

import (
"context"
"fmt"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"net/http"
"context"
"net/http/httptrace"
)

Expand All @@ -20,8 +20,6 @@ func (h *httpService) Get(ctx context.Context, path string, params map[string]in

uri := h.url + "/" + path

fmt.Sprintf("Making a request to %s\n", uri)

tr := otel.Tracer("gofr-http-client")
ctx, span := tr.Start(ctx, uri)
defer span.End()
Expand Down

0 comments on commit 2f00f00

Please sign in to comment.