-
Notifications
You must be signed in to change notification settings - Fork 6
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
Fixes and refactors trace v2 #288
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,58 +2,92 @@ package trace | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/arquivei/foundationkit/app" | ||
"github.com/rs/zerolog/log" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.21.0" | ||
) | ||
|
||
// Config represents all trace's configuration | ||
type Config struct { | ||
Exporter string `default:""` | ||
ProbabilitySample float64 `default:"0"` | ||
ServiceName string | ||
ServiceVersion string | ||
Exporter string | ||
ProbabilitySample float64 | ||
Stackdriver StackdriverConfig | ||
OTLP OTLPConfig | ||
} | ||
|
||
// Setup use Config to setup an trace exporter and returns a shutdown handler | ||
func Setup(c Config) app.ShutdownFunc { | ||
exporter, err := newExporter(c) | ||
|
||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to create trace exporter") | ||
} | ||
|
||
res, err := newResource(c.ServiceName, c.ServiceVersion) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to create trace resource") | ||
} | ||
|
||
tp := newTraceProvider(res, c.ProbabilitySample, exporter) | ||
|
||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator(newPropagator()) | ||
|
||
return tp.ForceFlush | ||
} | ||
|
||
func newResource(serviceName, serviceVersion string) (*resource.Resource, error) { | ||
return resource.Merge(resource.Default(), | ||
resource.NewWithAttributes(semconv.SchemaURL, | ||
semconv.ServiceName(serviceName), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are |
||
semconv.ServiceVersion(serviceVersion), | ||
)) | ||
} | ||
|
||
func newPropagator() propagation.TextMapPropagator { | ||
return propagation.NewCompositeTextMapPropagator( | ||
propagation.TraceContext{}, | ||
propagation.Baggage{}, | ||
) | ||
} | ||
|
||
func newTraceProvider(res *resource.Resource, prob float64, exporter trace.SpanExporter) *trace.TracerProvider { | ||
return trace.NewTracerProvider( | ||
trace.WithResource(res), | ||
trace.WithSampler( | ||
trace.ParentBased(trace.TraceIDRatioBased(prob)), | ||
), | ||
trace.WithBatcher(exporter), | ||
) | ||
} | ||
|
||
func newExporter(c Config) (trace.SpanExporter, error) { | ||
var exporter trace.SpanExporter | ||
var err error | ||
|
||
exporterName := strings.ToLower(c.Exporter) | ||
switch exporterName { | ||
switch exporterName := strings.ToLower(c.Exporter); exporterName { | ||
case "stackdriver": | ||
exporter, err = newStackdriverExporter(c.Stackdriver) | ||
case "otlp": | ||
exporter, err = newOTLPExporter(c.OTLP) | ||
case "": | ||
// No trace will be exported, but will be created | ||
default: | ||
err = errors.New("invalid exporter") | ||
err = errors.New("invalid exporter name: " + exporterName) | ||
} | ||
|
||
if err != nil { | ||
log.Fatal().Str("exporter", exporterName).Err(err).Msg("Failed to create trace exporter") | ||
return nil, fmt.Errorf("creating trace exporter: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: here it could use |
||
} | ||
|
||
tp := trace.NewTracerProvider( | ||
trace.WithSampler( | ||
trace.ParentBased(trace.TraceIDRatioBased(c.ProbabilitySample)), | ||
), | ||
trace.WithBatcher(exporter), | ||
) | ||
|
||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator( | ||
propagation.NewCompositeTextMapPropagator( | ||
propagation.TraceContext{}, | ||
propagation.Baggage{}, | ||
), | ||
) | ||
|
||
return tp.ForceFlush | ||
return exporter, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
.PHONY: send send-% | ||
|
||
send: | ||
$(eval URL := http://localhost:8686/ping/v1) | ||
@if [ -f "$(R)" ] ; then \ | ||
cat $(R) | curl -v -H 'Content-Type: application/json' -d @- "$(URL)"; \ | ||
else \ | ||
echo "Invalid request file '$(R)'" > /dev/stderr; \ | ||
fi | ||
|
||
send-without-trace: R ?= docs/requests/3-5000.json | ||
send-without-trace: send | ||
docker compose run sender | ||
|
||
run: | ||
GOOGLE_APPLICATION_CREDENTIALS=gcp-credentials.json go run cmd/api/*.go | ||
docker compose run api |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM golang:1.21-alpine | ||
|
||
WORKDIR /usr/src/app | ||
|
||
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change | ||
COPY go.mod go.sum ./ | ||
RUN go mod download && go mod verify | ||
|
||
COPY . . | ||
RUN go build -v -o /usr/local/bin/app ./trace/v2/examples/cmd/api | ||
|
||
CMD ["app"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "importante" -> "important"