Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
.spin
*.wasm
3 changes: 3 additions & 0 deletions go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.phony: go-bindings
go-bindings:
go run go.bytecodealliance.org/cmd/wit-bindgen-go generate --world imports --out internal ../../wit
21 changes: 20 additions & 1 deletion go/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# OpenTelemetry WASI for Go

TODO Not implemented yet
## Requirements
- [Go](https://go.dev/dl/) version `1.24`
- [Tinygo](https://github.com/tinygo-org/tinygo/releases/tag/v0.38.0) version `0.38`

## Usage

```sh
git clone --depth 1 --branch factor-otel https://github.com/asteurer/otel-spin
cargo install --path otel-spin
spin plugin update
spin plugin install otel
```

```sh
cd examples/spin-basic
spin build
spin otel setup
spin otel up
curl localhost:3000
```
135 changes: 135 additions & 0 deletions go/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package wasi_otel

import (
"time"

wasiTrace "github.com/calebschoepp/opentelemetry-wasi/internal/wasi/otel/tracing"
"go.bytecodealliance.org/cm"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/sdk/instrumentation"
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

func toWasiTraceState(s trace.TraceState) wasiTrace.TraceState {
// TODO: double check that this actually works
result := make([][2]string, 0)
s.Walk(func(k, v string) bool {
result = append(result, [2]string{k, v})
return true
})

return wasiTrace.TraceState(cm.ToList(result))
}

func toWasiDateTime(t time.Time) wasiTrace.DateTime {
// TODO: Make sure this is retrieving the correct time
return wasiTrace.DateTime{
Seconds: uint64(t.Second()),
Nanoseconds: uint32(t.Nanosecond()),
}
}

func toWasiAttributes(attributes []attribute.KeyValue) cm.List[wasiTrace.KeyValue] {
result := cm.NewList[wasiTrace.KeyValue](nil, 0)
for _, entry := range attributes {
result = cm.NewList(&wasiTrace.KeyValue{
Key: wasiTrace.Key(entry.Key),
Value: toWasiValue(entry.Value),
}, len(attributes))
}

return result
}

func toWasiEvents(events []sdkTrace.Event) cm.List[wasiTrace.Event] {
result := cm.NewList[wasiTrace.Event](nil, 0)
for _, event := range events {
result = cm.NewList(&wasiTrace.Event{
Name: event.Name,
Time: toWasiDateTime(event.Time),
Attributes: toWasiAttributes(event.Attributes),
}, len(events))
}

return result
}

func toWasiSpanContext(s trace.SpanContext) wasiTrace.SpanContext {
return wasiTrace.SpanContext{
TraceID: wasiTrace.TraceID(s.TraceID().String()),
SpanID: wasiTrace.SpanID(s.SpanID().String()),
TraceFlags: wasiTrace.TraceFlags(s.TraceFlags()),
IsRemote: s.IsRemote(),
TraceState: toWasiTraceState(s.TraceState()),
}
}

func toWasiLinks(links []sdkTrace.Link) cm.List[wasiTrace.Link] {
result := cm.NewList[wasiTrace.Link](nil, 0)
for _, link := range links {
result = cm.NewList(&wasiTrace.Link{
SpanContext: toWasiSpanContext(link.SpanContext),
Attributes: toWasiAttributes(link.Attributes),
}, len(links))
}

return result
}

func toWasiInstrumentationScope(s instrumentation.Scope) wasiTrace.InstrumentationScope {
return wasiTrace.InstrumentationScope{
Name: s.Name,
Version: getOption(s.Version),
SchemaURL: getOption(s.SchemaURL),
Attributes: toWasiAttributes(s.Attributes.ToSlice()),
}
}

func toWasiStatus(s sdkTrace.Status) wasiTrace.Status {
switch s.Code {
case codes.Error:
return wasiTrace.StatusError(s.Description)
case codes.Ok:
return wasiTrace.StatusOK()
default:
return wasiTrace.StatusUnset()
}
}

func toWasiValue(v attribute.Value) wasiTrace.Value {
switch v.Type() {
case attribute.STRING:
return wasiTrace.ValueString_(v.AsString())
case attribute.BOOL:
return wasiTrace.ValueBool(v.AsBool())
case attribute.FLOAT64:
return wasiTrace.ValueF64(v.AsFloat64())
case attribute.INT64:
return wasiTrace.ValueS64(v.AsInt64())
case attribute.STRINGSLICE:
stringSlice := v.AsStringSlice()
return wasiTrace.ValueStringArray(cm.ToList(stringSlice))
case attribute.BOOLSLICE:
boolSlice := v.AsBoolSlice()
return wasiTrace.ValueBoolArray(cm.ToList(boolSlice))
case attribute.FLOAT64SLICE:
floatSlice := v.AsFloat64Slice()
return wasiTrace.ValueF64Array(cm.ToList(floatSlice))
case attribute.INT64SLICE:
intSlice := v.AsInt64Slice()
return wasiTrace.ValueS64Array(cm.ToList(intSlice))
default:
return wasiTrace.ValueString_(v.AsString())
}
}

func getOption[T comparable](v T) cm.Option[T] {
// TODO: Claude claims that this is the best way to do this function. VALIDATE
var empty T
if v == empty {
return cm.None[T]()
}
return cm.Some(v)
}
10 changes: 10 additions & 0 deletions go/examples/bare-bones-wasi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# NOTE: the `tinygo build` command also works with `-target=wasip2`
.phony: build
build:
@wkg wit fetch -d pkg/sdk/wit
@wkg wit build -d pkg/sdk/wit
@cd pkg/app && tinygo build -target=wasip1 -o ../../main.wasm main.go

.phony: run
run:
@wasmtime main.wasm
9 changes: 9 additions & 0 deletions go/examples/bare-bones-wasi/pkg/app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module asteurer.com

go 1.24.4

require sdk.com v0.0.0

require go.bytecodealliance.org/cm v0.3.0 // indirect

replace sdk.com => ../sdk
2 changes: 2 additions & 0 deletions go/examples/bare-bones-wasi/pkg/app/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.bytecodealliance.org/cm v0.3.0 h1:VhV+4vjZPUGCozCg9+up+FNL3YU6XR+XKghk7kQ0vFc=
go.bytecodealliance.org/cm v0.3.0/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=
13 changes: 13 additions & 0 deletions go/examples/bare-bones-wasi/pkg/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"

"sdk.com"
)

func init() {
fmt.Println(sdk.AddExternal(123, 456))
}

func main() {}
22 changes: 22 additions & 0 deletions go/examples/bare-bones-wasi/pkg/sdk/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module sdk.com

go 1.24.4

tool go.bytecodealliance.org/cmd/wit-bindgen-go

require go.bytecodealliance.org/cm v0.3.0

require (
github.com/coreos/go-semver v0.3.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/regclient/regclient v0.8.3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tetratelabs/wazero v1.9.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/urfave/cli/v3 v3.3.3 // indirect
go.bytecodealliance.org v0.7.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sys v0.33.0 // indirect
)
48 changes: 48 additions & 0 deletions go/examples/bare-bones-wasi/pkg/sdk/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/olareg/olareg v0.1.2 h1:75G8X6E9FUlzL/CSjgFcYfMgNzlc7CxULpUUNsZBIvI=
github.com/olareg/olareg v0.1.2/go.mod h1:TWs+N6pO1S4bdB6eerzUm/ITRQ6kw91mVf9ZYeGtw+Y=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/regclient/regclient v0.8.3 h1:AFAPu/vmOYGyY22AIgzdBUKbzH+83lEpRioRYJ/reCs=
github.com/regclient/regclient v0.8.3/go.mod h1:gjQh5uBVZoo/CngchghtQh9Hx81HOMKRRDd5WPcPkbk=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v3 v3.3.3 h1:byCBaVdIXuLPIDm5CYZRVG6NvT7tv1ECqdU4YzlEa3I=
github.com/urfave/cli/v3 v3.3.3/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
go.bytecodealliance.org v0.7.0 h1:CTJ1eb5kFhBKHw1/xycxxz4SmVWNKXYHhrA78oLNXhY=
go.bytecodealliance.org v0.7.0/go.mod h1:PCLMft5yTQsHT9oNPWlq0I6Qdmo6THvdky2AZHjNUkA=
go.bytecodealliance.org/cm v0.3.0 h1:VhV+4vjZPUGCozCg9+up+FNL3YU6XR+XKghk7kQ0vFc=
go.bytecodealliance.org/cm v0.3.0/go.mod h1:JD5vtVNZv7sBoQQkvBvAAVKJPhR/bqBH7yYXTItMfZI=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading