Skip to content

Commit

Permalink
coap-gateway: set request to opentelemetry attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Jun 15, 2022
1 parent 87c31c0 commit 4305225
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions coap-gateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ func (s *Service) makeCommandTask(req *mux.Message, client *Client, fnc func(req
tmp, err := client.server.messagePool.ConvertFrom(req.Message)
if err == nil {
otelcoap.MessageReceivedEvent(ctx, tmp)
otelcoap.SetRequest(ctx, tmp)
}
return func() {
defer span.End()
Expand Down
20 changes: 20 additions & 0 deletions pkg/opentelemetry/otelcoap/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/plgd-dev/go-coap/v2/message/codes"
tcpMessage "github.com/plgd-dev/go-coap/v2/tcp/message"
"github.com/plgd-dev/go-coap/v2/tcp/message/pool"
pkgMessage "github.com/plgd-dev/hub/v2/coap-gateway/service/message"
"github.com/plgd-dev/hub/v2/pkg/opentelemetry"
"github.com/plgd-dev/kit/v2/codec/json"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
Expand All @@ -19,6 +21,7 @@ var (
COAPStatusCodeKey = attribute.Key("coap.status_code")
COAPMethodKey = attribute.Key("coap.method")
COAPPathKey = attribute.Key("coap.path")
COAPRequest = attribute.Key("coap.request")
)

type MessageType attribute.KeyValue
Expand All @@ -45,6 +48,23 @@ func (m MessageType) Event(ctx context.Context, message *pool.Message) {
))
}

func SetRequest(ctx context.Context, message *pool.Message) {
span := trace.SpanFromContext(ctx)
msg := pkgMessage.ToJson(message, true, false)
if msg.Body != nil {
var request = ""
if body, ok := msg.Body.(string); ok {
request = body
} else {
v, err := json.Encode(msg.Body)
if err == nil {
request = string(v)
}
}
span.SetAttributes(COAPRequest.String(request))
}
}

func DefaultTransportFormatter(path string) string {
return "COAP " + path
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/opentelemetry/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const InstrumentationName = "github.com/plgd-dev/hub/pkg/opentelemetry"

// Version is the current release version of the plgd instrumentation.
func Version() string {
return "0.0.1"
return "0.0.2"
}

// SemVersion is the semantic version to be supplied to tracer/meter creation.
Expand Down

0 comments on commit 4305225

Please sign in to comment.