Skip to content

Commit

Permalink
Add example for gorilla mux instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
krnowak committed Apr 28, 2020
1 parent cdb8f74 commit 495a822
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
20 changes: 20 additions & 0 deletions plugins/gorilla/mux/example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.
FROM golang:alpine AS base
COPY . /src/
WORKDIR /src/plugins/gorilla/mux

FROM base AS mux-server
RUN go install ./example/server.go
CMD ["/go/bin/server"]
28 changes: 28 additions & 0 deletions plugins/gorilla/mux/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# mux-gonic instrumentation example

An HTTP server using gorilla mux and instrumentation. The server has a
`/users/{id:[0-9]+}` endpoint. The server generates span information to
`stdout`.

These instructions expect you have
[docker-compose](https://docs.docker.com/compose/) installed.

Bring up the `mux-server` and `mux-client` services to run the
example:

```sh
docker-compose up --detach mux-server mux-client
```

The `mux-client` service sends just one HTTP request to `mux-server`
and then exits. View the span generated by `mux-server` in the logs:

```sh
docker-compose logs mux-server
```

Shut down the services when you are finished with the example:

```sh
docker-compose down
```
39 changes: 39 additions & 0 deletions plugins/gorilla/mux/example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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.
version: "3.7"
services:
mux-client:
image: golang:alpine
networks:
- example
command:
- "/bin/sh"
- "-c"
- "wget http://mux-server:8080/users/123 && cat 123"
depends_on:
- mux-server
mux-server:
build:
dockerfile: $PWD/Dockerfile
context: ../../../..
ports:
- "8080:80"
command:
- "/bin/sh"
- "-c"
- "/go/bin/server"
networks:
- example
networks:
example:
75 changes: 75 additions & 0 deletions plugins/gorilla/mux/example/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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"

"github.com/gorilla/mux"

muxtrace "go.opentelemetry.io/contrib/plugins/gorilla/mux"
otelglobal "go.opentelemetry.io/otel/api/global"
otelkey "go.opentelemetry.io/otel/api/key"
oteltrace "go.opentelemetry.io/otel/api/trace"
oteltracestdout "go.opentelemetry.io/otel/exporters/trace/stdout"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

var tracer = otelglobal.Tracer("mux-server")

func main() {
initTracer()
r := mux.NewRouter()
r.Use(muxtrace.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() {
exporter, err := oteltracestdout.NewExporter(oteltracestdout.Options{PrettyPrint: true})
if err != nil {
log.Fatal(err)
}
cfg := sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
}
tp, err := sdktrace.NewProvider(
sdktrace.WithConfig(cfg),
sdktrace.WithSyncer(exporter),
)
if err != nil {
log.Fatal(err)
}
otelglobal.SetTraceProvider(tp)
}

func getUser(ctx context.Context, id string) string {
_, span := tracer.Start(ctx, "getUser", oteltrace.WithAttributes(otelkey.String("id", id)))
defer span.End()
if id == "123" {
return "muxtrace tester"
}
return "unknown"
}

0 comments on commit 495a822

Please sign in to comment.