Skip to content

Commit

Permalink
Merge branch 'main' into add-process-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Jan 4, 2022
2 parents 5863b1a + 02a2d07 commit aa97090
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed

- `runtime.go.*` instrumentation metrics have been renamed to `process.runtime.go.*` according to metrics conventions (#1549)
- All metric instruments from the `go.opentelemetry.io/contrib/instrumentation/runtime` package have been renamed from `runtime.go.*` to `process.runtime.go.*` so as to comply with OpenTelemetry semantic conventions. (#1549)

### Fixed

- Change the `http-server-duration` instrument in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` to record milliseconds instead of microseconds match what is specified in the OpenTelemetry specification. (#1414, #1537)

## [1.3.0/0.28.0] - 2021-12-10

Expand Down
13 changes: 12 additions & 1 deletion detectors/gcp/cloud-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"strings"

"cloud.google.com/go/compute/metadata"

Expand Down Expand Up @@ -56,6 +57,16 @@ func NewCloudRun() *CloudRun {
}
}

func (c *CloudRun) cloudRegion(ctx context.Context) (string, error) {
region, err := c.mc.Get("instance/region")
if err != nil {
return "", err
}
// Region from the metadata server is in the format /projects/123/regions/r.
// https://cloud.google.com/run/docs/reference/container-contract#metadata-server
return region[strings.LastIndex(region, "/")+1:], nil
}

// Detect detects associated resources when running on Cloud Run hosts.
// NOTE: the service.namespace attribute is currently hardcoded to be
// "cloud-run-managed". This may change in the future, please do not rely on
Expand All @@ -80,7 +91,7 @@ func (c *CloudRun) Detect(ctx context.Context) (*resource.Resource, error) {
attributes = append(attributes, semconv.CloudAccountIDKey.String(projectID))
}

if region, err := c.mc.Get("instance/region"); hasProblem(err) {
if region, err := c.cloudRegion(ctx); hasProblem(err) {
errInfo = append(errInfo, err.Error())
} else if region != "" {
attributes = append(attributes, semconv.CloudRegionKey.String(region))
Expand Down
2 changes: 1 addition & 1 deletion detectors/gcp/cloud-run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestCloudRunDetectorExpectSuccess(t *testing.T) {
metadata := map[string]string{
"project/project-id": "foo",
"instance/id": "bar",
"instance/region": "utopia",
"instance/region": "/projects/123/regions/utopia",
}
envvars := map[string]string{
"K_SERVICE": "x-service",
Expand Down
9 changes: 5 additions & 4 deletions instrumentation/net/http/otelhttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Handler struct {
filters []Filter
spanNameFormatter func(string, *http.Request) string
counters map[string]metric.Int64Counter
valueRecorders map[string]metric.Int64Histogram
valueRecorders map[string]metric.Float64Histogram
}

func defaultHandlerFormatter(operation string, _ *http.Request) string {
Expand Down Expand Up @@ -94,15 +94,15 @@ func handleErr(err error) {

func (h *Handler) createMeasures() {
h.counters = make(map[string]metric.Int64Counter)
h.valueRecorders = make(map[string]metric.Int64Histogram)
h.valueRecorders = make(map[string]metric.Float64Histogram)

requestBytesCounter, err := h.meter.NewInt64Counter(RequestContentLength)
handleErr(err)

responseBytesCounter, err := h.meter.NewInt64Counter(ResponseContentLength)
handleErr(err)

serverLatencyMeasure, err := h.meter.NewInt64Histogram(ServerLatency)
serverLatencyMeasure, err := h.meter.NewFloat64Histogram(ServerLatency)
handleErr(err)

h.counters[RequestContentLength] = requestBytesCounter
Expand Down Expand Up @@ -195,7 +195,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.counters[RequestContentLength].Add(ctx, bw.read, attributes...)
h.counters[ResponseContentLength].Add(ctx, rww.written, attributes...)

elapsedTime := time.Since(requestStartTime).Microseconds()
// Use floating point division here for higher precision (instead of Millisecond method).
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond)

h.valueRecorders[ServerLatency].Record(ctx, elapsedTime, attributes...)
}
Expand Down

0 comments on commit aa97090

Please sign in to comment.