Skip to content

Instantly share code, notes, and snippets.

@stuartnelson3
Last active July 19, 2021 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartnelson3/61b9e61bf4ab78955dc0a7844d26c626 to your computer and use it in GitHub Desktop.
Save stuartnelson3/61b9e61bf4ab78955dc0a7844d26c626 to your computer and use it in GitHub Desktop.
Example go otel server
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
oteltrace "go.opentelemetry.io/otel/trace"
)
var tracer = otel.Tracer("mux-server")
func main() {
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
r := mux.NewRouter()
r.Use(otelmux.Middleware("my-server"))
r.HandleFunc("/users/{id:[0-9]+}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
name := getUser(r.Context(), id)
reply := fmt.Sprintf("user %s (id %s)\n", name, id)
_, _ = w.Write(([]byte)(reply))
}))
http.Handle("/", r)
_ = http.ListenAndServe(":8080", nil)
}
func initTracer() *sdktrace.TracerProvider {
ctx := context.Background()
// Create the OTLP exporter that
// will receive the telemetry data
endpoint := os.Getenv("EXPORTER_ENDPOINT")
if endpoint == "" {
// endpoint = "localhost:55680"
endpoint = "localhost:8200"
}
driver := otlpgrpc.NewDriver(
otlpgrpc.WithInsecure(),
otlpgrpc.WithEndpoint(endpoint),
)
exporter, err := otlp.NewExporter(ctx, driver)
if err != nil {
log.Fatal(err)
}
ary := attribute.Array("unhandled_ids", []string{"123", "456"})
resources := resource.NewWithAttributes(ary)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}
func getUser(ctx context.Context, id string) string {
ary := attribute.Array("other_ids", []string{"778", "097"})
_, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(ary))
// _, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(attribute.String("some_new_label", id)))
defer span.End()
if id == "123" {
return "otelmux tester"
}
return "unknown"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment