forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple use of
otel/internal/internaltest
(open-telemetry#4424)
* Add internaltest templates * Generate internaltest using gotmpl * Generate the sdk/internal/internaltest pkg * Use sdk/internal/internaltest in sdk module * Generate exporters/jaeger/internal/internaltest pkg * Use exporters/jaeger/internal/internaltest in jaeger exporter * Generate the exporters/zipkin/internal/internaltest pkg * Use local internaltest in zipkin exporter * Fix import path name in trace test
- Loading branch information
Showing
60 changed files
with
4,952 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Code created by gotmpl. DO NOT MODIFY. | ||
// source: internal/shared/internaltest/alignment.go.tmpl | ||
|
||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internaltest // import "go.opentelemetry.io/otel/exporters/jaeger/internal/internaltest" | ||
|
||
/* | ||
This file contains common utilities and objects to validate memory alignment | ||
of Go types. The primary use of this functionality is intended to ensure | ||
`struct` fields that need to be 64-bit aligned so they can be passed as | ||
arguments to 64-bit atomic operations. | ||
The common workflow is to define a slice of `FieldOffset` and pass them to the | ||
`Aligned8Byte` function from within a `TestMain` function from a package's | ||
tests. It is important to make this call from the `TestMain` function prior | ||
to running the rest of the test suit as it can provide useful diagnostics | ||
about field alignment instead of ambiguous nil pointer dereference and runtime | ||
panic. | ||
For more information: | ||
https://github.com/open-telemetry/opentelemetry-go/issues/341 | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// FieldOffset is a preprocessor representation of a struct field alignment. | ||
type FieldOffset struct { | ||
// Name of the field. | ||
Name string | ||
|
||
// Offset of the field in bytes. | ||
// | ||
// To compute this at compile time use unsafe.Offsetof. | ||
Offset uintptr | ||
} | ||
|
||
// Aligned8Byte returns if all fields are aligned modulo 8-bytes. | ||
// | ||
// Error messaging is printed to out for any field determined misaligned. | ||
func Aligned8Byte(fields []FieldOffset, out io.Writer) bool { | ||
misaligned := make([]FieldOffset, 0) | ||
for _, f := range fields { | ||
if f.Offset%8 != 0 { | ||
misaligned = append(misaligned, f) | ||
} | ||
} | ||
|
||
if len(misaligned) == 0 { | ||
return true | ||
} | ||
|
||
fmt.Fprintln(out, "struct fields not aligned for 64-bit atomic operations:") | ||
for _, f := range misaligned { | ||
fmt.Fprintf(out, " %s: %d-byte offset\n", f.Name, f.Offset) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Code created by gotmpl. DO NOT MODIFY. | ||
// source: internal/shared/internaltest/env.go.tmpl | ||
|
||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internaltest // import "go.opentelemetry.io/otel/exporters/jaeger/internal/internaltest" | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
type Env struct { | ||
Name string | ||
Value string | ||
Exists bool | ||
} | ||
|
||
// EnvStore stores and recovers environment variables. | ||
type EnvStore interface { | ||
// Records the environment variable into the store. | ||
Record(key string) | ||
|
||
// Restore recovers the environment variables in the store. | ||
Restore() error | ||
} | ||
|
||
var _ EnvStore = (*envStore)(nil) | ||
|
||
type envStore struct { | ||
store map[string]Env | ||
} | ||
|
||
func (s *envStore) add(env Env) { | ||
s.store[env.Name] = env | ||
} | ||
|
||
func (s *envStore) Restore() error { | ||
var err error | ||
for _, v := range s.store { | ||
if v.Exists { | ||
err = os.Setenv(v.Name, v.Value) | ||
} else { | ||
err = os.Unsetenv(v.Name) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *envStore) setEnv(key, value string) error { | ||
s.Record(key) | ||
|
||
err := os.Setenv(key, value) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *envStore) Record(key string) { | ||
originValue, exists := os.LookupEnv(key) | ||
s.add(Env{ | ||
Name: key, | ||
Value: originValue, | ||
Exists: exists, | ||
}) | ||
} | ||
|
||
func NewEnvStore() EnvStore { | ||
return newEnvStore() | ||
} | ||
|
||
func newEnvStore() *envStore { | ||
return &envStore{store: make(map[string]Env)} | ||
} | ||
|
||
func SetEnvVariables(env map[string]string) (EnvStore, error) { | ||
envStore := newEnvStore() | ||
|
||
for k, v := range env { | ||
err := envStore.setEnv(k, v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return envStore, nil | ||
} |
Oops, something went wrong.