10BC0 Merge branch 'main' into fix-histogram-maxmin · open-telemetry/opentelemetry-go@9c9c91e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c9c91e

Browse files
authored
Merge branch 'main' into fix-histogram-maxmin
2 parents 27b30d8 + 79371c1 commit 9c9c91e

File tree

7 files changed

+198
-28
lines changed

7 files changed

+198
-28
lines changed

example/dice/doc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Dice is the "Roll the dice" getting started example application.
15+
// Dice is the "Roll the dice" application.
16+
//
17+
// [Getting Started] uses this example to demonstrate OpenTelemetry Go.
18+
//
19+
// [Getting Started]: https://opentelemetry.io/docs/languages/net/automatic/getting-started/
1620
package main

example/dice/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func run() (err error) {
3939
defer stop()
4040

4141
// Set up OpenTelemetry.
42-
serviceName := "dice"
43-
serviceVersion := "0.1.0"
44-
otelShutdown, err := setupOTelSDK(ctx, serviceName, serviceVersion)
42+
otelShutdown, err := setupOTelSDK(ctx)
4543
if err != nil {
4644
return
4745
}

example/dice/otel.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ import (
2424
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
2525
"go.opentelemetry.io/otel/propagation"
2626
"go.opentelemetry.io/otel/sdk/metric"
27-
"go.opentelemetry.io/otel/sdk/resource"
2827
"go.opentelemetry.io/otel/sdk/trace"
29-
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
3028
)
3129

3230
// setupOTelSDK bootstraps the OpenTelemetry pipeline.
3331
// If it does not return an error, make sure to call shutdown for proper cleanup.
34-
func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shutdown func(context.Context) error, err error) {
32+
func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) {
3533
var shutdownFuncs []func(context.Context) error
3634

3735
// shutdown calls cleanup functions registered via shutdownFuncs.
@@ -51,19 +49,12 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut
5149
err = errors.Join(inErr, shutdown(ctx))
5250
}
5351

54-
// Set up resource.
55-
res, err := newResource(serviceName, serviceVersion)
56-
if err != nil {
57-
handleErr(err)
58-
return
59-
}
60-
6152
// Set up propagator.
6253
prop := newPropagator()
6354
otel.SetTextMapPropagator(prop)
6455

