Skip to content

Commit

Permalink
Merge branch 'main' into feature/tracestate0209
Browse files Browse the repository at this point in the history
  • Loading branch information
1046102779 committed Feb 18, 2022
2 parents 3d95339 + 8ebef75 commit 30bff98
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 260 deletions.
170 changes: 56 additions & 114 deletions .github/dependabot.yml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Added support to configure the span limits with environment variables.
The following environment variables are used. (#2606)
- `OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT`
- `OTEL_SPAN_EVENT_COUNT_LIMIT`
- `OTEL_SPAN_LINK_COUNT_LIMIT`

If the provided environment variables are invalid (negative), the default values would be used.

### Changed

- For tracestate's members, prepend the new element and remove the oldest one, which is over capacity (#2592)
- Add event and link drop counts to the exported data from the `oltptrace` exporter. (#2601)

### Fixed

- Remove the OTLP trace exporter limit of SpanEvents when exporting. (#2616)

## [1.4.1] - 2022-02-16

### Fixed
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TIMEOUT = 60
.DEFAULT_GOAL := precommit

.PHONY: precommit ci
precommit: license-check misspell go-mod-tidy golangci-lint-fix test-default
precommit: dependabot-generate license-check misspell go-mod-tidy golangci-lint-fix test-default
ci: dependabot-check license-check lint vanity-import-check build test-default check-clean-work-tree test-coverage

# Tools
Expand All @@ -47,6 +47,9 @@ $(TOOLS)/semconvgen: PACKAGE=go.opentelemetry.io/build-tools/semconvgen
CROSSLINK = $(TOOLS)/crosslink
$(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/crosslink

DBOTCONF = $(TOOLS)/dbotconf
$(TOOLS)/dbotconf: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/dbotconf

GOLANGCI_LINT = $(TOOLS)/golangci-lint
$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lint

Expand All @@ -66,7 +69,7 @@ GOJQ = $(TOOLS)/gojq
$(TOOLS)/gojq: PACKAGE=github.com/itchyny/gojq/cmd/gojq

.PHONY: tools
tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD)
tools: $(CROSSLINK) $(DBOTCONF) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(GOJQ) $(SEMCONVGEN) $(MULTIMOD)

# Build

Expand Down Expand Up @@ -187,9 +190,15 @@ dependabot-check:
if [ -n "$$result" ]; then \
echo "missing dependabot entry:"; echo "$$result"; \
echo "new modules need to be added to the $(DEPENDABOT_PATH) file"; \
echo "(run: make dependabot-generate)"; \
exit 1; \
fi

.PHONY: dependabot-generate
dependabot-generate: $(DBOTCONF)
@echo "gerating dependabot configuration"; \
$(DBOTCONF)

.PHONY: check-clean-work-tree
check-clean-work-tree:
@if ! git diff --quiet; then \
Expand Down
14 changes: 2 additions & 12 deletions exporters/otlp/otlptrace/internal/tracetransform/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
)

const (
maxEventsPerSpan = 128
)

// Spans transforms a slice of OpenTelemetry spans into a slice of OTLP
// ResourceSpans.
func Spans(sdl []tracesdk.ReadOnlySpan) []*tracepb.ResourceSpans {
Expand Down Expand Up @@ -177,22 +173,16 @@ func spanEvents(es []tracesdk.Event) []*tracepb.Span_Event {
return nil
}

evCount := len(es)
if evCount > maxEventsPerSpan {
evCount = maxEventsPerSpan
}
events := make([]*tracepb.Span_Event, evCount)

events := make([]*tracepb.Span_Event, len(es))
// Transform message events
for i := 0; i < evCount; i++ {
for i := 0; i < len(es); i++ {
events[i] = &tracepb.Span_Event{
Name: es[i].Name,
TimeUnixNano: uint64(es[i].Time.UnixNano()),
Attributes: KeyValues(es[i].Attributes),
DroppedAttributesCount: uint32(es[i].DroppedAttributeCount),
}
}

return events
}

Expand Down
13 changes: 0 additions & 13 deletions exporters/otlp/otlptrace/internal/tracetransform/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package tracetransform

import (
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -102,18 +101,6 @@ func TestSpanEvent(t *testing.T) {
assert.Equal(t, &tracepb.Span_Event{Name: "test 2", Attributes: KeyValues(attrs), TimeUnixNano: eventTimestamp, DroppedAttributesCount: 2}, got[1])
}

func TestExcessiveSpanEvents(t *testing.T) {
e := make([]tracesdk.Event, maxEventsPerSpan+1)
for i := 0; i < maxEventsPerSpan+1; i++ {
e[i] = tracesdk.Event{Name: strconv.Itoa(i)}
}
assert.Len(t, e, maxEventsPerSpan+1)
got := spanEvents(e)
assert.Len(t, got, maxEventsPerSpan)
// Ensure the drop order.
assert.Equal(t, strconv.Itoa(maxEventsPerSpan-1), got[len(got)-1].Name)
}

func TestNilLinks(t *testing.T) {
assert.Nil(t, links(nil))
}
Expand Down
79 changes: 73 additions & 6 deletions internal/tools/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@
package tools // import "go.opentelemetry.io/otel/internal/tools"

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"text/tabwriter"

"golang.org/x/mod/modfile"
)

// FindRepoRoot retrieves the root of the repository containing the current working directory.
// Beginning at the current working directory (dir), the algorithm checks if joining the ".git"
// suffix, such as "dir.get", is a valid file. Otherwise, it will continue checking the dir's
// parent directory until it reaches the repo root or returns an error if it cannot be found.
func FindRepoRoot() (string, error) {
// Repo represents a git repository.
type Repo string

// FindRepoRoot retrieves the root of the repository containing the current
// working directory. Beginning at the current working directory (dir), the
// algorithm checks if joining the ".git" suffix, such as "dir.get", is a
// valid file. Otherwise, it will continue checking the dir's parent directory
// until it reaches the repo root or returns an error if it cannot be found.
func FindRepoRoot() (Repo, error) {
start, err := os.Getwd()
if err != nil {
return "", err
Expand All @@ -52,6 +62,63 @@ func FindRepoRoot() (string, error) {
return "", err
}

return dir, nil
return Repo(dir), nil
}
}

// FindModules returns all Go modules contained in Repo r.
func (r Repo) FindModules() ([]*modfile.File, error) {
var results []*modfile.File
err := filepath.Walk(string(r), func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
// Walk failed to walk into this directory. Stop walking and
// signal this error.
return walkErr
}

if !info.IsDir() {
return nil
}

goMod := filepath.Join(path, "go.mod")
f, err := os.Open(goMod)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}

var b bytes.Buffer
io.Copy(&b, f)
if err = f.Close(); err != nil {
return err
}

mFile, err := modfile.Parse(goMod, b.Bytes(), nil)
if err != nil {
return err
}
results = append(results, mFile)
return nil
})

sort.SliceStable(results, func(i, j int) bool {
return results[i].Syntax.Name < results[j].Syntax.Name
})

return results, err
}

func PrintModFiles(w io.Writer, mFiles []*modfile.File) error {
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
if _, err := fmt.Fprintln(tw, "FILE PATH\tIMPORT PATH"); err != nil {
return err
}
for _, m := range mFiles {
if _, err := fmt.Fprintf(tw, "%s\t%s\n", m.Syntax.Name, m.Module.Mod.Path); err != nil {
return err
}
}
return tw.Flush()
}
112 changes: 23 additions & 89 deletions internal/tools/crosslink/crosslink.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,130 +25,64 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/tabwriter"

"go.opentelemetry.io/otel/internal/tools"
"golang.org/x/mod/modfile"
)

type repo string

type mod struct {
filePath string
importPath string
}

func (r repo) findModules() (mods, error) {
var results []mod
err := filepath.Walk(string(r), func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}

_, err = os.Stat(filepath.Join(path, "go.mod"))
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}

cmd := exec.Command("go", "mod", "edit", "-json")
cmd.Dir = path
out, err := cmd.Output()
if err != nil {
return err
}

var result struct {
Module struct {
Path string
}
}
err = json.Unmarshal(out, &result)
if err != nil {
return err
}

results = append(results, mod{
filePath: path,
importPath: result.Module.Path,
})
return nil
})

return results, err
}

type mods []mod

func (m mods) print(w io.Writer) error {
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
if _, err := fmt.Fprintln(tw, "FILE PATH\tIMPORT PATH"); err != nil {
return err
}
for _, m := range m {
if _, err := fmt.Fprintf(tw, "%s\t%s\n", m.filePath, m.importPath); err != nil {
return err
}
}
return tw.Flush()
}

func (m mods) crossLink() error {
func crossLink(m []*modfile.File) error {
for _, from := range m {
args := []string{"mod", "edit"}

basepath := filepath.Dir(from.Syntax.Name)
for _, to := range m {
localPath, err := filepath.Rel(from.filePath, to.filePath)
newPath, err := filepath.Rel(basepath, filepath.Dir(to.Syntax.Name))
if err != nil {
return err
}
if localPath == "." || localPath == ".." {
localPath += "/"
} else if !strings.HasPrefix(localPath, "..") {
localPath = "./" + localPath
switch {
case newPath == ".", newPath == "..":
newPath += "/"
case !strings.HasPrefix(newPath, ".."):
newPath = "./" + newPath
}
args = append(args, "-replace", to.importPath+"="+localPath)
from.AddReplace(to.Module.Mod.Path, "", newPath, "")
}

cmd := exec.Command("go", args...)
cmd.Dir = from.filePath
out, err := cmd.CombinedOutput()
from.Cleanup()

f, err := os.OpenFile(from.Syntax.Name, os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
log.Println(string(out))
return err
}
if _, err = f.Write(modfile.Format(from.Syntax)); err != nil {
return err
}
if err = f.Close(); err != nil {
return err
}
}
return nil
}

func main() {
repoRootStr, err := tools.FindRepoRoot()
root, err := tools.FindRepoRoot()
if err != nil {
log.Fatalf("unable to find repo root: %v", err)
}

repoRoot := repo(repoRootStr)

mods, err := repoRoot.findModules()
mods, err := root.FindModules()
if err != nil {
log.Fatalf("unable to list modules: %v", err)
}

if err := mods.print(os.Stdout); err != nil {
if err := tools.PrintModFiles(os.Stdout, mods); err != nil {
log.Fatalf("unable to print modules: %v", err)
}

if err := mods.crossLink(); err != nil {
if err := crossLink(mods); err != nil {
log.Fatalf("unable to crosslink: %v", err)
}
}
Loading

0 comments on commit 30bff98

Please sign in to comment.