Skip to content

Commit

Permalink
Add a simple HTTP example (#195)
Browse files Browse the repository at this point in the history
* Add a simple HTTP example

* fix lint + create a server instance

* remove un-needed files

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
dineshg13 and MrAlias committed Jun 27, 2023
1 parent dca14f8 commit 9940407
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/rolldice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM golang:1.20
WORKDIR /app
COPY . .
RUN go build -o main
ENTRYPOINT ["/app/main"]
19 changes: 19 additions & 0 deletions examples/rolldice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Example of Auto instrumentation of HTTP server

Rolldice server exposes an endpoint `/rolldice.` When we hit the endpoint, it returns a random number between 1-6.

For testing auto instrumentation, we can use the docker compose.

To run the example, bring up the services using the command.

```
docker compose up
```

Now, you can hit roll dice server using the below command
```
curl localhost:8080/rolldice
```
Every hit to the server should generate a trace that we can observe in [Jaeger UI](http://localhost:16686/)

Example trace ![Image](jaeger.jpg)
54 changes: 54 additions & 0 deletions examples/rolldice/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: "3.9"

networks:
default:
name: roll
driver: bridge

services:
rolldice:
depends_on:
- jaeger
build:
context: .
dockerfile: ./Dockerfile
pid: "host"
ports:
- "8080:8080"
volumes:
- shared-data:/app
- /proc:/host/proc
go-auto:
depends_on:
- rolldice
build:
context: ../..
dockerfile: Dockerfile
privileged: true
pid: "host"
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4317
- OTEL_GO_AUTO_TARGET_EXE=/app/main
- OTEL_SERVICE_NAME=rolldice
- OTEL_PROPAGATORS=tracecontext,baggage
volumes:
- shared-data:/app
- /proc:/host/proc

jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686"
- "14268:14268"
environment:
- COLLECTOR_OTLP_ENABLED=true
- LOG_LEVEL=debug
deploy:
resources:
limits:
memory: 300M
restart: unless-stopped


volumes:
shared-data:
10 changes: 10 additions & 0 deletions examples/rolldice/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module go.openetelemetry.io/auto/examples/rolldice

go 1.20

require go.uber.org/zap v1.24.0

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
)
18 changes: 18 additions & 0 deletions examples/rolldice/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
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/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Binary file added examples/rolldice/jaeger.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions examples/rolldice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 (
"fmt"
"math/rand"
"net/http"
"time"

"go.uber.org/zap"
)

// Server is Http server that exposes multiple endpoints.
type Server struct {
rand *rand.Rand
}

// NewServer creates a server struct after initialing rand.
func NewServer() *Server {
rd := rand.New(rand.NewSource(time.Now().Unix()))
return &Server{
rand: rd,
}
}

func (s *Server) rolldice(w http.ResponseWriter, _ *http.Request) {
n := s.rand.Intn(6)
logger.Info("rolldice called", zap.Int("dice", n))
fmt.Fprintf(w, "%v", n)
}

var logger *zap.Logger

func setupHandler(s *Server) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/rolldice", s.rolldice)
return mux
}

func main() {
var err error
logger, err = zap.NewDevelopment()
if err != nil {
fmt.Printf("error creating zap logger, error:%v", err)
return
}
port := fmt.Sprintf(":%d", 8080)
logger.Info("starting http server", zap.String("port", port))

s := NewServer()
mux := setupHandler(s)
if err := http.ListenAndServe(port, mux); err != nil {
logger.Error("error running server", zap.Error(err))
}
}

0 comments on commit 9940407

Please sign in to comment.