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

Fixes and refactors trace v2 #288

Merged
merged 4 commits into from
Dec 1, 2023
Merged

Fixes and refactors trace v2 #288

merged 4 commits into from
Dec 1, 2023

Conversation

rjfonseca
Copy link
Member

The trace v2 package was not working properly. It didn't support naming the system and the integration with another packages were broken, e.g., not being injected on the logs.

I tried to solve all the issues without breaking too much compatibility but some breaking changes on trace v2 were made.

The tracking middleware were coupling request and trace packages. I split the middleware into two (the old one still exists but now is deprecated), so request and trace has each one its own middleware. The request middleware should be used after the trace middleware so it can inject the request-id into the trace.

The previous middleware supports both versions of trace so it can be used during the phasing out of v1.

The span object in trace v2 was dropped in favor of the otel span directly. The otel span is already an interface and it is very powerful. If we would wrap this interface we would need to write too much code and still would lose some of its power. Because otel is a good market standard I felt confident in exposing it's span and use the trace v2 package only as an orquestrator.

The example was also redone and it's now complete dockerized and using jaeger to collect and serve the traces.

A new gokit endpoint middleware was created that starts a trace for the endpoint but also handle errors and allows for custom attributes to be inserted on the span.

The trace v2 package was not working properly. It didn't support naming
the system and the integration with another packages were broken, e.g.,
not being injected on the logs.

I tried to solve all the issues without breaking too much compatibility
but some breaking changes on trace v2 were made.

The tracking middleware were coupling request and trace packages. I
split the middleware into two (the old one still exists but now is
deprecated), so request and trace has each one its own middleware. The
request middleware should be used after the trace middleware so it can
inject the request-id into the trace.

The previous middleware supports both versions of trace so it can be
used during the phasing out of v1.

The span object in trace v2 was dropped in favor of the otel span
directly. The otel span is already an interface and it is very
powerful. If we would wrap this interface we would need to write too
much code and still would lose some of its power. Because otel is a good
market standard I felt confident in exposing it's span and use the trace
v2 package only as an orquestrator.

The example was also redone and it's now complete dockerized and using
jaeger to collect and serve the traces.

A new gokit endpoint middleware was created that starts a trace for the
endpoint but also handle errors and allows for custom attributes to be
inserted on the span.
@rjfonseca rjfonseca self-assigned this Nov 23, 2023
request/http.go Outdated
// HTTPMiddleware returns an http middleware that adds a request id
// to the context of the request and the same id in the header of the
// http response. If there is an active trace span, the request id is
// also registred as an attribute 'request.id'. It's importante that
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "importante" -> "important"

}

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: here it could use errors.Errorf in order to avoid importing fmt, as errors is already imported

func newResource(serviceName, serviceVersion string) (*resource.Resource, error) {
return resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName(serviceName),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are serviceName and serviceVersion required? If they are, does resource return a good error message? If not, a custom error message would make it quick to someone who is using fkit without deep knowledge of this source code to know which configurations are missing

Copy link
Collaborator

@eric-reis eric-reis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. I added some nit's and a question. I'm not deep into the tracev2 and opentelemetry, so I can't vouch for correctness. The commit message implied a deprecated middleware, so I think a deprecated comment is missing.

The Dockerfile was dropped and the docker-compose was enhanced due to
security issues raised by sonar.

Custom configuration was dropped in favor of the official environment
variables.

README as converted into a proper godoc.

Added some sanity checks for checking for missing environment
variables when the default OpenTelemetry values are not ideal.
OTEL_TRACES_SAMPLER: "parentbased_traceidratio"
OTEL_TRACES_SAMPLER_ARG: "0.5"

# TraceV1 is only used for compatibility demontration
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: demontration -> demonstration

Copy link
Collaborator

@eric-reis eric-reis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. A typo pointed out

The cache was supposed to be committed commented out.
Copy link

sonarcloud bot commented Dec 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@rjfonseca rjfonseca merged commit 446a5a7 into master Dec 1, 2023
7 checks passed
@rjfonseca rjfonseca deleted the fix-trace-v2 branch December 1, 2023 18:57
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