Skip to content
Merged
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
20 changes: 10 additions & 10 deletions go/examples/spin-basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()

/*
### LOGS ###
*/
loggerProvider := log.NewLoggerProvider(log.WithProcessor(wasiLogs.NewWasiLogProcessor()))
logger := loggerProvider.Logger("spin-logs")
logRecord := logApi.Record{}
logRecord.SetBody(logApi.StringValue("Hello from Go!"))
logRecord.SetSeverity(logApi.SeverityInfo)
logger.Emit(ctx, logRecord)

/*
### METRICS ###
*/
Expand Down Expand Up @@ -112,6 +102,16 @@ func init() {
childSpan.SetStatus(codes.Ok, "success")
}()

/*
### LOGS ###
*/
loggerProvider := log.NewLoggerProvider(log.WithProcessor(wasiLogs.NewWasiLogProcessor()))
logger := loggerProvider.Logger("spin-logs")
logRecord := logApi.Record{}
logRecord.SetBody(logApi.StringValue("Hello from Go!"))
logRecord.SetSeverity(logApi.SeverityInfo)
logger.Emit(hostCtx, logRecord) // To have the log associated with the trace, we pass the host context

w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello World!")
})
Expand Down
9 changes: 7 additions & 2 deletions go/tracing/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracing
import (
"encoding/hex"
"fmt"
"strings"

"github.com/bytecodealliance/wit-bindgen/wit_types"
"github.com/calebschoepp/opentelemetry-wasi/internal/wasi_otel_tracing"
Expand Down Expand Up @@ -89,15 +90,19 @@ func toWasiSpanKind(sk traceApi.SpanKind) wasi_otel_tracing.SpanKind {
}

func toOtelSpanContext(ctx wasi_otel_tracing.SpanContext) traceApi.SpanContext {
tid, err := hex.DecodeString(ctx.TraceId)
traceIdHex := fmt.Sprintf("%032s", ctx.TraceId)
traceIdHex = strings.ReplaceAll(traceIdHex, " ", "0")
tid, err := hex.DecodeString(traceIdHex)
if err != nil {
panic(fmt.Sprintf("invalid trace ID: %v", err))
}
if len(tid) != 16 {
panic(fmt.Sprintf("trace ID must be 16 bytes, got %d", len(tid)))
}

sid, err := hex.DecodeString(ctx.SpanId)
spanIdHex := fmt.Sprintf("%016s", ctx.SpanId)
spanIdHex = strings.ReplaceAll(spanIdHex, " ", "0")
sid, err := hex.DecodeString(spanIdHex)
if err != nil {
panic(fmt.Sprintf("invalid span ID: %v", err))
}
Expand Down
14 changes: 8 additions & 6 deletions ts/examples/spin-basic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const provider = new BasicTracerProvider({

const router = AutoRouter();
router.get('/', async () => {
const hostContext = propagator.extract(context.active());

/*
### METRICS ###
*/
Expand Down Expand Up @@ -64,16 +66,16 @@ router.get('/', async () => {

const logger = logs.getLogger('spin-logs');
logger.emit({
severityNumber: SeverityNumber.INFO,
severityText: 'INFO',
body: 'Hello from TypeScript!',
attributes: attrs,
});
severityNumber: SeverityNumber.INFO,
severityText: 'INFO',
body: 'Hello from TypeScript!',
attributes: attrs,
context: hostContext, // To have the log associated with the trace, we pass the host context
});

/*
### TRACING ###
*/
const hostContext = propagator.extract(context.active());
const tracer = provider.getTracer('basic-spin');
return tracer.startActiveSpan(
'main-operation',
Expand Down
7 changes: 4 additions & 3 deletions ts/src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LogRecord as WasiLogRecord } from 'wasi:otel/logs@0.2.0-rc.2';
import { KeyValue as WasiKeyValue } from 'wasi:otel/types@0.2.0-rc.2';
import { dateTimeToWasi, instrumentationScopeToWasi } from './types';
import { AnyValue, AnyValueMap } from '@opentelemetry/api-logs';
import { TraceFlags } from '@opentelemetry/api';

export class WasiLogProcessor implements LogRecordProcessor {
onEmit(logRecord: SdkLogRecord): void {
Expand Down Expand Up @@ -38,9 +39,9 @@ function logRecordToWasi(r: SdkLogRecord): WasiLogRecord {
schemaUrl: r.resource.schemaUrl,
},
instrumentationScope: instrumentationScopeToWasi(r.instrumentationScope),
traceId: undefined,
spanId: undefined,
traceFlags: undefined,
traceId: r.spanContext?.traceId,
spanId: r.spanContext?.spanId,
traceFlags: r.spanContext ? { sampled: !!(r.spanContext.traceFlags & TraceFlags.SAMPLED) } : undefined,
};
}

Expand Down
Loading