Skip to content

Commit a9ce74b

Browse files
author
Alex Boten
authored
[chore] add semconvkit to reduce toil (#9057)
This code, copied from the opentelemetry-go repo, allows to generate schema.go automatically as well, reducing effort needed to generate semantic convention packages in the future --------- Signed-off-by: Alex Boten <[email protected]>
1 parent eed3b4e commit a9ce74b

File tree

7 files changed

+93
-6
lines changed

7 files changed

+93
-6
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,14 @@ genpdata:
227227
$(MAKE) fmt
228228

229229
# Generate semantic convention constants. Requires a clone of the opentelemetry-specification repo
230-
gensemconv: $(SEMCONVGEN)
230+
gensemconv: $(SEMCONVGEN) $(SEMCONVKIT)
231231
@[ "${SPECPATH}" ] || ( echo ">> env var SPECPATH is not set"; exit 1 )
232232
@[ "${SPECTAG}" ] || ( echo ">> env var SPECTAG is not set"; exit 1 )
233233
@echo "Generating semantic convention constants from specification version ${SPECTAG} at ${SPECPATH}"
234234
$(SEMCONVGEN) -o semconv/${SPECTAG} -t semconv/template.j2 -s ${SPECTAG} -i ${SPECPATH}/model/. --only=resource -p conventionType=resource -f generated_resource.go
235235
$(SEMCONVGEN) -o semconv/${SPECTAG} -t semconv/template.j2 -s ${SPECTAG} -i ${SPECPATH}/model/. --only=event -p conventionType=event -f generated_event.go
236236
$(SEMCONVGEN) -o semconv/${SPECTAG} -t semconv/template.j2 -s ${SPECTAG} -i ${SPECPATH}/model/. --only=span -p conventionType=trace -f generated_trace.go
237+
$(SEMCONVKIT) -output "semconv/$(SPECTAG)" -tag "$(SPECTAG)"
237238

238239
# Checks that the HEAD of the contrib repo checked out in CONTRIB_PATH compiles
239240
# against the current version of this repo.

Makefile.Common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ MISSPELL := $(TOOLS_BIN_DIR)/misspell
3232
MULTIMOD := $(TOOLS_BIN_DIR)/multimod
3333
PORTO := $(TOOLS_BIN_DIR)/porto
3434
SEMCONVGEN := $(TOOLS_BIN_DIR)/semconvgen
35+
SEMCONVKIT := $(TOOLS_BIN_DIR)/semconvkit
3536
YQ := $(TOOLS_BIN_DIR)/yq
3637

3738
.PHONY: install-tools

internal/tools/semconvkit/main.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"embed"
8+
"flag"
9+
"log"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
"text/template"
14+
)
15+
16+
var (
17+
out = flag.String("output", "./", "output directory")
18+
tag = flag.String("tag", "", "OpenTelemetry tagged version")
19+
20+
//go:embed templates/*.tmpl
21+
rootFS embed.FS
22+
)
23+
24+
// SemanticConventions are information about the semantic conventions being
25+
// generated.
26+
type SemanticConventions struct {
27+
// TagVer is the tagged version (i.e. v1.7.0 and not 1.7.0).
28+
TagVer string
29+
}
30+
31+
func (sc SemanticConventions) SemVer() string {
32+
return strings.TrimPrefix(*tag, "v")
33+
}
34+
35+
// render renders all templates to the dest directory using the data.
36+
func render(src, dest string, data *SemanticConventions) error {
37+
tmpls, err := template.ParseFS(rootFS, src)
38+
if err != nil {
39+
return err
40+
}
41+
for _, tmpl := range tmpls.Templates() {
42+
target := filepath.Join(dest, strings.TrimSuffix(tmpl.Name(), ".tmpl"))
43+
// nolint: gosec
44+
wr, err := os.Create(target)
45+
if err != nil {
46+
return err
47+
}
48+
49+
err = tmpl.Execute(wr, data)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
55+
return nil
56+
}
57+
58+
func main() {
59+
flag.Parse()
60+
61+
if *tag == "" {
62+
log.Fatalf("invalid tag: %q", *tag)
63+
}
64+
65+
sc := &SemanticConventions{TagVer: *tag}
66+
67+
if err := render("templates/*.tmpl", *out, sc); err != nil {
68+
log.Fatal(err)
69+
}
70+
}
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 semconv implements OpenTelemetry semantic conventions.
5+
//
6+
// OpenTelemetry semantic conventions are agreed standardized naming
7+
// patterns for OpenTelemetry things. This package represents the {{.TagVer}}
8+
// version of the OpenTelemetry semantic conventions.
9+
package semconv // import "go.opentelemetry.io/collector/semconv/{{.TagVer}}"
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 semconv // import "go.opentelemetry.io/collector/semconv/{{.TagVer}}"
5+
6+
// SchemaURL is the schema URL that matches the version of the semantic conventions
7+
// that this package defines. Semconv packages starting from v1.4.0 must declare
8+
// non-empty schema URL in the form https://opentelemetry.io/schemas/<version>
9+
const SchemaURL = "https://opentelemetry.io/schemas/{{.SemVer}}"

internal/tools/tools.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ import (
2727
_ "golang.org/x/exp/cmd/apidiff"
2828
_ "golang.org/x/tools/cmd/goimports"
2929
_ "golang.org/x/vuln/cmd/govulncheck"
30+
31+
_ "go.opentelemetry.io/collector/internal/tools/semconvkit"
3032
)

semconv/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,3 @@ Generating semantic convention constants from specification version v1.22.0 at /
1616
.tools/semconvgen -o semconv/v1.22.0 -t semconv/template.j2 -s v1.22.0 -i /tmp/semantic-conventions/model/. --only=event -p conventionType=event -f generated_event.go
1717
.tools/semconvgen -o semconv/v1.22.0 -t semconv/template.j2 -s v1.22.0 -i /tmp/semantic-conventions/model/. --only=span -p conventionType=trace -f generated_trace.go
1818
```
19-
20-
When generating the constants for a new version ot the specification it is important to note that only
21-
`generated_trace.go` and `generated_resource.go` are generated automatically. The `schema.go` and `nonstandard.go`
22-
files should be copied from a prior version's package and updated as appropriate. Most important will be to update
23-
the `SchemaURL` constant in `schema.go`.

0 commit comments

Comments
 (0)