Skip to content

Commit c2a6172

Browse files
yumosxMrAlias
andauthored
feat(stdouttrace): generate counter implementation via templates (#7231)
Use templates to generate the counter implementation for `stdouttrace`, so it can be easily reused in other modules. --------- Co-authored-by: Tyler Yahn <[email protected]>
1 parent 70e8929 commit c2a6172

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

exporters/stdout/stdouttrace/internal/counter/counter.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporters/stdout/stdouttrace/internal/counter/counter_test.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package internal provides internal functionality for the stdouttrace
5+
// package.
6+
package internal // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace/internal"
7+
8+
//go:generate gotmpl --body=../../../../internal/shared/counter/counter.go.tmpl "--data={}" --out=counter/counter.go
9+
//go:generate gotmpl --body=../../../../internal/shared/counter/counter_test.go.tmpl "--data={}" --out=counter/counter_test.go
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Code generated by gotmpl. DO NOT MODIFY.
2+
// source: internal/shared/counter/counter.go.tmpl
3+
4+
// Copyright The OpenTelemetry Authors
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
// Package counter provides a simple counter for generating unique IDs.
8+
//
9+
// This package is used to generate unique IDs while allowing testing packages
10+
// to reset the counter.
11+
package counter // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/counter"
12+
13+
import "sync/atomic"
14+
15+
// exporterN is a global 0-based count of the number of exporters created.
16+
var exporterN atomic.Int64
17+
18+
// NextExporterID returns the next unique ID for an exporter.
19+
func NextExporterID() int64 {
20+
const inc = 1
21+
return exporterN.Add(inc) - inc
22+
}
23+
24+
// SetExporterID sets the exporter ID counter to v and returns the previous
25+
// value.
26+
//
27+
// This function is useful for testing purposes, allowing you to reset the
28+
// counter. It should not be used in production code.
29+
func SetExporterID(v int64) int64 {
30+
return exporterN.Swap(v)
31+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Code generated by gotmpl. DO NOT MODIFY.
2+
// source: internal/shared/counter/counter_test.go.tmpl
3+
4+
// Copyright The OpenTelemetry Authors
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
package counter
8+
9+
import (
10+
"sync"
11+
"testing"
12+
)
13+
14+
func TestNextExporterID(t *testing.T) {
15+
SetExporterID(0)
16+
17+
var expected int64
18+
for range 10 {
19+
id := NextExporterID()
20+
if id != expected {
21+
t.Errorf("NextExporterID() = %d; want %d", id, expected)
22+
}
23+
expected++
24+
}
25+
}
26+
27+
func TestSetExporterID(t *testing.T) {
28+
SetExporterID(0)
29+
30+
prev := SetExporterID(42)
31+
if prev != 0 {
32+
t.Errorf("SetExporterID(42) returned %d; want 0", prev)
33+
}
34+
35+
id := NextExporterID()
36+
if id != 42 {
37+
t.Errorf("NextExporterID() = %d; want 42", id)
38+
}
39+
}
40+
41+
func TestNextExporterIDConcurrentSafe(t *testing.T) {
42+
SetExporterID(0)
43+
44+
const goroutines = 100
45+
const increments = 10
46+
47+
var wg sync.WaitGroup
48+
wg.Add(goroutines)
49+
50+
for range goroutines {
51+
go func() {
52+
defer wg.Done()
53+
for range increments {
54+
NextExporterID()
55+
}
56+
}()
57+
}
58+
59+
wg.Wait()
60+
61+
expected := int64(goroutines * increments)
62+
if id := NextExporterID(); id != expected {
63+
t.Errorf("NextExporterID() = %d; want %d", id, expected)
64+
}
65+
}

0 commit comments

Comments
 (0)