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

Unify the Config type across instrumentation #226

Merged
merged 12 commits into from
Dec 8, 2021

Conversation

MrAlias
Copy link
Contributor

@MrAlias MrAlias commented Dec 2, 2021

The Config and its related Options are recreated for all our instrumentation currently. This results in a lot of duplicate code and tests. This adds a common Config type with appropriate functionality as well as common options. These internal types and functions are expected to be reused in instrumentation libraries by embedding the types or forwarding calls to functions.

@MrAlias MrAlias requested review from a team as code owners December 2, 2021 18:34
@MrAlias MrAlias requested a review from TomRoSystems December 2, 2021 18:34
@codecov-commenter
Copy link

codecov-commenter commented Dec 2, 2021

Codecov Report

Merging #226 (4d464fa) into main (c8d7b10) will decrease coverage by 0.30%.
The diff coverage is 98.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #226      +/-   ##
==========================================
- Coverage   76.93%   76.63%   -0.31%     
==========================================
  Files          41       43       +2     
  Lines        2012     2037      +25     
==========================================
+ Hits         1548     1561      +13     
- Misses        422      433      +11     
- Partials       42       43       +1     
Flag Coverage Δ
Linux 76.63% <98.63%> (+0.19%) ⬆️
Windows ?
macOS ?

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

Impacted Files Coverage Δ
...m/syndtr/goleveldb/leveldb/splunkleveldb/config.go 80.00% <88.23%> (-16.08%) ⬇️
...b.com/syndtr/goleveldb/leveldb/splunkleveldb/db.go 93.75% <100.00%> (+0.41%) ⬆️
...syndtr/goleveldb/leveldb/splunkleveldb/iterator.go 100.00% <100.00%> (ø)
...syndtr/goleveldb/leveldb/splunkleveldb/snapshot.go 100.00% <100.00%> (ø)
...dtr/goleveldb/leveldb/splunkleveldb/transaction.go 100.00% <100.00%> (ø)
instrumentation/internal/config.go 100.00% <100.00%> (ø)
instrumentation/internal/option.go 100.00% <100.00%> (ø)
...n/github.com/lib/pq/splunkpq/internal/connector.go 51.75% <0.00%> (-5.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 c8d7b10...4d464fa. Read the comment docs.

@MrAlias MrAlias enabled auto-merge (squash) December 2, 2021 19:42
MrAlias added a commit to MrAlias/splunk-otel-go that referenced this pull request Dec 2, 2021
The config is mostly copied from
signalfx#226
Copy link
Contributor

@pellared pellared left a comment

Choose a reason for hiding this comment

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

Can you please update at least one instrumentation library to see how the new internal package would be used?

@MrAlias
Copy link
Contributor Author

MrAlias commented Dec 8, 2021

Can you please update at least one instrumentation library to see how the new internal package would be used?

Added a use-case in the splunkleveldb instrumentation. PTAL

Copy link
Contributor

@pellared pellared left a comment

Choose a reason for hiding this comment

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

Even though I dislike type embedding it looks like the most efficient way to avoid code duplication in this scenario. Left a few comments but in general, I think that this is a very pragmatic approach 👍

ctx context.Context
tracer trace.Tracer
defaultStartOpts []trace.SpanStartOption
*internal.Config
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason why not embedding as value? it should avoid some memory allocations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The returned value from NewConfig is a pointer.

If we want to optimize I would want to build out benchmarks first. Definitely something we can do in the future if needed.

copy(merged[len(c.defaultStartOpts):], opts)
}
return merged
type optConv struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

how about renaming to optionConv to have it similar to optionFunc?

trace.WithAttributes(semconv.DBOperationKey.String("Iterator")),
)
_, span := c.resolveTracer().Start(c.ctx, "Iterator", sso...)
_, span := c.ResolveTracer(c.ctx).Start(c.ctx, "Iterator", sso...)
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it awkward to have the the ctx in the config struct.
I propose to have two methods in the common config:

  • ResolveTracer
  • ResolveTracerContext (when some call have context)

Same for WithSpan.

Then if the instrumented library used context we use ResolveTracerContext and WithSpanContext (e.g. for http) if not then we simply use ResolveTracerContext and WithSpan (e.g. for leveldb).

Calling trace.SpanFromContext(ctx) is useless if we know that it will never return anything.

If you disagree I would rather put context.Background() in all places instead of cfg.ctx. Current code can give a false impression that can be some real context.

Copy link
Contributor Author

@MrAlias MrAlias Dec 8, 2021

Choose a reason for hiding this comment

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

I find it awkward to have the the ctx in the config struct.

Sure, agreed. However, this is an artifact of the underlying library not accepting a context as an argument. The instrumentation consequentially needs to accept this and apply it as needed.

I propose to have two methods in the common config:

  • ResolveTracer
  • ResolveTracerContext (when some call have context)

ResolveTracer is equivalent to just Config.Tracer. If you don't have a context to extract a TracerProvider from it should not be used.

Same for WithSpan.

The context passed to WithSpan is updated to include the span and passed to the closure. Having the ability to set the parent is going to be important for more than just span propagation. Like the rest of the Go ecosystem, having a context as a first argument is a good idea in that it keeps context propagation explicit and adding *Context type functions are useful only in that they allow backwards compatibility for context propagation. Having one from the start seems to be the opposite of idiomatic Go.

If you disagree I would rather put context.Background() in all places instead of cfg.ctx. Current code can give a false impression that can be some real context.

That can in fact be a real context, with a span embedded in it. Passing that context as included in this PR ensures the correct user specified TracerProvider is used and heredity is preserved.

@MrAlias MrAlias merged commit f051a59 into signalfx:main Dec 8, 2021
@MrAlias MrAlias deleted the internal-config branch December 8, 2021 20:16
MrAlias added a commit that referenced this pull request Dec 14, 2021
* Initial instrumentation layout

* Add changes to changelog

* Add initial config and instrumentation

The config is mostly copied from
#226

* Implement instrumentation

Remove unneeded Config methods.

* Add integration tests

* Shutdown TracerProvider in tests

* Update Middleware docs

* Fix example test
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