6556
// Set up trace provider.
66-
tracerProvider, err := newTraceProvider(res)
57+
tracerProvider, err := newTraceProvider()
6758
if err != nil {
6859
handleErr(err)
6960
return
@@ -72,7 +63,7 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut
7263
otel.SetTracerProvider(tracerProvider)
7364

7465
// Set up meter provider.
75-
meterProvider, err := newMeterProvider(res)
66+
meterProvider, err := newMeterProvider()
7667
if err != nil {
7768
handleErr(err)
7869
return
@@ -83,22 +74,14 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut
8374
return
8475
}
8576

86-
func newResource(serviceName, serviceVersion string) (*resource.Resource, error) {
87-
return resource.Merge(resource.Default(),
88-
resource.NewWithAttributes(semconv.SchemaURL,
89-
semconv.ServiceName(serviceName),
90-
semconv.ServiceVersion(serviceVersion),
91-
))
92-
}
93-
9477
func newPropagator() propagation.TextMapPropagator {
9578
return propagation.NewCompositeTextMapPropagator(
9679
propagation.TraceContext{},
9780
propagation.Baggage{},
9881
)
9982
}
10083

101-
func newTraceProvider(res *resource.Resource) (*trace.TracerProvider, error) {
84+
func newTraceProvider() (*trace.TracerProvider, error) {
10285
traceExporter, err := stdouttrace.New(
10386
stdouttrace.WithPrettyPrint())
10487
if err != nil {
@@ -109,19 +92,17 @@ func newTraceProvider(res *resource.Resource) (*trace.TracerProvider, error) {
10992
trace.WithBatcher(traceExporter,
11093
// Default is 5s. Set to 1s for demonstrative purposes.
11194
trace.WithBatchTimeout(time.Second)),
112-
trace.WithResource(res),
11395
)
11496
return traceProvider, nil
11597
}
11698

117-
func newMeterProvider(res *resource.Resource) (*metric.MeterProvider, error) {
99+
func newMeterProvider() (*metric.MeterProvider, error) {
118100
metricExporter, err := stdoutmetric.New()
119101
if err != nil {
120102
return nil, err
121103
}
122104

123105
meterProvider := metric.NewMeterProvider(
124-
metric.WithResource(res),
125106
metric.WithReader(metric.NewPeriodicReader(metricExporter,
126107
// Default is 1m. Set to 3s for demonstrative purposes.
127108
metric.WithInterval(3*time.Second))),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
16+
17+
import (
18+
"context"
19+
"time"
20+
21+
"go.opentelemetry.io/otel/attribute"
22+
"go.opentelemetry.io/otel/sdk/metric/metricdata"
23+
)
24+
25+
// Drop returns a [Reservoir] that drops all measurements it is offered.
26+
func Drop[N int64 | float64]() Reservoir[N] { return &dropRes[N]{} }
27+
28+
type dropRes[N int64 | float64] struct{}
29+
30+
// Offer does nothing, all measurements offered will be dropped.
31+
func (r *dropRes[N]) Offer(context.Context, time.Time, N, []attribute.KeyValue) {}
32+
33+
// Collect resets dest. No exemplars will ever be returned.
34+
func (r *dropRes[N]) Collect(dest *[]metricdata.Exemplar[N]) {
35+
*dest = (*dest)[:0]
36+
}
37+
38+
// Flush resets dest. No exemplars will ever be returned.
39+
func (r *dropRes[N]) Flush(dest *[]metricdata.Exemplar[N]) {
40+
*dest = (*dest)[:0]
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestDrop(t *testing.T) {
22+
t.Run("Int64", ReservoirTest[int64](func(int) (Reservoir[int64], int) {
23+
return Drop[int64](), 0
24+
}))
25+
26+
t.Run("Float64", ReservoirTest[float64](func(int) (Reservoir[float64], int) {
27+
return Drop[float64](), 0
28+
}))
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
16+
17+
import (
18+
"context"
19+
"time"
20+
21+
"go.opentelemetry.io/otel/attribute"
22+
"go.opentelemetry.io/otel/trace"
23+
)
24+
25+
// SampledFilter r 3B95 eturns a [Reservoir] wrapping r that will only offer measurements
26+
// to r if the passed context associated with the measurement contains a sampled
27+
// [go.opentelemetry.io/otel/trace.SpanContext].
28+
func SampledFilter[N int64 | float64](r Reservoir[N]) Reservoir[N] {
29+
return filtered[N]{Reservoir: r}
30+
}
31+
32+
type filtered[N int64 | float64] struct {
33+
Reservoir[N]
34+
}
35+
36+
func (f filtered[N]) Offer(ctx context.Context, t time.Time, n N, a []attribute.KeyValue) {
37+
if trace.SpanContextFromContext(ctx).IsSampled() {
38+
f.Reservoir.Offer(ctx, t, n, a)
39+
}
40+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
16+
17+
import (
18+
"context"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
24+
"go.opentelemetry.io/otel/attribute"
25+
"go.opentelemetry.io/otel/sdk/metric/metricdata"
26+
"go.opentelemetry.io/otel/trace"
27+
)
28+
29+
func TestSampledFilter(t *testing.T) {
30+
t.Run("Int64", testSampledFiltered[int64])
31+
t.Run("Float64", testSampledFiltered[float64])
32+
}
33+
34+
func testSampledFiltered[N int64 | float64](t *testing.T) {
35+
under := &res[N]{}
36+
37+
r := SampledFilter[N](under)
38+
39+
ctx := context.Background()
40+
r.Offer(ctx, staticTime, 0, nil)
41+
assert.False(t, under.OfferCalled, "underlying Reservoir Offer called")
42+
r.Offer(sample(ctx), staticTime, 0, nil)
43+
assert.True(t, under.OfferCalled, "underlying Reservoir Offer not called")
44+
45+
r.Collect(nil)
46+
assert.True(t, under.CollectCalled, "underlying Reservoir Collect not called")
47+
48+
r.Flush(nil)
49+
assert.True(t, under.FlushCalled, "underlying Reservoir Flush not called")
50+
}
51+
52+
func sample(parent context.Context) context.Context {
53+
sc := trace.NewSpanContext(trace.SpanContextConfig{
54+
TraceID: trace.TraceID{0x01},
55+
SpanID: trace.SpanID{0x01},
56+
TraceFlags: trace.FlagsSampled,
57+
})
58+
return trace.ContextWithSpanContext(parent, sc)
59+
}
60+
61+
type res[N int64 | float64] struct {
62+
OfferCalled bool
63+
CollectCalled bool
64+
FlushCalled bool
65+
}
66+
67+
func (r *res[N]) Offer(context.Context, time.Time, N, []attribute.KeyValue) {
68+
r.OfferCalled = true
69+
}
70+
71+
func (r *res[N]) Collect(*[]metricdata.Exemplar[N]) {
72+
r.CollectCalled = true
73+
}
74+
75+
func (r *res[N]) Flush(*[]metricdata.Exemplar[N]) {
76+
r.FlushCalled = true
77+
}

0 commit comments

Comments
 (0)
0