Skip to content

Commit

Permalink
ci: add more tests (#21)
Browse files Browse the repository at this point in the history
add more tests!
  • Loading branch information
123liuziming authored and y1yang0 committed Aug 9, 2024
1 parent 89fb683 commit 385785d
Show file tree
Hide file tree
Showing 25 changed files with 1,323 additions and 103 deletions.
20 changes: 7 additions & 13 deletions pkg/inst-api-semconv/instrumenter/db/db_client_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import (
"context"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api/utils"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
)

const db_name = attribute.Key("db.name")
const db_system = attribute.Key("db.system")
const db_user = attribute.Key("db.user")
const db_connection_string = attribute.Key("db.connection_string")
const db_statement = attribute.Key("db.statement")
const db_operation = attribute.Key("db.operation")

type DbClientCommonAttrsExtractor[REQUEST any, RESPONSE any, GETTER DbClientCommonAttrsGetter[REQUEST]] struct {
getter GETTER
}
Expand All @@ -23,16 +17,16 @@ func (d *DbClientCommonAttrsExtractor[REQUEST, RESPONSE, GETTER]) GetSpanKey() a

func (d *DbClientCommonAttrsExtractor[REQUEST, RESPONSE, GETTER]) OnStart(attributes []attribute.KeyValue, parentContext context.Context, request REQUEST) []attribute.KeyValue {
attributes = append(attributes, attribute.KeyValue{
Key: db_name,
Key: semconv.DBNameKey,
Value: attribute.StringValue(d.getter.GetName(request)),
}, attribute.KeyValue{
Key: db_system,
Key: semconv.DBSystemKey,
Value: attribute.StringValue(d.getter.GetSystem(request)),
}, attribute.KeyValue{
Key: db_user,
Key: semconv.DBUserKey,
Value: attribute.StringValue(d.getter.GetUser(request)),
}, attribute.KeyValue{
Key: db_connection_string,
Key: semconv.DBConnectionStringKey,
Value: attribute.StringValue(d.getter.GetConnectionString(request)),
})
return attributes
Expand All @@ -49,10 +43,10 @@ type DbClientAttrsExtractor[REQUEST any, RESPONSE any, GETTER DbClientAttrsGette
func (d *DbClientAttrsExtractor[REQUEST, RESPONSE, GETTER]) OnStart(attrs []attribute.KeyValue, parentContext context.Context, request REQUEST) []attribute.KeyValue {
attrs = d.base.OnStart(attrs, parentContext, request)
attrs = append(attrs, attribute.KeyValue{
Key: db_statement,
Key: semconv.DBStatementKey,
Value: attribute.StringValue(d.base.getter.GetStatement(request)),
}, attribute.KeyValue{
Key: db_operation,
Key: semconv.DBOperationKey,
Value: attribute.StringValue(d.base.getter.GetOperation(request)),
})
return attrs
Expand Down
93 changes: 93 additions & 0 deletions pkg/inst-api-semconv/instrumenter/db/db_client_extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package db

import (
"context"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api/utils"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
"log"
"testing"
)

type testRequest struct {
Name string
Operation string
}

type testResponse struct {
}

type mongoAttrsGetter struct {
}

func (m mongoAttrsGetter) GetSystem(request testRequest) string {
return "test"
}

func (m mongoAttrsGetter) GetUser(request testRequest) string {
return "test"
}

func (m mongoAttrsGetter) GetName(request testRequest) string {
if request.Name != "" {
return request.Name
}
return ""
}

func (m mongoAttrsGetter) GetConnectionString(request testRequest) string {
return "test"
}

func (m mongoAttrsGetter) GetStatement(request testRequest) string {
return "test"
}

func (m mongoAttrsGetter) GetOperation(request testRequest) string {
if request.Operation != "" {
return request.Operation
}
return ""
}

func TestGetSpanKey(t *testing.T) {
dbExtractor := &DbClientAttrsExtractor[testRequest, any, mongoAttrsGetter]{}
if dbExtractor.GetSpanKey() != utils.DB_CLIENT_KEY {
t.Fatalf("Should have returned DB_CLIENT_KEY")
}
}

func TestDbClientExtractorStart(t *testing.T) {
dbExtractor := DbClientAttrsExtractor[testRequest, testResponse, mongoAttrsGetter]{}
attrs := make([]attribute.KeyValue, 0)
parentContext := context.Background()
attrs = dbExtractor.OnStart(attrs, parentContext, testRequest{Name: "test"})
if attrs[0].Key != semconv.DBNameKey || attrs[0].Value.AsString() != "test" {
t.Fatalf("db name should be test")
}
if attrs[1].Key != semconv.DBSystemKey || attrs[1].Value.AsString() != "test" {
t.Fatalf("db system should be test")
}
if attrs[2].Key != semconv.DBUserKey || attrs[2].Value.AsString() != "test" {
t.Fatalf("db user should be test")
}
if attrs[3].Key != semconv.DBConnectionStringKey || attrs[3].Value.AsString() != "test" {
t.Fatalf("db connection key should be test")
}
if attrs[4].Key != semconv.DBStatementKey || attrs[4].Value.AsString() != "test" {
t.Fatalf("db statement key should be test")
}
if attrs[5].Key != semconv.DBOperationKey || attrs[5].Value.AsString() != "" {
t.Fatalf("db operation key should be empty")
}
}

func TestDbClientExtractorEnd(t *testing.T) {
dbExtractor := DbClientAttrsExtractor[testRequest, testResponse, mongoAttrsGetter]{}
attrs := make([]attribute.KeyValue, 0)
parentContext := context.Background()
attrs = dbExtractor.OnEnd(attrs, parentContext, testRequest{Name: "test"}, testResponse{}, nil)
if len(attrs) != 0 {
log.Fatal("attrs should be empty")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package db

import "testing"

func TestDbNameExtractor(t *testing.T) {
dbSpanNameExtractor := DBSpanNameExtractor[testRequest]{
getter: mongoAttrsGetter{},
}
if dbSpanNameExtractor.Extract(testRequest{}) != "DB Query" {
t.Fatalf("Should have returned DB_QUERY")
}
if dbSpanNameExtractor.Extract(testRequest{Name: "test"}) != "test" {
t.Fatalf("Should have returned test")
}
if dbSpanNameExtractor.Extract(testRequest{Operation: "op_test"}) != "op_test" {
t.Fatalf("Should have returned op_test")
}
}
26 changes: 8 additions & 18 deletions pkg/inst-api-semconv/instrumenter/http/http_attrs_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@ import (
"context"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api-semconv/instrumenter/net"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)

const http_request_method = attribute.Key("http.request.method")
const http_response_status_code = attribute.Key("http.response.status_code")
const http_route = attribute.Key("http.route")

const network_protocol_name = attribute.Key("network.protocol.name")
const network_protocol_version = attribute.Key("network.protocol.version")

const url_full = attribute.Key("url.full")

const user_agent_original = attribute.Key("user_agent.original")

// TODO: http.route

type HttpCommonAttrsExtractor[REQUEST any, RESPONSE any, GETTER1 HttpCommonAttrsGetter[REQUEST, RESPONSE], GETTER2 net.NetworkAttrsGetter[REQUEST, RESPONSE]] struct {
Expand All @@ -27,7 +17,7 @@ type HttpCommonAttrsExtractor[REQUEST any, RESPONSE any, GETTER1 HttpCommonAttrs

func (h *HttpCommonAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2]) OnStart(attributes []attribute.KeyValue, parentContext context.Context, request REQUEST) []attribute.KeyValue {
attributes = append(attributes, attribute.KeyValue{
Key: http_request_method,
Key: semconv.HTTPRequestMethodKey,
Value: attribute.StringValue(h.httpGetter.GetRequestMethod(request)),
})
return attributes
Expand All @@ -38,13 +28,13 @@ func (h *HttpCommonAttrsExtractor[REQUEST, RESPONSE, GETTER, GETTER2]) OnEnd(att
protocolName := h.netGetter.GetNetworkProtocolName(request, response)
protocolVersion := h.netGetter.GetNetworkProtocolVersion(request, response)
attributes = append(attributes, attribute.KeyValue{
Key: http_response_status_code,
Key: semconv.HTTPResponseStatusCodeKey,
Value: attribute.IntValue(statusCode),
}, attribute.KeyValue{
Key: network_protocol_name,
Key: semconv.NetworkProtocolNameKey,
Value: attribute.StringValue(protocolName),
}, attribute.KeyValue{
Key: network_protocol_version,
Key: semconv.NetworkProtocolVersionKey,
Value: attribute.StringValue(protocolVersion),
})
return attributes
Expand All @@ -60,7 +50,7 @@ func (h *HttpClientAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2]) OnStart(
fullUrl := h.base.httpGetter.GetUrlFull(request)
// TODO: add resend count
attributes = append(attributes, attribute.KeyValue{
Key: url_full,
Key: semconv.URLFullKey,
Value: attribute.StringValue(fullUrl),
})
return attributes
Expand Down Expand Up @@ -88,10 +78,10 @@ func (h *HttpServerAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2, GETTER3])
} else {
firstUserAgent = ""
}
attributes = append(attributes, attribute.KeyValue{Key: http_route,
attributes = append(attributes, attribute.KeyValue{Key: semconv.HTTPRouteKey,
Value: attribute.StringValue(h.base.httpGetter.GetHttpRoute(request)),
}, attribute.KeyValue{
Key: user_agent_original,
Key: semconv.UserAgentOriginalKey,
Value: attribute.StringValue(firstUserAgent),
})
return attributes
Expand Down
Loading

0 comments on commit 385785d

Please sign in to comment.