NewService creates a new telemetry service with the provided flush function and options.
(flusher func(SendTelemetryPayload), opts ...telemetryServiceOption)
| 211 | |
| 212 | // NewService creates a new telemetry service with the provided flush function and options. |
| 213 | func NewService(flusher func(SendTelemetryPayload), opts ...telemetryServiceOption) ghtelemetry.Service { |
| 214 | telemetryServiceOpts := telemetryServiceOpts{ |
| 215 | additionalDimensions: make(ghtelemetry.Dimensions), |
| 216 | } |
| 217 | for _, opt := range opts { |
| 218 | opt(&telemetryServiceOpts) |
| 219 | } |
| 220 | |
| 221 | deviceID, err := deviceIDFunc() |
| 222 | if err != nil { |
| 223 | deviceID = "<unknown>" |
| 224 | } |
| 225 | |
| 226 | invocationID := uuid.NewString() |
| 227 | |
| 228 | var commonDimensions = ghtelemetry.Dimensions{ |
| 229 | "device_id": deviceID, |
| 230 | "invocation_id": invocationID, |
| 231 | "os": runtime.GOOS, |
| 232 | "architecture": runtime.GOARCH, |
| 233 | } |
| 234 | maps.Copy(commonDimensions, telemetryServiceOpts.additionalDimensions) |
| 235 | |
| 236 | hash := uuid.NewSHA1(uuid.Nil, []byte(invocationID)) |
| 237 | sampleBucket := byte(binary.BigEndian.Uint32(hash[:4]) % 100) |
| 238 | |
| 239 | s := &service{ |
| 240 | flush: flusher, |
| 241 | commonDimensions: commonDimensions, |
| 242 | sampleRate: telemetryServiceOpts.sampleRate, |
| 243 | sampleBucket: sampleBucket, |
| 244 | } |
| 245 | |
| 246 | return s |
| 247 | } |
| 248 | |
| 249 | type recordedEvent struct { |
| 250 | event ghtelemetry.Event |