forked from vfarcic/cncf-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.go
33 lines (29 loc) · 864 Bytes
/
fibonacci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel/attribute"
)
func fibonacciHandler(ctx *gin.Context) {
traceContext, span := tp.Tracer(serviceName).Start(ctx, "fibonacci")
defer func() { span.End() }()
number, err := strconv.Atoi(ctx.Query("number"))
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
fib := calculateFibonacci(number, traceContext)
ctx.String(http.StatusOK, fmt.Sprintf("%d", fib))
}
func calculateFibonacci(n int, ctx context.Context) int {
traceContext, span := tp.Tracer(serviceName).Start(ctx, "fibonacci-"+strconv.Itoa(n))
span.SetAttributes(attribute.String("number", strconv.Itoa(n)))
defer func() { span.End() }()
if n <= 1 {
return n
}
return calculateFibonacci(n-1, traceContext) + calculateFibonacci(n-2, traceContext)
}