Skip to content

Instantly share code, notes, and snippets.

@dgzlopes
Last active November 3, 2023 12:10
Show Gist options
  • Save dgzlopes/831a393c8071193b50165df9b72d3653 to your computer and use it in GitHub Desktop.
Save dgzlopes/831a393c8071193b50165df9b72d3653 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
collectorpb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"google.golang.org/protobuf/encoding/protojson"
)
func main() {
span := tracepb.Span{
TraceId: []byte("9890e50e34146fb0c3f371322fa98681"),
SpanId: []byte("53fc540cb095d5e2"),
ParentSpanId: []byte(""),
Name: "my-span",
Kind: *tracepb.Span_SPAN_KIND_CLIENT.Enum(),
StartTimeUnixNano: uint64(time.Now().Add(-2 * time.Second).UnixNano()),
EndTimeUnixNano: uint64(time.Now().UnixNano()),
Attributes: []*commonpb.KeyValue{
{
Key: "span.kind",
Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_StringValue{StringValue: "client"}},
},
},
Status: &tracepb.Status{
Code: 0,
Message: "OK",
},
}
td := collectorpb.ExportTraceServiceRequest{
ResourceSpans: []*tracepb.ResourceSpans{
{
Resource: &resourcepb.Resource{
Attributes: []*commonpb.KeyValue{
{
Key: "service.name",
Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_StringValue{StringValue: "my-service"}},
},
},
},
ScopeSpans: []*tracepb.ScopeSpans{
{
Scope: &commonpb.InstrumentationScope{
Name: "my-scope",
},
Spans: []*tracepb.Span{
&span,
},
},
},
},
},
}
payload, err := protojson.Marshal(&td)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", "http://localhost:4318/v1/traces", bytes.NewBuffer(payload))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
fmt.Println(resp.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment