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

Use more efficient WithAttributeSet() #5664

Merged
merged 4 commits into from
May 29, 2024

Conversation

ash2k
Copy link
Contributor

@ash2k ash2k commented May 28, 2024

https://github.com/open-telemetry/opentelemetry-go/blob/5661ff0ded32cf1b83f1147dae96ca403c198504/metric/instrument.go#L349-L364

Since all the usage places of WithAttributes() operate on a locally allocated slice, there is no need to make a defensive copy.

@ash2k ash2k requested a review from dmathieu as a code owner May 28, 2024 04:22
@ash2k ash2k requested a review from a team May 28, 2024 04:22
Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.5%. Comparing base (bd97adf) to head (d4bf830).

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #5664   +/-   ##
=====================================
  Coverage   63.5%   63.5%           
=====================================
  Files        194     194           
  Lines      12061   12064    +3     
=====================================
+ Hits        7667    7670    +3     
  Misses      4178    4178           
  Partials     216     216           
Files Coverage Δ
...ion/google.golang.org/grpc/otelgrpc/interceptor.go 89.4% <100.0%> (ø)
...n/google.golang.org/grpc/otelgrpc/stats_handler.go 100.0% <100.0%> (ø)
instrumentation/net/http/otelhttp/handler.go 91.9% <100.0%> (+<0.1%) ⬆️
instrumentation/net/http/otelhttp/transport.go 97.4% <100.0%> (+<0.1%) ⬆️

Copy link
Member

@dmathieu dmathieu left a comment

Choose a reason for hiding this comment

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

Could you run the existing benchmarks to see if they provide any improvement information?

CHANGELOG.md Outdated Show resolved Hide resolved
@ash2k
Copy link
Contributor Author

ash2k commented May 29, 2024

I ran existing benchmarks in the package but they are unreliable. They report a different numbers on each run:

3 runs on the main branch:

BenchmarkNoInstrumentation
BenchmarkNoInstrumentation-10          	    1177	   1046092 ns/op	 4067107 B/op	    1612 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	    1096	   1078666 ns/op	 4071180 B/op	    1646 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	    1125	   1054867 ns/op	 4072056 B/op	    1691 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	    1101	   1088741 ns/op	 4073663 B/op	    1655 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	    1098	   1075309 ns/op	 4071058 B/op	    1671 allocs/op


BenchmarkNoInstrumentation
BenchmarkNoInstrumentation-10          	    1003	   1079321 ns/op	 4066551 B/op	    1620 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	     908	   1175979 ns/op	 4083241 B/op	    1688 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	     913	   1137071 ns/op	 4081054 B/op	    1690 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	     883	   1307467 ns/op	 4090217 B/op	    1673 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	     964	   1205874 ns/op	 4083989 B/op	    1677 allocs/op


BenchmarkNoInstrumentation-10          	    1070	   1082558 ns/op	 4066511 B/op	    1622 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	    1040	   1120522 ns/op	 4075504 B/op	    1677 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	    1060	   1123797 ns/op	 4073679 B/op	    1692 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	    1048	   1126572 ns/op	 4069418 B/op	    1633 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	    1002	   1157537 ns/op	 4075354 B/op	    1680 allocs/op

3 runs on this PR:

BenchmarkNoInstrumentation
BenchmarkNoInstrumentation-10          	     997	   1018199 ns/op	 4064651 B/op	    1623 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	    1130	   1151011 ns/op	 4077869 B/op	    1677 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	    1122	   1087530 ns/op	 4080164 B/op	    1694 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	    1119	   1056265 ns/op	 4067321 B/op	    1631 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	    1095	   1088941 ns/op	 4073179 B/op	    1690 allocs/op


BenchmarkNoInstrumentation
BenchmarkNoInstrumentation-10          	    1134	   1059390 ns/op	 4061954 B/op	    1601 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	     982	   1103331 ns/op	 4071457 B/op	    1652 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	    1063	   1094388 ns/op	 4072858 B/op	    1685 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	    1053	   1119437 ns/op	 4071801 B/op	    1639 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	    1014	   1122490 ns/op	 4076550 B/op	    1684 allocs/op


BenchmarkNoInstrumentation
BenchmarkNoInstrumentation-10          	    1132	   1058675 ns/op	 4064408 B/op	    1614 allocs/op
BenchmarkUnaryServerInterceptor
BenchmarkUnaryServerInterceptor-10     	    1041	   1077159 ns/op	 4072335 B/op	    1660 allocs/op
BenchmarkStreamServerInterceptor
BenchmarkStreamServerInterceptor-10    	    1075	   1083785 ns/op	 4074521 B/op	    1718 allocs/op
BenchmarkUnaryClientInterceptor
BenchmarkUnaryClientInterceptor-10     	    1047	   1111051 ns/op	 4071612 B/op	    1645 allocs/op
BenchmarkStreamClientInterceptor
BenchmarkStreamClientInterceptor-10    	    1051	   1109932 ns/op	 4075154 B/op	    1684 allocs/op

p.s. 5fdddf2 broke the benchmarks and some of the tests now work differently. NewClient() uses the DNS resolver vs old funcs used passthrough resolver by default. So, the code tries to do DNS resolution on things like bufnet, fake, etc. Fix is in #5676.

Co-authored-by: Damien Mathieu <[email protected]>
@MrAlias MrAlias merged commit 1808301 into open-telemetry:main May 29, 2024
23 checks passed
@ash2k ash2k deleted the use-WithAttributeSet branch May 29, 2024 23:46
MrAlias added a commit that referenced this pull request May 30, 2024
)

This adds high-level benchmarks for `otelhttp.Handler.ServeHTTP` and
`otelhttp.Transport.RoundTrip`, as well as a benchmark comparison
without those wrappers.

Related: #5664.

Current results on my machine:

```
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/test
BenchmarkHandlerServeHTTP/without_the_otelhttp_handler-10               37971928                31.35 ns/op           28 B/op          0 allocs/op
BenchmarkHandlerServeHTTP/with_the_otelhttp_handler-10                    435142              2723 ns/op            5585 B/op         35 allocs/op
BenchmarkTransportRoundTrip/without_the_otelhttp_transport-10              11430            401735 ns/op           24436 B/op        117 allocs/op
BenchmarkTransportRoundTrip/with_the_otelhttp_transport-10                   278           4367998 ns/op            5416 B/op         85 allocs/op
PASS
ok      go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/test      10.056s
```

---------

Co-authored-by: Tyler Yahn <[email protected]>
@MrAlias MrAlias added this to the v1.28.0 milestone Jun 20, 2024
h.requestBytesCounter.Add(ctx, bw.read.Load(), o)
h.responseBytesCounter.Add(ctx, rww.written, o)
o := metric.WithAttributeSet(attribute.NewSet(attributes...))
addOpts := []metric.AddOption{o} // Allocate vararg slice once.
Copy link
Contributor

Choose a reason for hiding this comment

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

@ash2k please explain why this allocation is needed?

@open-telemetry open-telemetry locked as resolved and limited conversation to collaborators Jul 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants